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