|
| 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 | +} |
0 commit comments