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