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.controller.networkconfig.neutron.implementation;
11 import org.opendaylight.controller.networkconfig.neutron.INeutronLoadBalancerPoolCRUD;
12 import org.opendaylight.controller.networkconfig.neutron.NeutronLoadBalancerPool;
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 NeutronLoadBalancerPoolInterface implements INeutronLoadBalancerPoolCRUD {
26 private static final Logger logger = LoggerFactory.getLogger(NeutronLoadBalancerPoolInterface.class);
27 private ConcurrentMap<String, NeutronLoadBalancerPool> loadBalancerPoolDB = new ConcurrentHashMap<String, NeutronLoadBalancerPool>();
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 neutronLoadBalancerPoolExists(String uuid) {
59 return loadBalancerPoolDB.containsKey(uuid);
63 public NeutronLoadBalancerPool getNeutronLoadBalancerPool(String uuid) {
64 if (!neutronLoadBalancerPoolExists(uuid)) {
65 logger.debug("No LoadBalancerPool has Been Defined");
68 return loadBalancerPoolDB.get(uuid);
72 public List<NeutronLoadBalancerPool> getAllNeutronLoadBalancerPools() {
73 Set<NeutronLoadBalancerPool> allLoadBalancerPools = new HashSet<NeutronLoadBalancerPool>();
74 for (Entry<String, NeutronLoadBalancerPool> entry : loadBalancerPoolDB.entrySet()) {
75 NeutronLoadBalancerPool loadBalancerPool = entry.getValue();
76 allLoadBalancerPools.add(loadBalancerPool);
78 logger.debug("Exiting getLoadBalancerPools, Found {} OpenStackLoadBalancerPool", allLoadBalancerPools.size());
79 List<NeutronLoadBalancerPool> ans = new ArrayList<NeutronLoadBalancerPool>();
80 ans.addAll(allLoadBalancerPools);
85 public boolean addNeutronLoadBalancerPool(NeutronLoadBalancerPool input) {
86 if (neutronLoadBalancerPoolExists(input.getLoadBalancerPoolID())) {
89 loadBalancerPoolDB.putIfAbsent(input.getLoadBalancerPoolID(), input);
90 //TODO: add code to find INeutronLoadBalancerPoolAware services and call newtorkCreated on them
95 public boolean removeNeutronLoadBalancerPool(String uuid) {
96 if (!neutronLoadBalancerPoolExists(uuid)) {
99 loadBalancerPoolDB.remove(uuid);
100 //TODO: add code to find INeutronLoadBalancerPoolAware services and call newtorkDeleted on them
105 public boolean updateNeutronLoadBalancerPool(String uuid, NeutronLoadBalancerPool delta) {
106 if (!neutronLoadBalancerPoolExists(uuid)) {
109 NeutronLoadBalancerPool target = loadBalancerPoolDB.get(uuid);
110 return overwrite(target, delta);
114 public boolean neutronLoadBalancerPoolInUse(String loadBalancerPoolUUID) {
115 return !neutronLoadBalancerPoolExists(loadBalancerPoolUUID);