2 * Copyright (C) 2014 Red Hat, Inc.
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 org.opendaylight.neutron.spi.INeutronLoadBalancerCRUD;
12 import org.opendaylight.neutron.spi.NeutronLoadBalancer;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
16 import java.lang.reflect.Method;
17 import java.util.ArrayList;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Map.Entry;
22 import java.util.concurrent.ConcurrentHashMap;
23 import java.util.concurrent.ConcurrentMap;
25 public class NeutronLoadBalancerInterface implements INeutronLoadBalancerCRUD {
26 private static final Logger logger = LoggerFactory.getLogger(NeutronLoadBalancerInterface.class);
27 private ConcurrentMap<String, NeutronLoadBalancer> loadBalancerDB = new ConcurrentHashMap<String, NeutronLoadBalancer>();
30 // this method uses reflection to update an object from it's delta.
32 private boolean overwrite(Object target, Object delta) {
33 Method[] methods = target.getClass().getMethods();
35 for (Method toMethod : methods) {
36 if (toMethod.getDeclaringClass().equals(target.getClass())
37 && toMethod.getName().startsWith("set")) {
39 String toName = toMethod.getName();
40 String fromName = toName.replace("set", "get");
43 Method fromMethod = delta.getClass().getMethod(fromName);
44 Object value = fromMethod.invoke(delta, (Object[]) null);
46 toMethod.invoke(target, value);
48 } catch (Exception e) {
58 public boolean neutronLoadBalancerExists(String uuid) {
59 return loadBalancerDB.containsKey(uuid);
63 public NeutronLoadBalancer getNeutronLoadBalancer(String uuid) {
64 if (!neutronLoadBalancerExists(uuid)) {
65 logger.debug("No LoadBalancer Have Been Defined");
68 return loadBalancerDB.get(uuid);
72 public List<NeutronLoadBalancer> getAllNeutronLoadBalancers() {
73 Set<NeutronLoadBalancer> allLoadBalancers = new HashSet<NeutronLoadBalancer>();
74 for (Entry<String, NeutronLoadBalancer> entry : loadBalancerDB.entrySet()) {
75 NeutronLoadBalancer loadBalancer = entry.getValue();
76 allLoadBalancers.add(loadBalancer);
78 logger.debug("Exiting getLoadBalancers, Found {} OpenStackLoadBalancer", allLoadBalancers.size());
79 List<NeutronLoadBalancer> ans = new ArrayList<NeutronLoadBalancer>();
80 ans.addAll(allLoadBalancers);
85 public boolean addNeutronLoadBalancer(NeutronLoadBalancer input) {
86 if (neutronLoadBalancerExists(input.getLoadBalancerID())) {
89 loadBalancerDB.putIfAbsent(input.getLoadBalancerID(), input);
90 //TODO: add code to find INeutronLoadBalancerAware services and call newtorkCreated on them
95 public boolean removeNeutronLoadBalancer(String uuid) {
96 if (!neutronLoadBalancerExists(uuid)) {
99 loadBalancerDB.remove(uuid);
100 //TODO: add code to find INeutronLoadBalancerAware services and call newtorkDeleted on them
105 public boolean updateNeutronLoadBalancer(String uuid, NeutronLoadBalancer delta) {
106 if (!neutronLoadBalancerExists(uuid)) {
109 NeutronLoadBalancer target = loadBalancerDB.get(uuid);
110 return overwrite(target, delta);
114 public boolean neutronLoadBalancerInUse(String loadBalancerUUID) {
115 return !neutronLoadBalancerExists(loadBalancerUUID);