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;
21 import java.util.Map.Entry;
22 import java.util.concurrent.ConcurrentHashMap;
23 import java.util.concurrent.ConcurrentMap;
25 import org.apache.felix.dm.Component;
26 import org.opendaylight.controller.clustering.services.CacheConfigException;
27 import org.opendaylight.controller.clustering.services.CacheExistException;
28 import org.opendaylight.controller.clustering.services.IClusterContainerServices;
29 import org.opendaylight.controller.clustering.services.IClusterServices;
30 import org.opendaylight.controller.configuration.IConfigurationContainerAware;
31 import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkCRUD;
32 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
33 import org.opendaylight.controller.sal.utils.GlobalConstants;
34 import org.opendaylight.controller.sal.utils.IObjectReader;
35 import org.opendaylight.controller.sal.utils.ObjectReader;
36 import org.opendaylight.controller.sal.utils.ObjectWriter;
37 import org.opendaylight.controller.sal.utils.Status;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class NeutronNetworkInterface implements INeutronNetworkCRUD, IConfigurationContainerAware,
43 private static final Logger logger = LoggerFactory.getLogger(NeutronNetworkInterface.class);
44 private static String ROOT = GlobalConstants.STARTUPHOME.toString();
45 private static final String FILENAME ="neutron.network";
46 private static String fileName;
47 private String containerName = null;
49 private ConcurrentMap<String, NeutronNetwork> networkDB;
50 private IClusterContainerServices clusterContainerService = null;
52 // methods needed for creating caches
54 void setClusterContainerService(IClusterContainerServices s) {
55 logger.debug("Cluster Service set");
56 this.clusterContainerService = s;
59 void unsetClusterContainerService(IClusterContainerServices s) {
60 if (this.clusterContainerService == s) {
61 logger.debug("Cluster Service removed!");
62 this.clusterContainerService = null;
66 @SuppressWarnings("deprecation")
67 private void allocateCache() {
68 if (this.clusterContainerService == null) {
69 logger.error("un-initialized clusterContainerService, can't create cache");
72 logger.debug("Creating Cache for Neutron Networks");
75 this.clusterContainerService.createCache("neutronNetworks",
76 EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));
77 } catch (CacheConfigException cce) {
78 logger.error("Cache couldn't be created for Neutron Networks - check cache mode");
79 } catch (CacheExistException cce) {
80 logger.error("Cache for Neutron Networks already exists, destroy and recreate");
82 logger.debug("Cache successfully created for Neutron Networks");
85 @SuppressWarnings({ "unchecked", "deprecation" })
86 private void retrieveCache() {
87 if (this.clusterContainerService == null) {
88 logger.error("un-initialized clusterContainerService, can't retrieve cache");
91 logger.debug("Retrieving cache for Neutron Networks");
92 networkDB = (ConcurrentMap<String, NeutronNetwork>) this.clusterContainerService.getCache("neutronNetworks");
93 if (networkDB == null) {
94 logger.error("Cache couldn't be retrieved for Neutron Networks");
96 logger.debug("Cache was successfully retrieved for Neutron Networks");
99 private void startUp() {
102 if (networkDB.isEmpty()) {
108 * Function called by the dependency manager when all the required
109 * dependencies are satisfied
112 void init(Component c) {
113 Dictionary<?, ?> props = c.getServiceProperties();
115 this.containerName = (String) props.get("containerName");
116 logger.debug("Running containerName: {}", this.containerName);
118 // In the Global instance case the containerName is empty
119 this.containerName = "";
121 fileName = ROOT + FILENAME + "_" + containerName + ".conf";
125 @SuppressWarnings("deprecation")
126 private void destroyCache() {
127 if (this.clusterContainerService == null) {
128 logger.error("un-initialized clusterMger, can't destroy cache");
131 logger.debug("Destroying Cache for Neutron Networks");
132 this.clusterContainerService.destroyCache("Neutron Networks");
136 * Function called by the dependency manager when at least one dependency
137 * become unsatisfied or when the component is shutting down because for
138 * example bundle is being stopped.
146 * Function called by dependency manager after "init ()" is called and after
147 * the services provided by the class are registered in the service registry
154 * Function called by the dependency manager before the services exported by
155 * the component are unregistered, this will be followed by a "destroy ()"
162 // this method uses reflection to update an object from it's delta.
164 private boolean overwrite(Object target, Object delta) {
165 Method[] methods = target.getClass().getMethods();
167 for(Method toMethod: methods){
168 if(toMethod.getDeclaringClass().equals(target.getClass())
169 && toMethod.getName().startsWith("set")){
171 String toName = toMethod.getName();
172 String fromName = toName.replace("set", "get");
175 Method fromMethod = delta.getClass().getMethod(fromName);
176 Object value = fromMethod.invoke(delta, (Object[])null);
178 toMethod.invoke(target, value);
180 } catch (Exception e) {
189 // IfNBNetworkCRUD methods
192 public boolean networkExists(String uuid) {
193 return networkDB.containsKey(uuid);
197 public NeutronNetwork getNetwork(String uuid) {
198 if (!networkExists(uuid))
200 return networkDB.get(uuid);
204 public List<NeutronNetwork> getAllNetworks() {
205 Set<NeutronNetwork> allNetworks = new HashSet<NeutronNetwork>();
206 for (Entry<String, NeutronNetwork> entry : networkDB.entrySet()) {
207 NeutronNetwork network = entry.getValue();
208 allNetworks.add(network);
210 logger.debug("Exiting getAllNetworks, Found {} OpenStackNetworks", allNetworks.size());
211 List<NeutronNetwork> ans = new ArrayList<NeutronNetwork>();
212 ans.addAll(allNetworks);
217 public boolean addNetwork(NeutronNetwork input) {
218 if (networkExists(input.getID()))
220 networkDB.putIfAbsent(input.getID(), input);
221 //TODO: add code to find INeutronNetworkAware services and call newtorkCreated on them
226 public boolean removeNetwork(String uuid) {
227 if (!networkExists(uuid))
229 networkDB.remove(uuid);
230 //TODO: add code to find INeutronNetworkAware services and call newtorkDeleted on them
235 public boolean updateNetwork(String uuid, NeutronNetwork delta) {
236 if (!networkExists(uuid))
238 NeutronNetwork target = networkDB.get(uuid);
239 return overwrite(target, delta);
243 public boolean networkInUse(String netUUID) {
244 if (!networkExists(netUUID))
246 NeutronNetwork target = networkDB.get(netUUID);
247 if (target.getPortsOnNetwork().size() > 0)
252 @SuppressWarnings("unchecked")
253 private void loadConfiguration() {
254 ObjectReader objReader = new ObjectReader();
255 ConcurrentMap<String, NeutronNetwork> confList = (ConcurrentMap<String, NeutronNetwork>)
256 objReader.read(this, fileName);
258 if (confList == null) {
262 for (String key : confList.keySet()) {
263 networkDB.put(key, confList.get(key));
268 public Status saveConfiguration() {
269 ObjectWriter objWriter = new ObjectWriter();
270 return objWriter.write(new ConcurrentHashMap<String, NeutronNetwork>(networkDB), fileName);
274 public Object readObject(ObjectInputStream ois) throws FileNotFoundException, IOException, ClassNotFoundException {
275 return ois.readObject();