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.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
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>();
34 NeutronMeteringLabelRuleInterface(ProviderContext providerContext) {
35 super(providerContext);
38 // this method uses reflection to update an object from it's delta.
40 private boolean overwrite(Object target, Object delta) {
41 Method[] methods = target.getClass().getMethods();
43 for(Method toMethod: methods){
44 if(toMethod.getDeclaringClass().equals(target.getClass())
45 && toMethod.getName().startsWith("set")){
47 String toName = toMethod.getName();
48 String fromName = toName.replace("set", "get");
51 Method fromMethod = delta.getClass().getMethod(fromName);
52 Object value = fromMethod.invoke(delta, (Object[])null);
54 toMethod.invoke(target, value);
56 } catch (Exception e) {
65 // IfNBMeteringLabelRuleCRUD methods
68 public boolean neutronMeteringLabelRuleExists(String uuid) {
69 return meteringLabelRuleDB.containsKey(uuid);
73 public NeutronMeteringLabelRule getNeutronMeteringLabelRule(String uuid) {
74 if (!neutronMeteringLabelRuleExists(uuid)) {
77 return meteringLabelRuleDB.get(uuid);
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);
87 logger.debug("Exiting getAllMeteringLabelRules, Found {} OpenStackMeteringLabelRules", allMeteringLabelRules.size());
88 List<NeutronMeteringLabelRule> ans = new ArrayList<NeutronMeteringLabelRule>();
89 ans.addAll(allMeteringLabelRules);
94 public boolean addNeutronMeteringLabelRule(NeutronMeteringLabelRule input) {
95 if (neutronMeteringLabelRuleExists(input.getMeteringLabelRuleUUID())) {
98 meteringLabelRuleDB.putIfAbsent(input.getMeteringLabelRuleUUID(), input);
99 //TODO: add code to find INeutronMeteringLabelRuleAware services and call newtorkCreated on them
104 public boolean removeNeutronMeteringLabelRule(String uuid) {
105 if (!neutronMeteringLabelRuleExists(uuid)) {
108 meteringLabelRuleDB.remove(uuid);
109 //TODO: add code to find INeutronMeteringLabelRuleAware services and call newtorkDeleted on them
114 public boolean updateNeutronMeteringLabelRule(String uuid, NeutronMeteringLabelRule delta) {
115 if (!neutronMeteringLabelRuleExists(uuid)) {
118 NeutronMeteringLabelRule target = meteringLabelRuleDB.get(uuid);
119 return overwrite(target, delta);
123 public boolean neutronMeteringLabelRuleInUse(String netUUID) {
124 if (!neutronMeteringLabelRuleExists(netUUID)) {
131 protected InstanceIdentifier createInstanceIdentifier(DataObject item) {
132 // TODO Auto-generated method stub
137 protected DataObject toMd(Object neutronObject) {
138 // TODO Auto-generated method stub
143 protected DataObject toMd(String uuid) {
144 // TODO Auto-generated method stub