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.HashSet;
14 import java.util.List;
15 import java.util.Map.Entry;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
20 import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkCRUD;
21 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 public class NeutronNetworkInterface implements INeutronNetworkCRUD {
26 private static final Logger logger = LoggerFactory.getLogger(NeutronNetworkInterface.class);
27 private ConcurrentMap<String, NeutronNetwork> networkDB = new ConcurrentHashMap<String, NeutronNetwork>();
32 // this method uses reflection to update an object from it's delta.
34 private boolean overwrite(Object target, Object delta) {
35 Method[] methods = target.getClass().getMethods();
37 for(Method toMethod: methods){
38 if(toMethod.getDeclaringClass().equals(target.getClass())
39 && toMethod.getName().startsWith("set")){
41 String toName = toMethod.getName();
42 String fromName = toName.replace("set", "get");
45 Method fromMethod = delta.getClass().getMethod(fromName);
46 Object value = fromMethod.invoke(delta, (Object[])null);
48 toMethod.invoke(target, value);
50 } catch (Exception e) {
59 // IfNBNetworkCRUD methods
62 public boolean networkExists(String uuid) {
63 return networkDB.containsKey(uuid);
67 public NeutronNetwork getNetwork(String uuid) {
68 if (!networkExists(uuid)) {
71 return networkDB.get(uuid);
75 public List<NeutronNetwork> getAllNetworks() {
76 Set<NeutronNetwork> allNetworks = new HashSet<NeutronNetwork>();
77 for (Entry<String, NeutronNetwork> entry : networkDB.entrySet()) {
78 NeutronNetwork network = entry.getValue();
79 allNetworks.add(network);
81 logger.debug("Exiting getAllNetworks, Found {} OpenStackNetworks", allNetworks.size());
82 List<NeutronNetwork> ans = new ArrayList<NeutronNetwork>();
83 ans.addAll(allNetworks);
88 public boolean addNetwork(NeutronNetwork input) {
89 if (networkExists(input.getID())) {
92 networkDB.putIfAbsent(input.getID(), input);
93 //TODO: add code to find INeutronNetworkAware services and call newtorkCreated on them
98 public boolean removeNetwork(String uuid) {
99 if (!networkExists(uuid)) {
102 networkDB.remove(uuid);
103 //TODO: add code to find INeutronNetworkAware services and call newtorkDeleted on them
108 public boolean updateNetwork(String uuid, NeutronNetwork delta) {
109 if (!networkExists(uuid)) {
112 NeutronNetwork target = networkDB.get(uuid);
113 return overwrite(target, delta);
117 public boolean networkInUse(String netUUID) {
118 if (!networkExists(netUUID)) {
121 NeutronNetwork target = networkDB.get(netUUID);
122 if (target.getPortsOnNetwork().size() > 0) {