472de903588e8bac55aca1b829ee17a3471f5840
[controller.git] / opendaylight / networkconfiguration / neutron / implementation / src / main / java / org / opendaylight / controller / networkconfig / neutron / implementation / NeutronLoadBalancerInterface.java
1 /*
2  * Copyright (C) 2014 Red Hat, Inc.
3  *
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
7  */
8
9 package org.opendaylight.controller.networkconfig.neutron.implementation;
10
11 import org.opendaylight.controller.networkconfig.neutron.INeutronLoadBalancerCRUD;
12 import org.opendaylight.controller.networkconfig.neutron.NeutronLoadBalancer;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
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;
21 import java.util.Set;
22 import java.util.concurrent.ConcurrentHashMap;
23 import java.util.concurrent.ConcurrentMap;
24
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>();
28
29
30     // this method uses reflection to update an object from it's delta.
31
32     private boolean overwrite(Object target, Object delta) {
33         Method[] methods = target.getClass().getMethods();
34
35         for (Method toMethod : methods) {
36             if (toMethod.getDeclaringClass().equals(target.getClass())
37                     && toMethod.getName().startsWith("set")) {
38
39                 String toName = toMethod.getName();
40                 String fromName = toName.replace("set", "get");
41
42                 try {
43                     Method fromMethod = delta.getClass().getMethod(fromName);
44                     Object value = fromMethod.invoke(delta, (Object[]) null);
45                     if (value != null) {
46                         toMethod.invoke(target, value);
47                     }
48                 } catch (Exception e) {
49                     e.printStackTrace();
50                     return false;
51                 }
52             }
53         }
54         return true;
55     }
56
57     @Override
58     public boolean neutronLoadBalancerExists(String uuid) {
59         return loadBalancerDB.containsKey(uuid);
60     }
61
62     @Override
63     public NeutronLoadBalancer getNeutronLoadBalancer(String uuid) {
64         if (!neutronLoadBalancerExists(uuid)) {
65             logger.debug("No LoadBalancer Have Been Defined");
66             return null;
67         }
68         return loadBalancerDB.get(uuid);
69     }
70
71     @Override
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);
77         }
78         logger.debug("Exiting getLoadBalancers, Found {} OpenStackLoadBalancer", allLoadBalancers.size());
79         List<NeutronLoadBalancer> ans = new ArrayList<NeutronLoadBalancer>();
80         ans.addAll(allLoadBalancers);
81         return ans;
82     }
83
84     @Override
85     public boolean addNeutronLoadBalancer(NeutronLoadBalancer input) {
86         if (neutronLoadBalancerExists(input.getLoadBalancerID())) {
87             return false;
88         }
89         loadBalancerDB.putIfAbsent(input.getLoadBalancerID(), input);
90         //TODO: add code to find INeutronLoadBalancerAware services and call newtorkCreated on them
91         return true;
92     }
93
94     @Override
95     public boolean removeNeutronLoadBalancer(String uuid) {
96         if (!neutronLoadBalancerExists(uuid)) {
97             return false;
98         }
99         loadBalancerDB.remove(uuid);
100         //TODO: add code to find INeutronLoadBalancerAware services and call newtorkDeleted on them
101         return true;
102     }
103
104     @Override
105     public boolean updateNeutronLoadBalancer(String uuid, NeutronLoadBalancer delta) {
106         if (!neutronLoadBalancerExists(uuid)) {
107             return false;
108         }
109         NeutronLoadBalancer target = loadBalancerDB.get(uuid);
110         return overwrite(target, delta);
111     }
112
113     @Override
114     public boolean neutronLoadBalancerInUse(String loadBalancerUUID) {
115         return !neutronLoadBalancerExists(loadBalancerUUID);
116     }
117
118 }