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.List;
20 import java.util.Map.Entry;
22 import java.util.concurrent.ConcurrentMap;
24 import org.apache.felix.dm.Component;
25 import org.opendaylight.controller.clustering.services.CacheConfigException;
26 import org.opendaylight.controller.clustering.services.CacheExistException;
27 import org.opendaylight.controller.clustering.services.IClusterContainerServices;
28 import org.opendaylight.controller.clustering.services.IClusterServices;
29 import org.opendaylight.controller.configuration.ConfigurationObject;
30 import org.opendaylight.controller.configuration.IConfigurationContainerAware;
31 import org.opendaylight.controller.configuration.IConfigurationContainerService;
32 import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkCRUD;
33 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
34 import org.opendaylight.controller.sal.utils.IObjectReader;
35 import org.opendaylight.controller.sal.utils.Status;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public class NeutronNetworkInterface implements INeutronNetworkCRUD, IConfigurationContainerAware,
41 private static final Logger logger = LoggerFactory.getLogger(NeutronNetworkInterface.class);
42 private static final String FILE_NAME ="neutron.network.conf";
43 private String containerName = null;
45 private ConcurrentMap<String, NeutronNetwork> networkDB;
46 private IClusterContainerServices clusterContainerService = null;
47 private IConfigurationContainerService configurationService;
49 // methods needed for creating caches
51 void setClusterContainerService(IClusterContainerServices s) {
52 logger.debug("Cluster Service set");
53 this.clusterContainerService = s;
56 void unsetClusterContainerService(IClusterContainerServices s) {
57 if (this.clusterContainerService == s) {
58 logger.debug("Cluster Service removed!");
59 this.clusterContainerService = null;
63 public void setConfigurationContainerService(IConfigurationContainerService service) {
64 logger.trace("Configuration service set: {}", service);
65 this.configurationService = service;
68 public void unsetConfigurationContainerService(IConfigurationContainerService service) {
69 logger.trace("Configuration service removed: {}", service);
70 this.configurationService = null;
73 private void allocateCache() {
74 if (this.clusterContainerService == null) {
75 logger.error("un-initialized clusterContainerService, can't create cache");
78 logger.debug("Creating Cache for Neutron Networks");
81 this.clusterContainerService.createCache("neutronNetworks",
82 EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));
83 } catch (CacheConfigException cce) {
84 logger.error("Cache couldn't be created for Neutron Networks - check cache mode");
85 } catch (CacheExistException cce) {
86 logger.error("Cache for Neutron Networks already exists, destroy and recreate");
88 logger.debug("Cache successfully created for Neutron Networks");
91 @SuppressWarnings({ "unchecked" })
92 private void retrieveCache() {
93 if (this.clusterContainerService == null) {
94 logger.error("un-initialized clusterContainerService, can't retrieve cache");
97 logger.debug("Retrieving cache for Neutron Networks");
98 networkDB = (ConcurrentMap<String, NeutronNetwork>) this.clusterContainerService.getCache("neutronNetworks");
99 if (networkDB == null) {
100 logger.error("Cache couldn't be retrieved for Neutron Networks");
102 logger.debug("Cache was successfully retrieved for Neutron Networks");
105 private void startUp() {
112 * Function called by the dependency manager when all the required
113 * dependencies are satisfied
116 void init(Component c) {
117 Dictionary<?, ?> props = c.getServiceProperties();
119 this.containerName = (String) props.get("containerName");
120 logger.debug("Running containerName: {}", this.containerName);
122 // In the Global instance case the containerName is empty
123 this.containerName = "";
128 private void destroyCache() {
129 if (this.clusterContainerService == null) {
130 logger.error("un-initialized clusterMger, can't destroy cache");
133 logger.debug("Destroying Cache for Neutron Networks");
134 this.clusterContainerService.destroyCache("Neutron Networks");
138 * Function called by the dependency manager when at least one dependency
139 * become unsatisfied or when the component is shutting down because for
140 * example bundle is being stopped.
148 * Function called by dependency manager after "init ()" is called and after
149 * the services provided by the class are registered in the service registry
156 * Function called by the dependency manager before the services exported by
157 * the component are unregistered, this will be followed by a "destroy ()"
164 // this method uses reflection to update an object from it's delta.
166 private boolean overwrite(Object target, Object delta) {
167 Method[] methods = target.getClass().getMethods();
169 for(Method toMethod: methods){
170 if(toMethod.getDeclaringClass().equals(target.getClass())
171 && toMethod.getName().startsWith("set")){
173 String toName = toMethod.getName();
174 String fromName = toName.replace("set", "get");
177 Method fromMethod = delta.getClass().getMethod(fromName);
178 Object value = fromMethod.invoke(delta, (Object[])null);
180 toMethod.invoke(target, value);
182 } catch (Exception e) {
191 // IfNBNetworkCRUD methods
194 public boolean networkExists(String uuid) {
195 return networkDB.containsKey(uuid);
199 public NeutronNetwork getNetwork(String uuid) {
200 if (!networkExists(uuid)) {
203 return networkDB.get(uuid);
207 public List<NeutronNetwork> getAllNetworks() {
208 Set<NeutronNetwork> allNetworks = new HashSet<NeutronNetwork>();
209 for (Entry<String, NeutronNetwork> entry : networkDB.entrySet()) {
210 NeutronNetwork network = entry.getValue();
211 allNetworks.add(network);
213 logger.debug("Exiting getAllNetworks, Found {} OpenStackNetworks", allNetworks.size());
214 List<NeutronNetwork> ans = new ArrayList<NeutronNetwork>();
215 ans.addAll(allNetworks);
220 public boolean addNetwork(NeutronNetwork input) {
221 if (networkExists(input.getID())) {
224 networkDB.putIfAbsent(input.getID(), input);
225 //TODO: add code to find INeutronNetworkAware services and call newtorkCreated on them
230 public boolean removeNetwork(String uuid) {
231 if (!networkExists(uuid)) {
234 networkDB.remove(uuid);
235 //TODO: add code to find INeutronNetworkAware services and call newtorkDeleted on them
240 public boolean updateNetwork(String uuid, NeutronNetwork delta) {
241 if (!networkExists(uuid)) {
244 NeutronNetwork target = networkDB.get(uuid);
245 return overwrite(target, delta);
249 public boolean networkInUse(String netUUID) {
250 if (!networkExists(netUUID)) {
253 NeutronNetwork target = networkDB.get(netUUID);
254 if (target.getPortsOnNetwork().size() > 0) {
260 private void loadConfiguration() {
261 for (ConfigurationObject conf : configurationService.retrieveConfiguration(this, FILE_NAME)) {
262 NeutronNetwork nn = (NeutronNetwork) conf;
263 networkDB.put(nn.getID(), nn);
268 public Status saveConfiguration() {
269 return configurationService.persistConfiguration(new ArrayList<ConfigurationObject>(networkDB.values()),
274 public Object readObject(ObjectInputStream ois) throws FileNotFoundException, IOException, ClassNotFoundException {
275 return ois.readObject();