Adding implementation for NeutronVPNServicesNorthbound
[neutron.git] / transcriber / src / main / java / org / opendaylight / neutron / transcriber / NeutronMeteringLabelInterface.java
1 /*
2  * Copyright IBM Corporation, 2015.  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.neutron.transcriber;
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.neutron.spi.INeutronMeteringLabelCRUD;
21 import org.opendaylight.neutron.spi.NeutronMeteringLabel;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class NeutronMeteringLabelInterface implements INeutronMeteringLabelCRUD {
26     private static final Logger logger = LoggerFactory.getLogger(NeutronMeteringLabelInterface.class);
27     private ConcurrentMap<String, NeutronMeteringLabel> meteringLabelDB = new ConcurrentHashMap<String, NeutronMeteringLabel>();
28
29
30
31
32     // this method uses reflection to update an object from it's delta.
33
34     private boolean overwrite(Object target, Object delta) {
35         Method[] methods = target.getClass().getMethods();
36
37         for(Method toMethod: methods){
38             if(toMethod.getDeclaringClass().equals(target.getClass())
39                     && toMethod.getName().startsWith("set")){
40
41                 String toName = toMethod.getName();
42                 String fromName = toName.replace("set", "get");
43
44                 try {
45                     Method fromMethod = delta.getClass().getMethod(fromName);
46                     Object value = fromMethod.invoke(delta, (Object[])null);
47                     if(value != null){
48                         toMethod.invoke(target, value);
49                     }
50                 } catch (Exception e) {
51                     e.printStackTrace();
52                     return false;
53                 }
54             }
55         }
56         return true;
57     }
58
59     // IfNBMeteringLabelCRUD methods
60
61     @Override
62     public boolean neutronMeteringLabelExists(String uuid) {
63         return meteringLabelDB.containsKey(uuid);
64     }
65
66     @Override
67     public NeutronMeteringLabel getNeutronMeteringLabel(String uuid) {
68         if (!neutronMeteringLabelExists(uuid)) {
69             return null;
70         }
71         return meteringLabelDB.get(uuid);
72     }
73
74     @Override
75     public List<NeutronMeteringLabel> getAllNeutronMeteringLabels() {
76         Set<NeutronMeteringLabel> allMeteringLabels = new HashSet<NeutronMeteringLabel>();
77         for (Entry<String, NeutronMeteringLabel> entry : meteringLabelDB.entrySet()) {
78             NeutronMeteringLabel meteringLabel = entry.getValue();
79             allMeteringLabels.add(meteringLabel);
80         }
81         logger.debug("Exiting getAllMeteringLabels, Found {} OpenStackMeteringLabels", allMeteringLabels.size());
82         List<NeutronMeteringLabel> ans = new ArrayList<NeutronMeteringLabel>();
83         ans.addAll(allMeteringLabels);
84         return ans;
85     }
86
87     @Override
88     public boolean addNeutronMeteringLabel(NeutronMeteringLabel input) {
89         if (neutronMeteringLabelExists(input.getMeteringLabelUUID())) {
90             return false;
91         }
92         meteringLabelDB.putIfAbsent(input.getMeteringLabelUUID(), input);
93       //TODO: add code to find INeutronMeteringLabelAware services and call newtorkCreated on them
94         return true;
95     }
96
97     @Override
98     public boolean removeNeutronMeteringLabel(String uuid) {
99         if (!neutronMeteringLabelExists(uuid)) {
100             return false;
101         }
102         meteringLabelDB.remove(uuid);
103       //TODO: add code to find INeutronMeteringLabelAware services and call newtorkDeleted on them
104         return true;
105     }
106
107     @Override
108     public boolean updateNeutronMeteringLabel(String uuid, NeutronMeteringLabel delta) {
109         if (!neutronMeteringLabelExists(uuid)) {
110             return false;
111         }
112         NeutronMeteringLabel target = meteringLabelDB.get(uuid);
113         return overwrite(target, delta);
114     }
115
116     @Override
117     public boolean neutronMeteringLabelInUse(String netUUID) {
118         if (!neutronMeteringLabelExists(netUUID)) {
119             return true;
120         }
121         return false;
122     }
123 }