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.io.FileNotFoundException;
12 import java.io.IOException;
13 import java.io.ObjectInputStream;
14 import java.lang.reflect.Method;
15 import java.util.ArrayList;
16 import java.util.Dictionary;
17 import java.util.EnumSet;
18 import java.util.HashSet;
19 import java.util.Iterator;
20 import java.util.List;
22 import java.util.Map.Entry;
23 import java.util.concurrent.ConcurrentHashMap;
24 import java.util.concurrent.ConcurrentMap;
26 import org.apache.felix.dm.Component;
27 import org.opendaylight.controller.clustering.services.CacheConfigException;
28 import org.opendaylight.controller.clustering.services.CacheExistException;
29 import org.opendaylight.controller.clustering.services.IClusterContainerServices;
30 import org.opendaylight.controller.clustering.services.IClusterServices;
31 import org.opendaylight.controller.configuration.IConfigurationContainerAware;
32 import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkCRUD;
33 import org.opendaylight.controller.networkconfig.neutron.INeutronPortCRUD;
34 import org.opendaylight.controller.networkconfig.neutron.INeutronSubnetCRUD;
35 import org.opendaylight.controller.networkconfig.neutron.NeutronCRUDInterfaces;
36 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
37 import org.opendaylight.controller.networkconfig.neutron.NeutronPort;
38 import org.opendaylight.controller.networkconfig.neutron.NeutronSubnet;
39 import org.opendaylight.controller.networkconfig.neutron.Neutron_IPs;
40 import org.opendaylight.controller.sal.utils.GlobalConstants;
41 import org.opendaylight.controller.sal.utils.IObjectReader;
42 import org.opendaylight.controller.sal.utils.ObjectReader;
43 import org.opendaylight.controller.sal.utils.ObjectWriter;
44 import org.opendaylight.controller.sal.utils.Status;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 public class NeutronPortInterface implements INeutronPortCRUD, IConfigurationContainerAware,
50 private static final Logger logger = LoggerFactory.getLogger(NeutronPortInterface.class);
51 private static String ROOT = GlobalConstants.STARTUPHOME.toString();
52 private static final String FILENAME ="neutron.port";
53 private static String fileName;
54 private String containerName = null;
56 private IClusterContainerServices clusterContainerService = null;
57 private ConcurrentMap<String, NeutronPort> portDB;
59 // methods needed for creating caches
61 void setClusterContainerService(IClusterContainerServices s) {
62 logger.debug("Cluster Service set");
63 clusterContainerService = s;
66 void unsetClusterContainerService(IClusterContainerServices s) {
67 if (clusterContainerService == s) {
68 logger.debug("Cluster Service removed!");
69 clusterContainerService = null;
73 @SuppressWarnings("deprecation")
74 private void allocateCache() {
75 if (clusterContainerService == null) {
76 logger.error("un-initialized clusterContainerService, can't create cache");
79 logger.debug("Creating Cache for OpenDOVE");
82 clusterContainerService.createCache("neutronPorts",
83 EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));
84 } catch (CacheConfigException cce) {
85 logger.error("Cache couldn't be created for OpenDOVE - check cache mode");
86 } catch (CacheExistException cce) {
87 logger.error("Cache for OpenDOVE already exists, destroy and recreate");
89 logger.debug("Cache successfully created for OpenDOVE");
92 @SuppressWarnings({ "unchecked", "deprecation" })
93 private void retrieveCache() {
94 if (clusterContainerService == null) {
95 logger.error("un-initialized clusterContainerService, can't retrieve cache");
99 logger.debug("Retrieving cache for Neutron Ports");
100 portDB = (ConcurrentMap<String, NeutronPort>) clusterContainerService
101 .getCache("neutronPorts");
102 if (portDB == null) {
103 logger.error("Cache couldn't be retrieved for Neutron Ports");
105 logger.debug("Cache was successfully retrieved for Neutron Ports");
108 @SuppressWarnings("deprecation")
109 private void destroyCache() {
110 if (clusterContainerService == null) {
111 logger.error("un-initialized clusterMger, can't destroy cache");
114 logger.debug("Destroying Cache for HostTracker");
115 clusterContainerService.destroyCache("neutronPorts");
118 private void startUp() {
121 if (portDB.isEmpty()) {
128 * Function called by the dependency manager when all the required
129 * dependencies are satisfied
132 void init(Component c) {
133 Dictionary<?, ?> props = c.getServiceProperties();
135 containerName = (String) props.get("containerName");
136 logger.debug("Running containerName: {}", containerName);
138 // In the Global instance case the containerName is empty
141 fileName = ROOT + FILENAME + "_" + containerName + ".conf";
146 * Function called by the dependency manager when at least one dependency
147 * become unsatisfied or when the component is shutting down because for
148 * example bundle is being stopped.
156 * Function called by dependency manager after "init ()" is called and after
157 * the services provided by the class are registered in the service registry
164 * Function called by the dependency manager before the services exported by
165 * the component are unregistered, this will be followed by a "destroy ()"
172 // this method uses reflection to update an object from it's delta.
174 private boolean overwrite(Object target, Object delta) {
175 Method[] methods = target.getClass().getMethods();
177 for(Method toMethod: methods){
178 if(toMethod.getDeclaringClass().equals(target.getClass())
179 && toMethod.getName().startsWith("set")){
181 String toName = toMethod.getName();
182 String fromName = toName.replace("set", "get");
185 Method fromMethod = delta.getClass().getMethod(fromName);
186 Object value = fromMethod.invoke(delta, (Object[])null);
188 toMethod.invoke(target, value);
190 } catch (Exception e) {
199 // IfNBPortCRUD methods
202 public boolean portExists(String uuid) {
203 return portDB.containsKey(uuid);
207 public NeutronPort getPort(String uuid) {
208 if (!portExists(uuid)) {
211 return portDB.get(uuid);
215 public List<NeutronPort> getAllPorts() {
216 Set<NeutronPort> allPorts = new HashSet<NeutronPort>();
217 for (Entry<String, NeutronPort> entry : portDB.entrySet()) {
218 NeutronPort port = entry.getValue();
221 logger.debug("Exiting getAllPorts, Found {} OpenStackPorts", allPorts.size());
222 List<NeutronPort> ans = new ArrayList<NeutronPort>();
223 ans.addAll(allPorts);
228 public boolean addPort(NeutronPort input) {
229 if (portExists(input.getID())) {
232 portDB.putIfAbsent(input.getID(), input);
233 // if there are no fixed IPs, allocate one for each subnet in the network
234 INeutronSubnetCRUD systemCRUD = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
235 if (input.getFixedIPs().size() == 0) {
236 List<Neutron_IPs> list = input.getFixedIPs();
237 Iterator<NeutronSubnet> subnetIterator = systemCRUD.getAllSubnets().iterator();
238 while (subnetIterator.hasNext()) {
239 NeutronSubnet subnet = subnetIterator.next();
240 if (subnet.getNetworkUUID().equals(input.getNetworkUUID())) {
241 list.add(new Neutron_IPs(subnet.getID()));
245 Iterator<Neutron_IPs> fixedIPIterator = input.getFixedIPs().iterator();
246 while (fixedIPIterator.hasNext()) {
247 Neutron_IPs ip = fixedIPIterator.next();
248 NeutronSubnet subnet = systemCRUD.getSubnet(ip.getSubnetUUID());
249 if (ip.getIpAddress() == null) {
250 ip.setIpAddress(subnet.getLowAddr());
252 if (!ip.getIpAddress().equals(subnet.getGatewayIP())) {
253 subnet.allocateIP(ip.getIpAddress());
256 subnet.setGatewayIPAllocated();
258 subnet.addPort(input);
260 INeutronNetworkCRUD networkIf = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);
262 NeutronNetwork network = networkIf.getNetwork(input.getNetworkUUID());
263 network.addPort(input);
268 public boolean removePort(String uuid) {
269 if (!portExists(uuid)) {
272 NeutronPort port = getPort(uuid);
274 INeutronNetworkCRUD networkCRUD = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);
275 INeutronSubnetCRUD systemCRUD = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
277 NeutronNetwork network = networkCRUD.getNetwork(port.getNetworkUUID());
278 network.removePort(port);
279 Iterator<Neutron_IPs> fixedIPIterator = port.getFixedIPs().iterator();
280 while (fixedIPIterator.hasNext()) {
281 Neutron_IPs ip = fixedIPIterator.next();
282 NeutronSubnet subnet = systemCRUD.getSubnet(ip.getSubnetUUID());
283 if (!ip.getIpAddress().equals(subnet.getGatewayIP())) {
284 subnet.releaseIP(ip.getIpAddress());
287 subnet.resetGatewayIPAllocated();
289 subnet.removePort(port);
295 public boolean updatePort(String uuid, NeutronPort delta) {
296 if (!portExists(uuid)) {
299 NeutronPort target = portDB.get(uuid);
300 // remove old Fixed_IPs
301 if (delta.getFixedIPs() != null) {
302 NeutronPort port = getPort(uuid);
303 INeutronSubnetCRUD systemCRUD = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
304 for (Neutron_IPs ip: port.getFixedIPs()) {
305 NeutronSubnet subnet = systemCRUD.getSubnet(ip.getSubnetUUID());
306 subnet.releaseIP(ip.getIpAddress());
309 // allocate new Fixed_IPs
310 for (Neutron_IPs ip: delta.getFixedIPs()) {
311 NeutronSubnet subnet = systemCRUD.getSubnet(ip.getSubnetUUID());
312 if (ip.getIpAddress() == null) {
313 ip.setIpAddress(subnet.getLowAddr());
315 subnet.allocateIP(ip.getIpAddress());
318 return overwrite(target, delta);
322 public boolean macInUse(String macAddress) {
323 List<NeutronPort> ports = getAllPorts();
324 Iterator<NeutronPort> portIterator = ports.iterator();
325 while (portIterator.hasNext()) {
326 NeutronPort port = portIterator.next();
327 if (macAddress.equalsIgnoreCase(port.getMacAddress())) {
335 public NeutronPort getGatewayPort(String subnetUUID) {
336 INeutronSubnetCRUD systemCRUD = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
337 NeutronSubnet subnet = systemCRUD.getSubnet(subnetUUID);
338 Iterator<NeutronPort> portIterator = getAllPorts().iterator();
339 while (portIterator.hasNext()) {
340 NeutronPort port = portIterator.next();
341 List<Neutron_IPs> fixedIPs = port.getFixedIPs();
342 if (fixedIPs.size() == 1) {
343 if (subnet.getGatewayIP().equals(fixedIPs.get(0).getIpAddress())) {
351 @SuppressWarnings("unchecked")
352 private void loadConfiguration() {
353 ObjectReader objReader = new ObjectReader();
354 ConcurrentMap<String, NeutronPort> confList = (ConcurrentMap<String, NeutronPort>)
355 objReader.read(this, fileName);
357 if (confList == null) {
361 for (String key : confList.keySet()) {
362 portDB.put(key, confList.get(key));
367 public Status saveConfiguration() {
368 ObjectWriter objWriter = new ObjectWriter();
369 return objWriter.write(new ConcurrentHashMap<String, NeutronPort>(portDB), fileName);
373 public Object readObject(ObjectInputStream ois) throws FileNotFoundException, IOException, ClassNotFoundException {
374 return ois.readObject();