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.List;
\r
17 import java.util.Set;
\r
18 import java.util.Map.Entry;
\r
19 import java.util.concurrent.ConcurrentMap;
\r
21 import org.apache.felix.dm.Component;
\r
22 import org.opendaylight.controller.clustering.services.CacheConfigException;
\r
23 import org.opendaylight.controller.clustering.services.CacheExistException;
\r
24 import org.opendaylight.controller.clustering.services.IClusterContainerServices;
\r
25 import org.opendaylight.controller.clustering.services.IClusterServices;
\r
26 import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkCRUD;
\r
27 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
\r
28 import org.slf4j.Logger;
\r
29 import org.slf4j.LoggerFactory;
\r
31 public class NeutronNetworkInterface implements INeutronNetworkCRUD {
\r
32 private static final Logger logger = LoggerFactory.getLogger(NeutronNetworkInterface.class);
\r
33 private String containerName = null;
\r
35 private ConcurrentMap<String, NeutronNetwork> networkDB;
\r
36 private IClusterContainerServices clusterContainerService = null;
\r
38 // methods needed for creating caches
\r
40 void setClusterContainerService(IClusterContainerServices s) {
\r
41 logger.debug("Cluster Service set");
\r
42 this.clusterContainerService = s;
\r
45 void unsetClusterContainerService(IClusterContainerServices s) {
\r
46 if (this.clusterContainerService == s) {
\r
47 logger.debug("Cluster Service removed!");
\r
48 this.clusterContainerService = null;
\r
52 @SuppressWarnings("deprecation")
\r
53 private void allocateCache() {
\r
54 if (this.clusterContainerService == null) {
\r
55 logger.error("un-initialized clusterContainerService, can't create cache");
\r
58 logger.debug("Creating Cache for Neutron Networks");
\r
61 this.clusterContainerService.createCache("neutronNetworks",
\r
62 EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));
\r
63 } catch (CacheConfigException cce) {
\r
64 logger.error("Cache couldn't be created for Neutron Networks - check cache mode");
\r
65 } catch (CacheExistException cce) {
\r
66 logger.error("Cache for Neutron Networks already exists, destroy and recreate");
\r
68 logger.debug("Cache successfully created for Neutron Networks");
\r
71 @SuppressWarnings({ "unchecked", "deprecation" })
\r
72 private void retrieveCache() {
\r
73 if (this.clusterContainerService == null) {
\r
74 logger.error("un-initialized clusterContainerService, can't retrieve cache");
\r
77 logger.debug("Retrieving cache for Neutron Networks");
\r
78 networkDB = (ConcurrentMap<String, NeutronNetwork>) this.clusterContainerService.getCache("neutronNetworks");
\r
79 if (networkDB == null) {
\r
80 logger.error("Cache couldn't be retrieved for Neutron Networks");
\r
82 logger.debug("Cache was successfully retrieved for Neutron Networks");
\r
85 private void startUp() {
\r
91 * Function called by the dependency manager when all the required
\r
92 * dependencies are satisfied
\r
95 void init(Component c) {
\r
96 Dictionary<?, ?> props = c.getServiceProperties();
\r
97 if (props != null) {
\r
98 this.containerName = (String) props.get("containerName");
\r
99 logger.debug("Running containerName: {}", this.containerName);
\r
101 // In the Global instance case the containerName is empty
\r
102 this.containerName = "";
\r
107 @SuppressWarnings("deprecation")
\r
108 private void destroyCache() {
\r
109 if (this.clusterContainerService == null) {
\r
110 logger.error("un-initialized clusterMger, can't destroy cache");
\r
113 logger.debug("Destroying Cache for Neutron Networks");
\r
114 this.clusterContainerService.destroyCache("Neutron Networks");
\r
118 * Function called by the dependency manager when at least one dependency
\r
119 * become unsatisfied or when the component is shutting down because for
\r
120 * example bundle is being stopped.
\r
128 * Function called by dependency manager after "init ()" is called and after
\r
129 * the services provided by the class are registered in the service registry
\r
136 * Function called by the dependency manager before the services exported by
\r
137 * the component are unregistered, this will be followed by a "destroy ()"
\r
144 // this method uses reflection to update an object from it's delta.
\r
146 private boolean overwrite(Object target, Object delta) {
\r
147 Method[] methods = target.getClass().getMethods();
\r
149 for(Method toMethod: methods){
\r
150 if(toMethod.getDeclaringClass().equals(target.getClass())
\r
151 && toMethod.getName().startsWith("set")){
\r
153 String toName = toMethod.getName();
\r
154 String fromName = toName.replace("set", "get");
\r
157 Method fromMethod = delta.getClass().getMethod(fromName);
\r
158 Object value = fromMethod.invoke(delta, (Object[])null);
\r
160 toMethod.invoke(target, value);
\r
162 } catch (Exception e) {
\r
163 e.printStackTrace();
\r
171 // IfNBNetworkCRUD methods
\r
173 public boolean networkExists(String uuid) {
\r
174 return networkDB.containsKey(uuid);
\r
177 public NeutronNetwork getNetwork(String uuid) {
\r
178 if (!networkExists(uuid))
\r
180 return networkDB.get(uuid);
\r
183 public List<NeutronNetwork> getAllNetworks() {
\r
184 Set<NeutronNetwork> allNetworks = new HashSet<NeutronNetwork>();
\r
185 for (Entry<String, NeutronNetwork> entry : networkDB.entrySet()) {
\r
186 NeutronNetwork network = entry.getValue();
\r
187 allNetworks.add(network);
\r
189 logger.debug("Exiting getAllNetworks, Found {} OpenStackNetworks", allNetworks.size());
\r
190 List<NeutronNetwork> ans = new ArrayList<NeutronNetwork>();
\r
191 ans.addAll(allNetworks);
\r
195 public boolean addNetwork(NeutronNetwork input) {
\r
196 if (networkExists(input.getID()))
\r
198 networkDB.putIfAbsent(input.getID(), input);
\r
199 //TODO: add code to find INeutronNetworkAware services and call newtorkCreated on them
\r
203 public boolean removeNetwork(String uuid) {
\r
204 if (!networkExists(uuid))
\r
206 networkDB.remove(uuid);
\r
207 //TODO: add code to find INeutronNetworkAware services and call newtorkDeleted on them
\r
211 public boolean updateNetwork(String uuid, NeutronNetwork delta) {
\r
212 if (!networkExists(uuid))
\r
214 NeutronNetwork target = networkDB.get(uuid);
\r
215 return overwrite(target, delta);
\r
218 public boolean networkInUse(String netUUID) {
\r
219 if (!networkExists(netUUID))
\r
221 NeutronNetwork target = networkDB.get(netUUID);
\r
222 if (target.getPortsOnNetwork().size() > 0)
\r