Prep work for transcriber writing yang model:
[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.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
21 import org.opendaylight.neutron.spi.INeutronMeteringLabelCRUD;
22 import org.opendaylight.neutron.spi.NeutronMeteringLabel;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class NeutronMeteringLabelInterface extends AbstractNeutronInterface implements INeutronMeteringLabelCRUD {
27     private static final Logger logger = LoggerFactory.getLogger(NeutronMeteringLabelInterface.class);
28     private ConcurrentMap<String, NeutronMeteringLabel> meteringLabelDB = new ConcurrentHashMap<String, NeutronMeteringLabel>();
29
30     NeutronMeteringLabelInterface(ProviderContext providerContext) {
31         super(providerContext);
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     // IfNBMeteringLabelCRUD methods
62
63     @Override
64     public boolean neutronMeteringLabelExists(String uuid) {
65         return meteringLabelDB.containsKey(uuid);
66     }
67
68     @Override
69     public NeutronMeteringLabel getNeutronMeteringLabel(String uuid) {
70         if (!neutronMeteringLabelExists(uuid)) {
71             return null;
72         }
73         return meteringLabelDB.get(uuid);
74     }
75
76     @Override
77     public List<NeutronMeteringLabel> getAllNeutronMeteringLabels() {
78         Set<NeutronMeteringLabel> allMeteringLabels = new HashSet<NeutronMeteringLabel>();
79         for (Entry<String, NeutronMeteringLabel> entry : meteringLabelDB.entrySet()) {
80             NeutronMeteringLabel meteringLabel = entry.getValue();
81             allMeteringLabels.add(meteringLabel);
82         }
83         logger.debug("Exiting getAllMeteringLabels, Found {} OpenStackMeteringLabels", allMeteringLabels.size());
84         List<NeutronMeteringLabel> ans = new ArrayList<NeutronMeteringLabel>();
85         ans.addAll(allMeteringLabels);
86         return ans;
87     }
88
89     @Override
90     public boolean addNeutronMeteringLabel(NeutronMeteringLabel input) {
91         if (neutronMeteringLabelExists(input.getMeteringLabelUUID())) {
92             return false;
93         }
94         meteringLabelDB.putIfAbsent(input.getMeteringLabelUUID(), input);
95       //TODO: add code to find INeutronMeteringLabelAware services and call newtorkCreated on them
96         return true;
97     }
98
99     @Override
100     public boolean removeNeutronMeteringLabel(String uuid) {
101         if (!neutronMeteringLabelExists(uuid)) {
102             return false;
103         }
104         meteringLabelDB.remove(uuid);
105       //TODO: add code to find INeutronMeteringLabelAware services and call newtorkDeleted on them
106         return true;
107     }
108
109     @Override
110     public boolean updateNeutronMeteringLabel(String uuid, NeutronMeteringLabel delta) {
111         if (!neutronMeteringLabelExists(uuid)) {
112             return false;
113         }
114         NeutronMeteringLabel target = meteringLabelDB.get(uuid);
115         return overwrite(target, delta);
116     }
117
118     @Override
119     public boolean neutronMeteringLabelInUse(String netUUID) {
120         if (!neutronMeteringLabelExists(netUUID)) {
121             return true;
122         }
123         return false;
124     }
125 }