Merge "Removing { } from NormalizedNodeJsonBodyWriter"
[controller.git] / opendaylight / networkconfiguration / neutron / implementation / src / main / java / org / opendaylight / controller / networkconfig / neutron / implementation / NeutronSubnetInterface.java
1 /*
2  * Copyright IBM Corporation, 2013.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.networkconfig.neutron.implementation;
10
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;
16 import java.util.Set;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
19
20 import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkCRUD;
21 import org.opendaylight.controller.networkconfig.neutron.INeutronSubnetCRUD;
22 import org.opendaylight.controller.networkconfig.neutron.NeutronCRUDInterfaces;
23 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
24 import org.opendaylight.controller.networkconfig.neutron.NeutronSubnet;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
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>();
31
32
33
34     // this method uses reflection to update an object from it's delta.
35
36     private boolean overwrite(Object target, Object delta) {
37         Method[] methods = target.getClass().getMethods();
38
39         for(Method toMethod: methods){
40             if(toMethod.getDeclaringClass().equals(target.getClass())
41                     && toMethod.getName().startsWith("set")){
42
43                 String toName = toMethod.getName();
44                 String fromName = toName.replace("set", "get");
45
46                 try {
47                     Method fromMethod = delta.getClass().getMethod(fromName);
48                     Object value = fromMethod.invoke(delta, (Object[])null);
49                     if(value != null){
50                         toMethod.invoke(target, value);
51                     }
52                 } catch (Exception e) {
53                     e.printStackTrace();
54                     return false;
55                 }
56             }
57         }
58         return true;
59     }
60
61
62     // IfNBSubnetCRUD methods
63
64     @Override
65     public boolean subnetExists(String uuid) {
66         return subnetDB.containsKey(uuid);
67     }
68
69     @Override
70     public NeutronSubnet getSubnet(String uuid) {
71         if (!subnetExists(uuid)) {
72             return null;
73         }
74         return subnetDB.get(uuid);
75     }
76
77     @Override
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);
83         }
84         logger.debug("Exiting getAllSubnets, Found {} OpenStackSubnets", allSubnets.size());
85         List<NeutronSubnet> ans = new ArrayList<NeutronSubnet>();
86         ans.addAll(allSubnets);
87         return ans;
88     }
89
90     @Override
91     public boolean addSubnet(NeutronSubnet input) {
92         String id = input.getID();
93         if (subnetExists(id)) {
94             return false;
95         }
96         subnetDB.putIfAbsent(id, input);
97         INeutronNetworkCRUD networkIf = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);
98
99         NeutronNetwork targetNet = networkIf.getNetwork(input.getNetworkUUID());
100         targetNet.addSubnet(id);
101         return true;
102     }
103
104     @Override
105     public boolean removeSubnet(String uuid) {
106         if (!subnetExists(uuid)) {
107             return false;
108         }
109         NeutronSubnet target = subnetDB.get(uuid);
110         INeutronNetworkCRUD networkIf = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);
111
112         NeutronNetwork targetNet = networkIf.getNetwork(target.getNetworkUUID());
113         targetNet.removeSubnet(uuid);
114         subnetDB.remove(uuid);
115         return true;
116     }
117
118     @Override
119     public boolean updateSubnet(String uuid, NeutronSubnet delta) {
120         if (!subnetExists(uuid)) {
121             return false;
122         }
123         NeutronSubnet target = subnetDB.get(uuid);
124         return overwrite(target, delta);
125     }
126
127     @Override
128     public boolean subnetInUse(String subnetUUID) {
129         if (!subnetExists(subnetUUID)) {
130             return true;
131         }
132         NeutronSubnet target = subnetDB.get(subnetUUID);
133         return (target.getPortsInSubnet().size() > 0);
134     }
135 }