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.neutron.spi.INeutronMeteringLabelRuleCRUD;
21 import org.opendaylight.neutron.spi.NeutronMeteringLabelRule;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 public class NeutronMeteringLabelRuleInterface implements INeutronMeteringLabelRuleCRUD {
26 private static final Logger logger = LoggerFactory.getLogger(NeutronMeteringLabelRuleInterface.class);
27 private ConcurrentMap<String, NeutronMeteringLabelRule> meteringLabelRuleDB = new ConcurrentHashMap<String, NeutronMeteringLabelRule>();
32 // this method uses reflection to update an object from it's delta.
34 private boolean overwrite(Object target, Object delta) {
35 Method[] methods = target.getClass().getMethods();
37 for(Method toMethod: methods){
38 if(toMethod.getDeclaringClass().equals(target.getClass())
39 && toMethod.getName().startsWith("set")){
41 String toName = toMethod.getName();
42 String fromName = toName.replace("set", "get");
45 Method fromMethod = delta.getClass().getMethod(fromName);
46 Object value = fromMethod.invoke(delta, (Object[])null);
48 toMethod.invoke(target, value);
50 } catch (Exception e) {
59 // IfNBMeteringLabelRuleCRUD methods
62 public boolean neutronMeteringLabelRuleExists(String uuid) {
63 return meteringLabelRuleDB.containsKey(uuid);
67 public NeutronMeteringLabelRule getNeutronMeteringLabelRule(String uuid) {
68 if (!neutronMeteringLabelRuleExists(uuid)) {
71 return meteringLabelRuleDB.get(uuid);
75 public List<NeutronMeteringLabelRule> getAllNeutronMeteringLabelRules() {
76 Set<NeutronMeteringLabelRule> allMeteringLabelRules = new HashSet<NeutronMeteringLabelRule>();
77 for (Entry<String, NeutronMeteringLabelRule> entry : meteringLabelRuleDB.entrySet()) {
78 NeutronMeteringLabelRule meteringLabelRule = entry.getValue();
79 allMeteringLabelRules.add(meteringLabelRule);
81 logger.debug("Exiting getAllMeteringLabelRules, Found {} OpenStackMeteringLabelRules", allMeteringLabelRules.size());
82 List<NeutronMeteringLabelRule> ans = new ArrayList<NeutronMeteringLabelRule>();
83 ans.addAll(allMeteringLabelRules);
88 public boolean addNeutronMeteringLabelRule(NeutronMeteringLabelRule input) {
89 if (neutronMeteringLabelRuleExists(input.getMeteringLabelRuleUUID())) {
92 meteringLabelRuleDB.putIfAbsent(input.getMeteringLabelRuleUUID(), input);
93 //TODO: add code to find INeutronMeteringLabelRuleAware services and call newtorkCreated on them
98 public boolean removeNeutronMeteringLabelRule(String uuid) {
99 if (!neutronMeteringLabelRuleExists(uuid)) {
102 meteringLabelRuleDB.remove(uuid);
103 //TODO: add code to find INeutronMeteringLabelRuleAware services and call newtorkDeleted on them
108 public boolean updateNeutronMeteringLabelRule(String uuid, NeutronMeteringLabelRule delta) {
109 if (!neutronMeteringLabelRuleExists(uuid)) {
112 NeutronMeteringLabelRule target = meteringLabelRuleDB.get(uuid);
113 return overwrite(target, delta);
117 public boolean neutronMeteringLabelRuleInUse(String netUUID) {
118 if (!neutronMeteringLabelRuleExists(netUUID)) {