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.neutron.transcriber;
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.neutron.spi.INeutronNetworkCRUD;
21 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
22 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
23 import org.opendaylight.neutron.spi.NeutronNetwork;
24 import org.opendaylight.neutron.spi.NeutronSubnet;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public class NeutronSubnetInterface implements INeutronSubnetCRUD {
29 private static final Logger logger = LoggerFactory.getLogger(NeutronSubnetInterface.class);
30 private ConcurrentMap<String, NeutronSubnet> subnetDB = new ConcurrentHashMap<String, NeutronSubnet>();
34 // this method uses reflection to update an object from it's delta.
36 private boolean overwrite(Object target, Object delta) {
37 Method[] methods = target.getClass().getMethods();
39 for(Method toMethod: methods){
40 if(toMethod.getDeclaringClass().equals(target.getClass())
41 && toMethod.getName().startsWith("set")){
43 String toName = toMethod.getName();
44 String fromName = toName.replace("set", "get");
47 Method fromMethod = delta.getClass().getMethod(fromName);
48 Object value = fromMethod.invoke(delta, (Object[])null);
50 toMethod.invoke(target, value);
52 } catch (Exception e) {
62 // IfNBSubnetCRUD methods
65 public boolean subnetExists(String uuid) {
66 return subnetDB.containsKey(uuid);
70 public NeutronSubnet getSubnet(String uuid) {
71 if (!subnetExists(uuid)) {
74 return subnetDB.get(uuid);
78 public List<NeutronSubnet> getAllSubnets() {
79 Set<NeutronSubnet> allSubnets = new HashSet<NeutronSubnet>();
80 for (Entry<String, NeutronSubnet> entry : subnetDB.entrySet()) {
81 NeutronSubnet subnet = entry.getValue();
82 allSubnets.add(subnet);
84 logger.debug("Exiting getAllSubnets, Found {} OpenStackSubnets", allSubnets.size());
85 List<NeutronSubnet> ans = new ArrayList<NeutronSubnet>();
86 ans.addAll(allSubnets);
91 public boolean addSubnet(NeutronSubnet input) {
92 String id = input.getID();
93 if (subnetExists(id)) {
96 subnetDB.putIfAbsent(id, input);
97 INeutronNetworkCRUD networkIf = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);
99 NeutronNetwork targetNet = networkIf.getNetwork(input.getNetworkUUID());
100 targetNet.addSubnet(id);
105 public boolean removeSubnet(String uuid) {
106 if (!subnetExists(uuid)) {
109 NeutronSubnet target = subnetDB.get(uuid);
110 INeutronNetworkCRUD networkIf = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);
112 NeutronNetwork targetNet = networkIf.getNetwork(target.getNetworkUUID());
113 targetNet.removeSubnet(uuid);
114 subnetDB.remove(uuid);
119 public boolean updateSubnet(String uuid, NeutronSubnet delta) {
120 if (!subnetExists(uuid)) {
123 NeutronSubnet target = subnetDB.get(uuid);
124 return overwrite(target, delta);
128 public boolean subnetInUse(String subnetUUID) {
129 if (!subnetExists(subnetUUID)) {
132 NeutronSubnet target = subnetDB.get(subnetUUID);
133 return (target.getPortsInSubnet().size() > 0);