Add md-transcribing for HealthMonitors, and add *key "id"* to list healthmonitors...
[neutron.git] / transcriber / src / main / java / org / opendaylight / neutron / transcriber / NeutronMeteringLabelRuleInterface.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.INeutronMeteringLabelRuleCRUD;
22 import org.opendaylight.neutron.spi.NeutronMeteringLabelRule;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class NeutronMeteringLabelRuleInterface extends AbstractNeutronInterface implements INeutronMeteringLabelRuleCRUD {
29     private static final Logger logger = LoggerFactory.getLogger(NeutronMeteringLabelRuleInterface.class);
30     private ConcurrentMap<String, NeutronMeteringLabelRule> meteringLabelRuleDB = new ConcurrentHashMap<String, NeutronMeteringLabelRule>();
31
32
33
34     NeutronMeteringLabelRuleInterface(ProviderContext providerContext) {
35         super(providerContext);
36     }
37
38     // this method uses reflection to update an object from it's delta.
39
40     private boolean overwrite(Object target, Object delta) {
41         Method[] methods = target.getClass().getMethods();
42
43         for(Method toMethod: methods){
44             if(toMethod.getDeclaringClass().equals(target.getClass())
45                     && toMethod.getName().startsWith("set")){
46
47                 String toName = toMethod.getName();
48                 String fromName = toName.replace("set", "get");
49
50                 try {
51                     Method fromMethod = delta.getClass().getMethod(fromName);
52                     Object value = fromMethod.invoke(delta, (Object[])null);
53                     if(value != null){
54                         toMethod.invoke(target, value);
55                     }
56                 } catch (Exception e) {
57                     e.printStackTrace();
58                     return false;
59                 }
60             }
61         }
62         return true;
63     }
64
65     // IfNBMeteringLabelRuleCRUD methods
66
67     @Override
68     public boolean neutronMeteringLabelRuleExists(String uuid) {
69         return meteringLabelRuleDB.containsKey(uuid);
70     }
71
72     @Override
73     public NeutronMeteringLabelRule getNeutronMeteringLabelRule(String uuid) {
74         if (!neutronMeteringLabelRuleExists(uuid)) {
75             return null;
76         }
77         return meteringLabelRuleDB.get(uuid);
78     }
79
80     @Override
81     public List<NeutronMeteringLabelRule> getAllNeutronMeteringLabelRules() {
82         Set<NeutronMeteringLabelRule> allMeteringLabelRules = new HashSet<NeutronMeteringLabelRule>();
83         for (Entry<String, NeutronMeteringLabelRule> entry : meteringLabelRuleDB.entrySet()) {
84             NeutronMeteringLabelRule meteringLabelRule = entry.getValue();
85             allMeteringLabelRules.add(meteringLabelRule);
86         }
87         logger.debug("Exiting getAllMeteringLabelRules, Found {} OpenStackMeteringLabelRules", allMeteringLabelRules.size());
88         List<NeutronMeteringLabelRule> ans = new ArrayList<NeutronMeteringLabelRule>();
89         ans.addAll(allMeteringLabelRules);
90         return ans;
91     }
92
93     @Override
94     public boolean addNeutronMeteringLabelRule(NeutronMeteringLabelRule input) {
95         if (neutronMeteringLabelRuleExists(input.getMeteringLabelRuleUUID())) {
96             return false;
97         }
98         meteringLabelRuleDB.putIfAbsent(input.getMeteringLabelRuleUUID(), input);
99       //TODO: add code to find INeutronMeteringLabelRuleAware services and call newtorkCreated on them
100         return true;
101     }
102
103     @Override
104     public boolean removeNeutronMeteringLabelRule(String uuid) {
105         if (!neutronMeteringLabelRuleExists(uuid)) {
106             return false;
107         }
108         meteringLabelRuleDB.remove(uuid);
109       //TODO: add code to find INeutronMeteringLabelRuleAware services and call newtorkDeleted on them
110         return true;
111     }
112
113     @Override
114     public boolean updateNeutronMeteringLabelRule(String uuid, NeutronMeteringLabelRule delta) {
115         if (!neutronMeteringLabelRuleExists(uuid)) {
116             return false;
117         }
118         NeutronMeteringLabelRule target = meteringLabelRuleDB.get(uuid);
119         return overwrite(target, delta);
120     }
121
122     @Override
123     public boolean neutronMeteringLabelRuleInUse(String netUUID) {
124         if (!neutronMeteringLabelRuleExists(netUUID)) {
125             return true;
126         }
127         return false;
128     }
129
130     @Override
131     protected InstanceIdentifier createInstanceIdentifier(DataObject item) {
132         // TODO Auto-generated method stub
133         return null;
134     }
135
136     @Override
137     protected DataObject toMd(Object neutronObject) {
138         // TODO Auto-generated method stub
139         return null;
140     }
141
142     @Override
143     protected DataObject toMd(String uuid) {
144         // TODO Auto-generated method stub
145         return null;
146     }
147 }