IPv6: move assignPublicIp6Address() from NetworkManager to new Ipv6AddressManager

Don't want to involve IP address management code in NetworkManager.
This commit is contained in:
Sheng Yang 2013-01-29 18:02:32 -08:00
parent 1f2147d3ec
commit cb9f2d018d
10 changed files with 172 additions and 79 deletions

View File

@ -91,6 +91,7 @@ import com.cloud.keystore.KeystoreManagerImpl;
import com.cloud.maint.UpgradeManagerImpl; import com.cloud.maint.UpgradeManagerImpl;
import com.cloud.maint.dao.AgentUpgradeDaoImpl; import com.cloud.maint.dao.AgentUpgradeDaoImpl;
import com.cloud.network.ExternalLoadBalancerUsageManagerImpl; import com.cloud.network.ExternalLoadBalancerUsageManagerImpl;
import com.cloud.network.Ipv6AddressManagerImpl;
import com.cloud.network.NetworkManagerImpl; import com.cloud.network.NetworkManagerImpl;
import com.cloud.network.NetworkModelImpl; import com.cloud.network.NetworkModelImpl;
import com.cloud.network.NetworkServiceImpl; import com.cloud.network.NetworkServiceImpl;
@ -464,6 +465,7 @@ public class DefaultComponentLibrary extends ComponentLibraryBase implements Com
addManager("TaggedResourcesManager", TaggedResourceManagerImpl.class); addManager("TaggedResourcesManager", TaggedResourceManagerImpl.class);
addManager("Site2SiteVpnManager", Site2SiteVpnManagerImpl.class); addManager("Site2SiteVpnManager", Site2SiteVpnManagerImpl.class);
addManager("QueryManager", QueryManagerImpl.class); addManager("QueryManager", QueryManagerImpl.class);
addManager("Ipv6AddressManager", Ipv6AddressManagerImpl.class);
} }
@Override @Override

View File

@ -0,0 +1,28 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.network;
import com.cloud.exception.InsufficientAddressCapacityException;
import com.cloud.user.Account;
import com.cloud.utils.component.Manager;
public interface Ipv6AddressManager extends Manager {
public PublicIpv6Address assignDirectIp6Address(long dcId, Account owner, Long networkId, String requestedIp6) throws InsufficientAddressCapacityException;
public void revokeDirectIpv6Address(long networkId, String ip6Address);
}

View File

@ -0,0 +1,132 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.network;
import java.util.Map;
import javax.ejb.Local;
import javax.naming.ConfigurationException;
import org.apache.log4j.Logger;
import com.cloud.dc.DataCenterVO;
import com.cloud.dc.Vlan;
import com.cloud.dc.dao.DataCenterDao;
import com.cloud.dc.dao.VlanDao;
import com.cloud.exception.InsufficientAddressCapacityException;
import com.cloud.network.dao.PublicIpv6AddressDao;
import com.cloud.user.Account;
import com.cloud.utils.component.Inject;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.net.NetUtils;
@Local(value = { Ipv6AddressManager.class } )
public class Ipv6AddressManagerImpl implements Ipv6AddressManager {
public static final Logger s_logger = Logger.getLogger(Ipv6AddressManagerImpl.class.getName());
String _name = null;
@Inject
DataCenterDao _dcDao;
@Inject
VlanDao _vlanDao;
@Inject
NetworkModel _networkModel;
@Inject
PublicIpv6AddressDao _ipv6Dao;
@Override
public boolean configure(String name, Map<String, Object> params)
throws ConfigurationException {
_name = name;
return true;
}
@Override
public boolean start() {
return true;
}
@Override
public boolean stop() {
return true;
}
@Override
public String getName() {
return _name;
}
@Override
public PublicIpv6Address assignDirectIp6Address(long dcId, Account owner, Long networkId, String requestedIp6)
throws InsufficientAddressCapacityException {
Vlan vlan = _networkModel.getVlanForNetwork(networkId);
if (vlan == null) {
s_logger.debug("Cannot find related vlan or too many vlan attached to network " + networkId);
return null;
}
String ip = null;
if (requestedIp6 == null) {
int count = 0;
while (ip == null || count >= 10) {
ip = NetUtils.getIp6FromRange(vlan.getIp6Range());
//Check for duplicate IP
if (_ipv6Dao.findByNetworkIdAndIp(networkId, ip) == null) {
break;
} else {
ip = null;
}
count ++;
}
if (ip == null) {
throw new CloudRuntimeException("Fail to get unique ipv6 address after 10 times trying!");
}
} else {
if (!NetUtils.isIp6InRange(requestedIp6, vlan.getIp6Range())) {
throw new CloudRuntimeException("Requested IPv6 is not in the predefined range!");
}
ip = requestedIp6;
if (_ipv6Dao.findByNetworkIdAndIp(networkId, ip) != null) {
throw new CloudRuntimeException("The requested IP is already taken!");
}
}
DataCenterVO dc = _dcDao.findById(dcId);
Long mac = dc.getMacAddress();
Long nextMac = mac + 1;
dc.setMacAddress(nextMac);
_dcDao.update(dc.getId(), dc);
String macAddress = NetUtils.long2Mac(NetUtils.createSequenceBasedMacAddress(mac));
PublicIpv6AddressVO ipVO = new PublicIpv6AddressVO(ip, dcId, macAddress, vlan.getId());
ipVO.setPhysicalNetworkId(vlan.getPhysicalNetworkId());
ipVO.setSourceNetworkId(vlan.getNetworkId());
ipVO.setState(PublicIpv6Address.State.Allocated);
ipVO.setDomainId(owner.getDomainId());
ipVO.setAccountId(owner.getAccountId());
_ipv6Dao.persist(ipVO);
return ipVO;
}
@Override
public void revokeDirectIpv6Address(long networkId, String ip6Address) {
PublicIpv6AddressVO ip = _ipv6Dao.findByNetworkIdAndIp(networkId, ip6Address);
if (ip != null) {
_ipv6Dao.remove(ip.getId());
}
}
}

View File

@ -327,8 +327,4 @@ public interface NetworkManager {
int getRuleCountForIp(Long addressId, FirewallRule.Purpose purpose, FirewallRule.State state); int getRuleCountForIp(Long addressId, FirewallRule.Purpose purpose, FirewallRule.State state);
LoadBalancingServiceProvider getLoadBalancingProviderForNetwork(Network network); LoadBalancingServiceProvider getLoadBalancingProviderForNetwork(Network network);
PublicIpv6Address assignPublicIp6Address(long dcId, Long podId, Account owner,
VlanType type, Long networkId, String requestedIp6, boolean isSystem)
throws InsufficientAddressCapacityException;
} }

View File

@ -276,6 +276,8 @@ public class NetworkManagerImpl implements NetworkManager, Manager, Listener {
NetworkModel _networkModel; NetworkModel _networkModel;
@Inject @Inject
PublicIpv6AddressDao _ipv6Dao; PublicIpv6AddressDao _ipv6Dao;
@Inject
Ipv6AddressManager _ipv6Mgr;
ScheduledExecutorService _executor; ScheduledExecutorService _executor;
@ -297,57 +299,6 @@ public class NetworkManagerImpl implements NetworkManager, Manager, Listener {
return fetchNewPublicIp(dcId, podId, null, owner, type, networkId, false, true, requestedIp, isSystem, null); return fetchNewPublicIp(dcId, podId, null, owner, type, networkId, false, true, requestedIp, isSystem, null);
} }
@Override
public PublicIpv6Address assignPublicIp6Address(long dcId, Long podId, Account owner, VlanType type, Long networkId, String requestedIp6, boolean isSystem) throws InsufficientAddressCapacityException {
Vlan vlan = _networkModel.getVlanForNetwork(networkId);
if (vlan == null) {
s_logger.debug("Cannot find related vlan or too many vlan attached to network " + networkId);
return null;
}
//TODO should check before this point
if (!NetUtils.isIp6InRange(requestedIp6, vlan.getIp6Range())) {
throw new CloudRuntimeException("Requested IPv6 is not in the predefined range!");
}
String ip = null;
if (requestedIp6 == null) {
int count = 0;
while (ip == null || count >= 10) {
ip = NetUtils.getIp6FromRange(vlan.getIp6Range());
//Check for duplicate IP
if (_ipv6Dao.findByDcIdAndIp(dcId, ip) == null) {
break;
} else {
ip = null;
}
count ++;
}
if (ip == null) {
throw new CloudRuntimeException("Fail to get unique ipv6 address after 10 times trying!");
}
} else {
ip = requestedIp6;
//TODO should check before this point
if (_ipv6Dao.findByDcIdAndIp(dcId, ip) != null) {
throw new CloudRuntimeException("The requested IP is already taken!");
}
}
DataCenterVO dc = _dcDao.findById(dcId);
Long mac = dc.getMacAddress();
Long nextMac = mac + 1;
dc.setMacAddress(nextMac);
_dcDao.update(dc.getId(), dc);
String macAddress = NetUtils.long2Mac(NetUtils.createSequenceBasedMacAddress(mac));
PublicIpv6AddressVO ipVO = new PublicIpv6AddressVO(ip, dcId, macAddress, vlan.getId());
ipVO.setPhysicalNetworkId(vlan.getPhysicalNetworkId());
ipVO.setSourceNetworkId(vlan.getNetworkId());
ipVO.setState(PublicIpv6Address.State.Allocated);
ipVO.setDomainId(owner.getDomainId());
ipVO.setAccountId(owner.getAccountId());
_ipv6Dao.persist(ipVO);
return ipVO;
}
@DB @DB
public PublicIp fetchNewPublicIp(long dcId, Long podId, Long vlanDbId, Account owner, VlanType vlanUse, public PublicIp fetchNewPublicIp(long dcId, Long podId, Long vlanDbId, Account owner, VlanType vlanUse,
Long guestNetworkId, boolean sourceNat, boolean assign, String requestedIp, boolean isSystem, Long vpcId) Long guestNetworkId, boolean sourceNat, boolean assign, String requestedIp, boolean isSystem, Long vpcId)
@ -3411,7 +3362,7 @@ public class NetworkManagerImpl implements NetworkManager, Manager, Listener {
if (network.getIp6Gateway() != null) { if (network.getIp6Gateway() != null) {
if (nic.getIp6Address() == null) { if (nic.getIp6Address() == null) {
ipv6 = true; ipv6 = true;
PublicIpv6Address ip = assignPublicIp6Address(dc.getId(), null, vm.getOwner(), VlanType.DirectAttached, network.getId(), requestedIpv6, false); PublicIpv6Address ip = _ipv6Mgr.assignDirectIp6Address(dc.getId(), vm.getOwner(), network.getId(), requestedIpv6);
Vlan vlan = _networkModel.getVlanForNetwork(network.getId()); Vlan vlan = _networkModel.getVlanForNetwork(network.getId());
if (vlan == null) { if (vlan == null) {
s_logger.debug("Cannot find related vlan or too many vlan attached to network " + network.getId()); s_logger.debug("Cannot find related vlan or too many vlan attached to network " + network.getId());

View File

@ -15,7 +15,7 @@ public interface PublicIpv6AddressDao extends GenericDao<PublicIpv6AddressVO, Lo
List<PublicIpv6AddressVO> listByNetwork(long networkId); List<PublicIpv6AddressVO> listByNetwork(long networkId);
public PublicIpv6AddressVO findByDcIdAndIp(long dcId, String ipAddress); public PublicIpv6AddressVO findByNetworkIdAndIp(long networkId, String ipAddress);
List<PublicIpv6AddressVO> listByPhysicalNetworkId(long physicalNetworkId); List<PublicIpv6AddressVO> listByPhysicalNetworkId(long physicalNetworkId);

View File

@ -73,9 +73,9 @@ public class PublicIpv6AddressDaoImpl extends GenericDaoBase<PublicIpv6AddressVO
} }
@Override @Override
public PublicIpv6AddressVO findByDcIdAndIp(long dcId, String ipAddress) { public PublicIpv6AddressVO findByNetworkIdAndIp(long networkId, String ipAddress) {
SearchCriteria<PublicIpv6AddressVO> sc = AllFieldsSearch.create(); SearchCriteria<PublicIpv6AddressVO> sc = AllFieldsSearch.create();
sc.setParameters("dataCenterId", dcId); sc.setParameters("networkId", networkId);
sc.setParameters("ipAddress", ipAddress); sc.setParameters("ipAddress", ipAddress);
return findOneBy(sc); return findOneBy(sc);
} }

View File

@ -31,6 +31,7 @@ import com.cloud.exception.InsufficientAddressCapacityException;
import com.cloud.exception.InsufficientVirtualNetworkCapcityException; import com.cloud.exception.InsufficientVirtualNetworkCapcityException;
import com.cloud.exception.InvalidParameterValueException; import com.cloud.exception.InvalidParameterValueException;
import com.cloud.network.IPAddressVO; import com.cloud.network.IPAddressVO;
import com.cloud.network.Ipv6AddressManager;
import com.cloud.network.Network; import com.cloud.network.Network;
import com.cloud.network.Network.GuestType; import com.cloud.network.Network.GuestType;
import com.cloud.network.Network.Service; import com.cloud.network.Network.Service;
@ -76,6 +77,8 @@ public class DirectNetworkGuru extends AdapterBase implements NetworkGuru {
NetworkOfferingDao _networkOfferingDao; NetworkOfferingDao _networkOfferingDao;
@Inject @Inject
PublicIpv6AddressDao _ipv6Dao; PublicIpv6AddressDao _ipv6Dao;
@Inject
Ipv6AddressManager _ipv6Mgr;
private static final TrafficType[] _trafficTypes = {TrafficType.Guest}; private static final TrafficType[] _trafficTypes = {TrafficType.Guest};
@ -230,10 +233,7 @@ public class DirectNetworkGuru extends AdapterBase implements NetworkGuru {
} }
if (nic.getIp6Address() != null) { if (nic.getIp6Address() != null) {
PublicIpv6AddressVO ip = _ipv6Dao.findByDcIdAndIp(network.getDataCenterId(), nic.getIp6Address()); _ipv6Mgr.revokeDirectIpv6Address(nic.getNetworkId(), nic.getIp6Address());
if (ip != null) {
_ipv6Dao.remove(ip.getId());
}
} }
nic.deallocate(); nic.deallocate();
} }

View File

@ -818,12 +818,4 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
// TODO Auto-generated method stub // TODO Auto-generated method stub
return null; return null;
} }
@Override
public PublicIpv6Address assignPublicIp6Address(long dcId, Long podId,
Account owner, VlanType type, Long networkId, String requestedIpv6,
boolean isSystem) throws InsufficientAddressCapacityException {
// TODO Auto-generated method stub
return null;
}
} }

View File

@ -1321,12 +1321,4 @@ public class MockNetworkManagerImpl implements NetworkManager, NetworkService, M
// TODO Auto-generated method stub // TODO Auto-generated method stub
return null; return null;
} }
@Override
public PublicIpv6Address assignPublicIp6Address(long dcId, Long podId,
Account owner, VlanType type, Long networkId, String requestedIpv6,
boolean isSystem) throws InsufficientAddressCapacityException {
// TODO Auto-generated method stub
return null;
}
} }