Skip to content

Commit cb9f2d0

Browse files
author
Sheng Yang
committed
IPv6: move assignPublicIp6Address() from NetworkManager to new Ipv6AddressManager
Don't want to involve IP address management code in NetworkManager.
1 parent 1f2147d commit cb9f2d0

10 files changed

Lines changed: 172 additions & 79 deletions

‎server/src/com/cloud/configuration/DefaultComponentLibrary.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
import com.cloud.maint.UpgradeManagerImpl;
9292
import com.cloud.maint.dao.AgentUpgradeDaoImpl;
9393
import com.cloud.network.ExternalLoadBalancerUsageManagerImpl;
94+
import com.cloud.network.Ipv6AddressManagerImpl;
9495
import com.cloud.network.NetworkManagerImpl;
9596
import com.cloud.network.NetworkModelImpl;
9697
import com.cloud.network.NetworkServiceImpl;
@@ -464,6 +465,7 @@ protected void populateManagers() {
464465
addManager("TaggedResourcesManager", TaggedResourceManagerImpl.class);
465466
addManager("Site2SiteVpnManager", Site2SiteVpnManagerImpl.class);
466467
addManager("QueryManager", QueryManagerImpl.class);
468+
addManager("Ipv6AddressManager", Ipv6AddressManagerImpl.class);
467469
}
468470

469471
@Override
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package com.cloud.network;
19+
20+
import com.cloud.exception.InsufficientAddressCapacityException;
21+
import com.cloud.user.Account;
22+
import com.cloud.utils.component.Manager;
23+
24+
public interface Ipv6AddressManager extends Manager {
25+
public PublicIpv6Address assignDirectIp6Address(long dcId, Account owner, Long networkId, String requestedIp6) throws InsufficientAddressCapacityException;
26+
27+
public void revokeDirectIpv6Address(long networkId, String ip6Address);
28+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package com.cloud.network;
19+
20+
import java.util.Map;
21+
22+
import javax.ejb.Local;
23+
import javax.naming.ConfigurationException;
24+
25+
import org.apache.log4j.Logger;
26+
27+
import com.cloud.dc.DataCenterVO;
28+
import com.cloud.dc.Vlan;
29+
import com.cloud.dc.dao.DataCenterDao;
30+
import com.cloud.dc.dao.VlanDao;
31+
import com.cloud.exception.InsufficientAddressCapacityException;
32+
import com.cloud.network.dao.PublicIpv6AddressDao;
33+
import com.cloud.user.Account;
34+
import com.cloud.utils.component.Inject;
35+
import com.cloud.utils.exception.CloudRuntimeException;
36+
import com.cloud.utils.net.NetUtils;
37+
38+
@Local(value = { Ipv6AddressManager.class } )
39+
public class Ipv6AddressManagerImpl implements Ipv6AddressManager {
40+
public static final Logger s_logger = Logger.getLogger(Ipv6AddressManagerImpl.class.getName());
41+
42+
String _name = null;
43+
44+
@Inject
45+
DataCenterDao _dcDao;
46+
@Inject
47+
VlanDao _vlanDao;
48+
@Inject
49+
NetworkModel _networkModel;
50+
@Inject
51+
PublicIpv6AddressDao _ipv6Dao;
52+
53+
@Override
54+
public boolean configure(String name, Map<String, Object> params)
55+
throws ConfigurationException {
56+
_name = name;
57+
return true;
58+
}
59+
60+
@Override
61+
public boolean start() {
62+
return true;
63+
}
64+
65+
@Override
66+
public boolean stop() {
67+
return true;
68+
}
69+
70+
@Override
71+
public String getName() {
72+
return _name;
73+
}
74+
75+
@Override
76+
public PublicIpv6Address assignDirectIp6Address(long dcId, Account owner, Long networkId, String requestedIp6)
77+
throws InsufficientAddressCapacityException {
78+
Vlan vlan = _networkModel.getVlanForNetwork(networkId);
79+
if (vlan == null) {
80+
s_logger.debug("Cannot find related vlan or too many vlan attached to network " + networkId);
81+
return null;
82+
}
83+
String ip = null;
84+
if (requestedIp6 == null) {
85+
int count = 0;
86+
while (ip == null || count >= 10) {
87+
ip = NetUtils.getIp6FromRange(vlan.getIp6Range());
88+
//Check for duplicate IP
89+
if (_ipv6Dao.findByNetworkIdAndIp(networkId, ip) == null) {
90+
break;
91+
} else {
92+
ip = null;
93+
}
94+
count ++;
95+
}
96+
if (ip == null) {
97+
throw new CloudRuntimeException("Fail to get unique ipv6 address after 10 times trying!");
98+
}
99+
} else {
100+
if (!NetUtils.isIp6InRange(requestedIp6, vlan.getIp6Range())) {
101+
throw new CloudRuntimeException("Requested IPv6 is not in the predefined range!");
102+
}
103+
ip = requestedIp6;
104+
if (_ipv6Dao.findByNetworkIdAndIp(networkId, ip) != null) {
105+
throw new CloudRuntimeException("The requested IP is already taken!");
106+
}
107+
}
108+
DataCenterVO dc = _dcDao.findById(dcId);
109+
Long mac = dc.getMacAddress();
110+
Long nextMac = mac + 1;
111+
dc.setMacAddress(nextMac);
112+
_dcDao.update(dc.getId(), dc);
113+
114+
String macAddress = NetUtils.long2Mac(NetUtils.createSequenceBasedMacAddress(mac));
115+
PublicIpv6AddressVO ipVO = new PublicIpv6AddressVO(ip, dcId, macAddress, vlan.getId());
116+
ipVO.setPhysicalNetworkId(vlan.getPhysicalNetworkId());
117+
ipVO.setSourceNetworkId(vlan.getNetworkId());
118+
ipVO.setState(PublicIpv6Address.State.Allocated);
119+
ipVO.setDomainId(owner.getDomainId());
120+
ipVO.setAccountId(owner.getAccountId());
121+
_ipv6Dao.persist(ipVO);
122+
return ipVO;
123+
}
124+
125+
@Override
126+
public void revokeDirectIpv6Address(long networkId, String ip6Address) {
127+
PublicIpv6AddressVO ip = _ipv6Dao.findByNetworkIdAndIp(networkId, ip6Address);
128+
if (ip != null) {
129+
_ipv6Dao.remove(ip.getId());
130+
}
131+
}
132+
}

‎server/src/com/cloud/network/NetworkManager.java‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,4 @@ Map<String, String> finalizeServicesAndProvidersForNetwork(NetworkOffering offer
327327
int getRuleCountForIp(Long addressId, FirewallRule.Purpose purpose, FirewallRule.State state);
328328

329329
LoadBalancingServiceProvider getLoadBalancingProviderForNetwork(Network network);
330-
331-
PublicIpv6Address assignPublicIp6Address(long dcId, Long podId, Account owner,
332-
VlanType type, Long networkId, String requestedIp6, boolean isSystem)
333-
throws InsufficientAddressCapacityException;
334330
}

‎server/src/com/cloud/network/NetworkManagerImpl.java‎

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ public class NetworkManagerImpl implements NetworkManager, Manager, Listener {
276276
NetworkModel _networkModel;
277277
@Inject
278278
PublicIpv6AddressDao _ipv6Dao;
279+
@Inject
280+
Ipv6AddressManager _ipv6Mgr;
279281

280282
ScheduledExecutorService _executor;
281283

@@ -297,57 +299,6 @@ public PublicIp assignPublicIpAddress(long dcId, Long podId, Account owner, Vlan
297299
return fetchNewPublicIp(dcId, podId, null, owner, type, networkId, false, true, requestedIp, isSystem, null);
298300
}
299301

300-
@Override
301-
public PublicIpv6Address assignPublicIp6Address(long dcId, Long podId, Account owner, VlanType type, Long networkId, String requestedIp6, boolean isSystem) throws InsufficientAddressCapacityException {
302-
Vlan vlan = _networkModel.getVlanForNetwork(networkId);
303-
if (vlan == null) {
304-
s_logger.debug("Cannot find related vlan or too many vlan attached to network " + networkId);
305-
return null;
306-
}
307-
//TODO should check before this point
308-
if (!NetUtils.isIp6InRange(requestedIp6, vlan.getIp6Range())) {
309-
throw new CloudRuntimeException("Requested IPv6 is not in the predefined range!");
310-
}
311-
String ip = null;
312-
if (requestedIp6 == null) {
313-
int count = 0;
314-
while (ip == null || count >= 10) {
315-
ip = NetUtils.getIp6FromRange(vlan.getIp6Range());
316-
//Check for duplicate IP
317-
if (_ipv6Dao.findByDcIdAndIp(dcId, ip) == null) {
318-
break;
319-
} else {
320-
ip = null;
321-
}
322-
count ++;
323-
}
324-
if (ip == null) {
325-
throw new CloudRuntimeException("Fail to get unique ipv6 address after 10 times trying!");
326-
}
327-
} else {
328-
ip = requestedIp6;
329-
//TODO should check before this point
330-
if (_ipv6Dao.findByDcIdAndIp(dcId, ip) != null) {
331-
throw new CloudRuntimeException("The requested IP is already taken!");
332-
}
333-
}
334-
DataCenterVO dc = _dcDao.findById(dcId);
335-
Long mac = dc.getMacAddress();
336-
Long nextMac = mac + 1;
337-
dc.setMacAddress(nextMac);
338-
_dcDao.update(dc.getId(), dc);
339-
340-
String macAddress = NetUtils.long2Mac(NetUtils.createSequenceBasedMacAddress(mac));
341-
PublicIpv6AddressVO ipVO = new PublicIpv6AddressVO(ip, dcId, macAddress, vlan.getId());
342-
ipVO.setPhysicalNetworkId(vlan.getPhysicalNetworkId());
343-
ipVO.setSourceNetworkId(vlan.getNetworkId());
344-
ipVO.setState(PublicIpv6Address.State.Allocated);
345-
ipVO.setDomainId(owner.getDomainId());
346-
ipVO.setAccountId(owner.getAccountId());
347-
_ipv6Dao.persist(ipVO);
348-
return ipVO;
349-
}
350-
351302
@DB
352303
public PublicIp fetchNewPublicIp(long dcId, Long podId, Long vlanDbId, Account owner, VlanType vlanUse,
353304
Long guestNetworkId, boolean sourceNat, boolean assign, String requestedIp, boolean isSystem, Long vpcId)
@@ -3411,7 +3362,7 @@ public void allocateDirectIp(NicProfile nic, DataCenter dc, VirtualMachineProfil
34113362
if (network.getIp6Gateway() != null) {
34123363
if (nic.getIp6Address() == null) {
34133364
ipv6 = true;
3414-
PublicIpv6Address ip = assignPublicIp6Address(dc.getId(), null, vm.getOwner(), VlanType.DirectAttached, network.getId(), requestedIpv6, false);
3365+
PublicIpv6Address ip = _ipv6Mgr.assignDirectIp6Address(dc.getId(), vm.getOwner(), network.getId(), requestedIpv6);
34153366
Vlan vlan = _networkModel.getVlanForNetwork(network.getId());
34163367
if (vlan == null) {
34173368
s_logger.debug("Cannot find related vlan or too many vlan attached to network " + network.getId());

‎server/src/com/cloud/network/dao/PublicIpv6AddressDao.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public interface PublicIpv6AddressDao extends GenericDao<PublicIpv6AddressVO, Lo
1515

1616
List<PublicIpv6AddressVO> listByNetwork(long networkId);
1717

18-
public PublicIpv6AddressVO findByDcIdAndIp(long dcId, String ipAddress);
18+
public PublicIpv6AddressVO findByNetworkIdAndIp(long networkId, String ipAddress);
1919

2020
List<PublicIpv6AddressVO> listByPhysicalNetworkId(long physicalNetworkId);
2121

‎server/src/com/cloud/network/dao/PublicIpv6AddressDaoImpl.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ public List<PublicIpv6AddressVO> listByNetwork(long networkId) {
7373
}
7474

7575
@Override
76-
public PublicIpv6AddressVO findByDcIdAndIp(long dcId, String ipAddress) {
76+
public PublicIpv6AddressVO findByNetworkIdAndIp(long networkId, String ipAddress) {
7777
SearchCriteria<PublicIpv6AddressVO> sc = AllFieldsSearch.create();
78-
sc.setParameters("dataCenterId", dcId);
78+
sc.setParameters("networkId", networkId);
7979
sc.setParameters("ipAddress", ipAddress);
8080
return findOneBy(sc);
8181
}

‎server/src/com/cloud/network/guru/DirectNetworkGuru.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.cloud.exception.InsufficientVirtualNetworkCapcityException;
3232
import com.cloud.exception.InvalidParameterValueException;
3333
import com.cloud.network.IPAddressVO;
34+
import com.cloud.network.Ipv6AddressManager;
3435
import com.cloud.network.Network;
3536
import com.cloud.network.Network.GuestType;
3637
import com.cloud.network.Network.Service;
@@ -76,6 +77,8 @@ public class DirectNetworkGuru extends AdapterBase implements NetworkGuru {
7677
NetworkOfferingDao _networkOfferingDao;
7778
@Inject
7879
PublicIpv6AddressDao _ipv6Dao;
80+
@Inject
81+
Ipv6AddressManager _ipv6Mgr;
7982

8083
private static final TrafficType[] _trafficTypes = {TrafficType.Guest};
8184

@@ -230,10 +233,7 @@ public void deallocate(Network network, NicProfile nic, VirtualMachineProfile<?
230233
}
231234

232235
if (nic.getIp6Address() != null) {
233-
PublicIpv6AddressVO ip = _ipv6Dao.findByDcIdAndIp(network.getDataCenterId(), nic.getIp6Address());
234-
if (ip != null) {
235-
_ipv6Dao.remove(ip.getId());
236-
}
236+
_ipv6Mgr.revokeDirectIpv6Address(nic.getNetworkId(), nic.getIp6Address());
237237
}
238238
nic.deallocate();
239239
}

‎server/test/com/cloud/network/MockNetworkManagerImpl.java‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -818,12 +818,4 @@ public IpAddress allocateIP(Account ipOwner, boolean isSystem, long zoneId) thro
818818
// TODO Auto-generated method stub
819819
return null;
820820
}
821-
822-
@Override
823-
public PublicIpv6Address assignPublicIp6Address(long dcId, Long podId,
824-
Account owner, VlanType type, Long networkId, String requestedIpv6,
825-
boolean isSystem) throws InsufficientAddressCapacityException {
826-
// TODO Auto-generated method stub
827-
return null;
828-
}
829821
}

‎server/test/com/cloud/vpc/MockNetworkManagerImpl.java‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,12 +1321,4 @@ public Network getNetwork(String networkUuid) {
13211321
// TODO Auto-generated method stub
13221322
return null;
13231323
}
1324-
1325-
@Override
1326-
public PublicIpv6Address assignPublicIp6Address(long dcId, Long podId,
1327-
Account owner, VlanType type, Long networkId, String requestedIpv6,
1328-
boolean isSystem) throws InsufficientAddressCapacityException {
1329-
// TODO Auto-generated method stub
1330-
return null;
1331-
}
13321324
}

0 commit comments

Comments
 (0)