2 * Copyright IBM Corporation, 2015. All rights reserved.
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
9 package org.opendaylight.neutron.transcriber;
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;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
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;
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>();
32 NeutronMeteringLabelRuleInterface(ProviderContext providerContext) {
33 super(providerContext);
36 // this method uses reflection to update an object from it's delta.
38 private boolean overwrite(Object target, Object delta) {
39 Method[] methods = target.getClass().getMethods();
41 for(Method toMethod: methods){
42 if(toMethod.getDeclaringClass().equals(target.getClass())
43 && toMethod.getName().startsWith("set")){
45 String toName = toMethod.getName();
46 String fromName = toName.replace("set", "get");
49 Method fromMethod = delta.getClass().getMethod(fromName);
50 Object value = fromMethod.invoke(delta, (Object[])null);
52 toMethod.invoke(target, value);
54 } catch (Exception e) {
63 // IfNBMeteringLabelRuleCRUD methods
66 public boolean neutronMeteringLabelRuleExists(String uuid) {
67 return meteringLabelRuleDB.containsKey(uuid);
71 public NeutronMeteringLabelRule getNeutronMeteringLabelRule(String uuid) {
72 if (!neutronMeteringLabelRuleExists(uuid)) {
75 return meteringLabelRuleDB.get(uuid);
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);
85 logger.debug("Exiting getAllMeteringLabelRules, Found {} OpenStackMeteringLabelRules", allMeteringLabelRules.size());
86 List<NeutronMeteringLabelRule> ans = new ArrayList<NeutronMeteringLabelRule>();
87 ans.addAll(allMeteringLabelRules);
92 public boolean addNeutronMeteringLabelRule(NeutronMeteringLabelRule input) {
93 if (neutronMeteringLabelRuleExists(input.getMeteringLabelRuleUUID())) {
96 meteringLabelRuleDB.putIfAbsent(input.getMeteringLabelRuleUUID(), input);
97 //TODO: add code to find INeutronMeteringLabelRuleAware services and call newtorkCreated on them
102 public boolean removeNeutronMeteringLabelRule(String uuid) {
103 if (!neutronMeteringLabelRuleExists(uuid)) {
106 meteringLabelRuleDB.remove(uuid);
107 //TODO: add code to find INeutronMeteringLabelRuleAware services and call newtorkDeleted on them
112 public boolean updateNeutronMeteringLabelRule(String uuid, NeutronMeteringLabelRule delta) {
113 if (!neutronMeteringLabelRuleExists(uuid)) {
116 NeutronMeteringLabelRule target = meteringLabelRuleDB.get(uuid);
117 return overwrite(target, delta);
121 public boolean neutronMeteringLabelRuleInUse(String netUUID) {
122 if (!neutronMeteringLabelRuleExists(netUUID)) {