2 * Copyright IBM Corporation, 2013. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
9 package org.opendaylight.controller.networkconfig.neutron.implementation;
\r
11 import java.lang.reflect.Method;
\r
12 import java.util.ArrayList;
\r
13 import java.util.Dictionary;
\r
14 import java.util.EnumSet;
\r
15 import java.util.HashSet;
\r
16 import java.util.Iterator;
\r
17 import java.util.List;
\r
18 import java.util.Set;
\r
19 import java.util.Map.Entry;
\r
20 import java.util.concurrent.ConcurrentMap;
\r
22 import org.apache.felix.dm.Component;
\r
23 import org.opendaylight.controller.clustering.services.CacheConfigException;
\r
24 import org.opendaylight.controller.clustering.services.CacheExistException;
\r
25 import org.opendaylight.controller.clustering.services.IClusterContainerServices;
\r
26 import org.opendaylight.controller.clustering.services.IClusterServices;
\r
27 import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkCRUD;
\r
28 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
\r
29 import org.slf4j.Logger;
\r
30 import org.slf4j.LoggerFactory;
\r
32 public class NeutronNetworkInterface implements INeutronNetworkCRUD {
\r
33 private static final Logger logger = LoggerFactory.getLogger(NeutronNetworkInterface.class);
\r
34 private String containerName = null;
\r
36 private ConcurrentMap<String, NeutronNetwork> networkDB;
\r
37 private IClusterContainerServices clusterContainerService = null;
\r
39 // methods needed for creating caches
\r
41 void setClusterContainerService(IClusterContainerServices s) {
\r
42 logger.debug("Cluster Service set");
\r
43 this.clusterContainerService = s;
\r
46 void unsetClusterContainerService(IClusterContainerServices s) {
\r
47 if (this.clusterContainerService == s) {
\r
48 logger.debug("Cluster Service removed!");
\r
49 this.clusterContainerService = null;
\r
53 @SuppressWarnings("deprecation")
\r
54 private void allocateCache() {
\r
55 if (this.clusterContainerService == null) {
\r
56 logger.error("un-initialized clusterContainerService, can't create cache");
\r
59 logger.debug("Creating Cache for Neutron Networks");
\r
62 this.clusterContainerService.createCache("neutronNetworks",
\r
63 EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));
\r
64 } catch (CacheConfigException cce) {
\r
65 logger.error("Cache couldn't be created for Neutron Networks - check cache mode");
\r
66 } catch (CacheExistException cce) {
\r
67 logger.error("Cache for Neutron Networks already exists, destroy and recreate");
\r
69 logger.debug("Cache successfully created for Neutron Networks");
\r
72 @SuppressWarnings({ "unchecked", "deprecation" })
\r
73 private void retrieveCache() {
\r
74 if (this.clusterContainerService == null) {
\r
75 logger.error("un-initialized clusterContainerService, can't retrieve cache");
\r
78 logger.debug("Retrieving cache for Neutron Networks");
\r
79 networkDB = (ConcurrentMap<String, NeutronNetwork>) this.clusterContainerService.getCache("neutronNetworks");
\r
80 if (networkDB == null) {
\r
81 logger.error("Cache couldn't be retrieved for Neutron Networks");
\r
83 logger.debug("Cache was successfully retrieved for Neutron Networks");
\r
86 private void startUp() {
\r
92 * Function called by the dependency manager when all the required
\r
93 * dependencies are satisfied
\r
96 void init(Component c) {
\r
97 Dictionary<?, ?> props = c.getServiceProperties();
\r
98 if (props != null) {
\r
99 this.containerName = (String) props.get("containerName");
\r
100 logger.debug("Running containerName: {}", this.containerName);
\r
102 // In the Global instance case the containerName is empty
\r
103 this.containerName = "";
\r
108 @SuppressWarnings("deprecation")
\r
109 private void destroyCache() {
\r
110 if (this.clusterContainerService == null) {
\r
111 logger.error("un-initialized clusterMger, can't destroy cache");
\r
114 logger.debug("Destroying Cache for Neutron Networks");
\r
115 this.clusterContainerService.destroyCache("Neutron Networks");
\r
119 * Function called by the dependency manager when at least one dependency
\r
120 * become unsatisfied or when the component is shutting down because for
\r
121 * example bundle is being stopped.
\r
129 * Function called by dependency manager after "init ()" is called and after
\r
130 * the services provided by the class are registered in the service registry
\r
137 * Function called by the dependency manager before the services exported by
\r
138 * the component are unregistered, this will be followed by a "destroy ()"
\r
145 // this method uses reflection to update an object from it's delta.
\r
147 private boolean overwrite(Object target, Object delta) {
\r
148 Method[] methods = target.getClass().getMethods();
\r
150 for(Method toMethod: methods){
\r
151 if(toMethod.getDeclaringClass().equals(target.getClass())
\r
152 && toMethod.getName().startsWith("set")){
\r
154 String toName = toMethod.getName();
\r
155 String fromName = toName.replace("set", "get");
\r
158 Method fromMethod = delta.getClass().getMethod(fromName);
\r
159 Object value = fromMethod.invoke(delta, (Object[])null);
\r
161 toMethod.invoke(target, value);
\r
163 } catch (Exception e) {
\r
164 e.printStackTrace();
\r
172 // IfNBNetworkCRUD methods
\r
174 public boolean networkExists(String uuid) {
\r
175 return networkDB.containsKey(uuid);
\r
178 public NeutronNetwork getNetwork(String uuid) {
\r
179 if (!networkExists(uuid))
\r
181 return networkDB.get(uuid);
\r
184 public List<NeutronNetwork> getAllNetworks() {
\r
185 Set<NeutronNetwork> allNetworks = new HashSet<NeutronNetwork>();
\r
186 for (Entry<String, NeutronNetwork> entry : networkDB.entrySet()) {
\r
187 NeutronNetwork network = entry.getValue();
\r
188 allNetworks.add(network);
\r
190 logger.debug("Exiting getAllNetworks, Found {} OpenStackNetworks", allNetworks.size());
\r
191 List<NeutronNetwork> ans = new ArrayList<NeutronNetwork>();
\r
192 ans.addAll(allNetworks);
\r
196 public boolean addNetwork(NeutronNetwork input) {
\r
197 if (networkExists(input.getID()))
\r
199 networkDB.putIfAbsent(input.getID(), input);
\r
200 //TODO: add code to find INeutronNetworkAware services and call newtorkCreated on them
\r
204 public boolean removeNetwork(String uuid) {
\r
205 if (!networkExists(uuid))
\r
207 networkDB.remove(uuid);
\r
208 //TODO: add code to find INeutronNetworkAware services and call newtorkDeleted on them
\r
212 public boolean updateNetwork(String uuid, NeutronNetwork delta) {
\r
213 if (!networkExists(uuid))
\r
215 NeutronNetwork target = networkDB.get(uuid);
\r
216 return overwrite(target, delta);
\r
219 public boolean networkInUse(String netUUID) {
\r
220 if (!networkExists(netUUID))
\r
222 NeutronNetwork target = networkDB.get(netUUID);
\r
223 if (target.getPortsOnNetwork().size() > 0)
\r