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.INeutronMeteringLabelCRUD;
22 import org.opendaylight.neutron.spi.NeutronMeteringLabel;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public class NeutronMeteringLabelInterface extends AbstractNeutronInterface implements INeutronMeteringLabelCRUD {
27 private static final Logger logger = LoggerFactory.getLogger(NeutronMeteringLabelInterface.class);
28 private ConcurrentMap<String, NeutronMeteringLabel> meteringLabelDB = new ConcurrentHashMap<String, NeutronMeteringLabel>();
30 NeutronMeteringLabelInterface(ProviderContext providerContext) {
31 super(providerContext);
34 // this method uses reflection to update an object from it's delta.
36 private boolean overwrite(Object target, Object delta) {
37 Method[] methods = target.getClass().getMethods();
39 for(Method toMethod: methods){
40 if(toMethod.getDeclaringClass().equals(target.getClass())
41 && toMethod.getName().startsWith("set")){
43 String toName = toMethod.getName();
44 String fromName = toName.replace("set", "get");
47 Method fromMethod = delta.getClass().getMethod(fromName);
48 Object value = fromMethod.invoke(delta, (Object[])null);
50 toMethod.invoke(target, value);
52 } catch (Exception e) {
61 // IfNBMeteringLabelCRUD methods
64 public boolean neutronMeteringLabelExists(String uuid) {
65 return meteringLabelDB.containsKey(uuid);
69 public NeutronMeteringLabel getNeutronMeteringLabel(String uuid) {
70 if (!neutronMeteringLabelExists(uuid)) {
73 return meteringLabelDB.get(uuid);
77 public List<NeutronMeteringLabel> getAllNeutronMeteringLabels() {
78 Set<NeutronMeteringLabel> allMeteringLabels = new HashSet<NeutronMeteringLabel>();
79 for (Entry<String, NeutronMeteringLabel> entry : meteringLabelDB.entrySet()) {
80 NeutronMeteringLabel meteringLabel = entry.getValue();
81 allMeteringLabels.add(meteringLabel);
83 logger.debug("Exiting getAllMeteringLabels, Found {} OpenStackMeteringLabels", allMeteringLabels.size());
84 List<NeutronMeteringLabel> ans = new ArrayList<NeutronMeteringLabel>();
85 ans.addAll(allMeteringLabels);
90 public boolean addNeutronMeteringLabel(NeutronMeteringLabel input) {
91 if (neutronMeteringLabelExists(input.getMeteringLabelUUID())) {
94 meteringLabelDB.putIfAbsent(input.getMeteringLabelUUID(), input);
95 //TODO: add code to find INeutronMeteringLabelAware services and call newtorkCreated on them
100 public boolean removeNeutronMeteringLabel(String uuid) {
101 if (!neutronMeteringLabelExists(uuid)) {
104 meteringLabelDB.remove(uuid);
105 //TODO: add code to find INeutronMeteringLabelAware services and call newtorkDeleted on them
110 public boolean updateNeutronMeteringLabel(String uuid, NeutronMeteringLabel delta) {
111 if (!neutronMeteringLabelExists(uuid)) {
114 NeutronMeteringLabel target = meteringLabelDB.get(uuid);
115 return overwrite(target, delta);
119 public boolean neutronMeteringLabelInUse(String netUUID) {
120 if (!neutronMeteringLabelExists(netUUID)) {