5c8780028dd0a5ce9cfa16aa5148ee952137aae2
[controller.git] / opendaylight / networkconfiguration / neutron / implementation / src / main / java / org / opendaylight / controller / networkconfig / neutron / implementation / NeutronPortInterface.java
1 /*
2  * Copyright IBM Corporation, 2013.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.networkconfig.neutron.implementation;
10
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;
17 import java.util.Set;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.ConcurrentMap;
20
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;
31
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>();
35
36
37
38     // this method uses reflection to update an object from it's delta.
39
40     private boolean overwrite(Object target, Object delta) {
41         Method[] methods = target.getClass().getMethods();
42
43         for(Method toMethod: methods){
44             if(toMethod.getDeclaringClass().equals(target.getClass())
45                     && toMethod.getName().startsWith("set")){
46
47                 String toName = toMethod.getName();
48                 String fromName = toName.replace("set", "get");
49
50                 try {
51                     Method fromMethod = delta.getClass().getMethod(fromName);
52                     Object value = fromMethod.invoke(delta, (Object[])null);
53                     if(value != null){
54                         toMethod.invoke(target, value);
55                     }
56                 } catch (Exception e) {
57                     e.printStackTrace();
58                     return false;
59                 }
60             }
61         }
62         return true;
63     }
64
65     // IfNBPortCRUD methods
66
67     @Override
68     public boolean portExists(String uuid) {
69         return portDB.containsKey(uuid);
70     }
71
72     @Override
73     public NeutronPort getPort(String uuid) {
74         if (!portExists(uuid)) {
75             return null;
76         }
77         return portDB.get(uuid);
78     }
79
80     @Override
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();
85             allPorts.add(port);
86         }
87         logger.debug("Exiting getAllPorts, Found {} OpenStackPorts", allPorts.size());
88         List<NeutronPort> ans = new ArrayList<NeutronPort>();
89         ans.addAll(allPorts);
90         return ans;
91     }
92
93     @Override
94     public boolean addPort(NeutronPort input) {
95         if (portExists(input.getID())) {
96             return false;
97         }
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>());
103         }
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()));
111                 }
112             }
113         }
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());
120             }
121             if (!ip.getIpAddress().equals(subnet.getGatewayIP())) {
122                 subnet.allocateIP(ip.getIpAddress());
123             }
124             else {
125                 subnet.setGatewayIPAllocated();
126             }
127             subnet.addPort(input);
128         }
129         INeutronNetworkCRUD networkIf = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);
130
131         NeutronNetwork network = networkIf.getNetwork(input.getNetworkUUID());
132         network.addPort(input);
133         return true;
134     }
135
136     @Override
137     public boolean removePort(String uuid) {
138         if (!portExists(uuid)) {
139             return false;
140         }
141         NeutronPort port = getPort(uuid);
142         portDB.remove(uuid);
143         INeutronNetworkCRUD networkCRUD = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);
144         INeutronSubnetCRUD systemCRUD = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
145
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());
154             }
155             else {
156                 subnet.resetGatewayIPAllocated();
157             }
158             subnet.removePort(port);
159         }
160         return true;
161     }
162
163     @Override
164     public boolean updatePort(String uuid, NeutronPort delta) {
165         if (!portExists(uuid)) {
166             return false;
167         }
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());
176             }
177
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());
183                 }
184                 subnet.allocateIP(ip.getIpAddress());
185             }
186         }
187         return overwrite(target, delta);
188     }
189
190     @Override
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())) {
197                 return true;
198             }
199         }
200         return false;
201     }
202
203     @Override
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())) {
213                     return port;
214                 }
215             }
216         }
217         return null;
218     }
219 }