2 * Copyright IBM Corporation, 2013. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.networkconfig.neutron.implementation;
11 import java.lang.reflect.Method;
12 import java.util.ArrayList;
13 import java.util.HashSet;
14 import java.util.Iterator;
15 import java.util.List;
16 import java.util.Map.Entry;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.ConcurrentMap;
21 import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkCRUD;
22 import org.opendaylight.controller.networkconfig.neutron.INeutronPortCRUD;
23 import org.opendaylight.controller.networkconfig.neutron.INeutronSubnetCRUD;
24 import org.opendaylight.controller.networkconfig.neutron.NeutronCRUDInterfaces;
25 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
26 import org.opendaylight.controller.networkconfig.neutron.NeutronPort;
27 import org.opendaylight.controller.networkconfig.neutron.NeutronSubnet;
28 import org.opendaylight.controller.networkconfig.neutron.Neutron_IPs;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class NeutronPortInterface implements INeutronPortCRUD {
33 private static final Logger logger = LoggerFactory.getLogger(NeutronPortInterface.class);
34 private ConcurrentMap<String, NeutronPort> portDB = new ConcurrentHashMap<String, NeutronPort>();
38 // this method uses reflection to update an object from it's delta.
40 private boolean overwrite(Object target, Object delta) {
41 Method[] methods = target.getClass().getMethods();
43 for(Method toMethod: methods){
44 if(toMethod.getDeclaringClass().equals(target.getClass())
45 && toMethod.getName().startsWith("set")){
47 String toName = toMethod.getName();
48 String fromName = toName.replace("set", "get");
51 Method fromMethod = delta.getClass().getMethod(fromName);
52 Object value = fromMethod.invoke(delta, (Object[])null);
54 toMethod.invoke(target, value);
56 } catch (Exception e) {
65 // IfNBPortCRUD methods
68 public boolean portExists(String uuid) {
69 return portDB.containsKey(uuid);
73 public NeutronPort getPort(String uuid) {
74 if (!portExists(uuid)) {
77 return portDB.get(uuid);
81 public List<NeutronPort> getAllPorts() {
82 Set<NeutronPort> allPorts = new HashSet<NeutronPort>();
83 for (Entry<String, NeutronPort> entry : portDB.entrySet()) {
84 NeutronPort port = entry.getValue();
87 logger.debug("Exiting getAllPorts, Found {} OpenStackPorts", allPorts.size());
88 List<NeutronPort> ans = new ArrayList<NeutronPort>();
94 public boolean addPort(NeutronPort input) {
95 if (portExists(input.getID())) {
98 portDB.putIfAbsent(input.getID(), input);
99 // if there are no fixed IPs, allocate one for each subnet in the network
100 INeutronSubnetCRUD systemCRUD = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
101 if (input.getFixedIPs() == null){
102 input.setFixedIPs(new ArrayList<Neutron_IPs>());
104 if (input.getFixedIPs().size() == 0) {
105 List<Neutron_IPs> list = input.getFixedIPs();
106 Iterator<NeutronSubnet> subnetIterator = systemCRUD.getAllSubnets().iterator();
107 while (subnetIterator.hasNext()) {
108 NeutronSubnet subnet = subnetIterator.next();
109 if (subnet.getNetworkUUID().equals(input.getNetworkUUID())) {
110 list.add(new Neutron_IPs(subnet.getID()));
114 Iterator<Neutron_IPs> fixedIPIterator = input.getFixedIPs().iterator();
115 while (fixedIPIterator.hasNext()) {
116 Neutron_IPs ip = fixedIPIterator.next();
117 NeutronSubnet subnet = systemCRUD.getSubnet(ip.getSubnetUUID());
118 if (ip.getIpAddress() == null) {
119 ip.setIpAddress(subnet.getLowAddr());
121 if (!ip.getIpAddress().equals(subnet.getGatewayIP())) {
122 subnet.allocateIP(ip.getIpAddress());
125 subnet.setGatewayIPAllocated();
127 subnet.addPort(input);
129 INeutronNetworkCRUD networkIf = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);
131 NeutronNetwork network = networkIf.getNetwork(input.getNetworkUUID());
132 network.addPort(input);
137 public boolean removePort(String uuid) {
138 if (!portExists(uuid)) {
141 NeutronPort port = getPort(uuid);
143 INeutronNetworkCRUD networkCRUD = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);
144 INeutronSubnetCRUD systemCRUD = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
146 NeutronNetwork network = networkCRUD.getNetwork(port.getNetworkUUID());
147 network.removePort(port);
148 Iterator<Neutron_IPs> fixedIPIterator = port.getFixedIPs().iterator();
149 while (fixedIPIterator.hasNext()) {
150 Neutron_IPs ip = fixedIPIterator.next();
151 NeutronSubnet subnet = systemCRUD.getSubnet(ip.getSubnetUUID());
152 if (!ip.getIpAddress().equals(subnet.getGatewayIP())) {
153 subnet.releaseIP(ip.getIpAddress());
156 subnet.resetGatewayIPAllocated();
158 subnet.removePort(port);
164 public boolean updatePort(String uuid, NeutronPort delta) {
165 if (!portExists(uuid)) {
168 NeutronPort target = portDB.get(uuid);
169 // remove old Fixed_IPs
170 if (delta.getFixedIPs() != null) {
171 NeutronPort port = getPort(uuid);
172 INeutronSubnetCRUD systemCRUD = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
173 for (Neutron_IPs ip: port.getFixedIPs()) {
174 NeutronSubnet subnet = systemCRUD.getSubnet(ip.getSubnetUUID());
175 subnet.releaseIP(ip.getIpAddress());
178 // allocate new Fixed_IPs
179 for (Neutron_IPs ip: delta.getFixedIPs()) {
180 NeutronSubnet subnet = systemCRUD.getSubnet(ip.getSubnetUUID());
181 if (ip.getIpAddress() == null) {
182 ip.setIpAddress(subnet.getLowAddr());
184 subnet.allocateIP(ip.getIpAddress());
187 return overwrite(target, delta);
191 public boolean macInUse(String macAddress) {
192 List<NeutronPort> ports = getAllPorts();
193 Iterator<NeutronPort> portIterator = ports.iterator();
194 while (portIterator.hasNext()) {
195 NeutronPort port = portIterator.next();
196 if (macAddress.equalsIgnoreCase(port.getMacAddress())) {
204 public NeutronPort getGatewayPort(String subnetUUID) {
205 INeutronSubnetCRUD systemCRUD = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
206 NeutronSubnet subnet = systemCRUD.getSubnet(subnetUUID);
207 Iterator<NeutronPort> portIterator = getAllPorts().iterator();
208 while (portIterator.hasNext()) {
209 NeutronPort port = portIterator.next();
210 List<Neutron_IPs> fixedIPs = port.getFixedIPs();
211 if (fixedIPs.size() == 1) {
212 if (subnet.getGatewayIP().equals(fixedIPs.get(0).getIpAddress())) {