Merge "Example of MD-transcribing for network"
[neutron.git] / transcriber / src / main / java / org / opendaylight / neutron / transcriber / NeutronLoadBalancerPoolInterface.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.neutron.transcriber;
10
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;
16 import java.util.Set;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
19
20 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
21 import org.opendaylight.neutron.spi.INeutronLoadBalancerPoolCRUD;
22 import org.opendaylight.neutron.spi.NeutronLoadBalancerPool;
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;
27
28 public class NeutronLoadBalancerPoolInterface extends AbstractNeutronInterface implements INeutronLoadBalancerPoolCRUD {
29     private static final Logger logger = LoggerFactory.getLogger(NeutronLoadBalancerPoolInterface.class);
30     private ConcurrentMap<String, NeutronLoadBalancerPool> loadBalancerPoolDB = new ConcurrentHashMap<String, NeutronLoadBalancerPool>();
31
32
33     NeutronLoadBalancerPoolInterface(ProviderContext providerContext) {
34         super(providerContext);
35     }
36
37     // this method uses reflection to update an object from it's delta.
38
39     private boolean overwrite(Object target, Object delta) {
40         Method[] methods = target.getClass().getMethods();
41
42         for (Method toMethod : methods) {
43             if (toMethod.getDeclaringClass().equals(target.getClass())
44                     && toMethod.getName().startsWith("set")) {
45
46                 String toName = toMethod.getName();
47                 String fromName = toName.replace("set", "get");
48
49                 try {
50                     Method fromMethod = delta.getClass().getMethod(fromName);
51                     Object value = fromMethod.invoke(delta, (Object[]) null);
52                     if (value != null) {
53                         toMethod.invoke(target, value);
54                     }
55                 } catch (Exception e) {
56                     e.printStackTrace();
57                     return false;
58                 }
59             }
60         }
61         return true;
62     }
63
64     @Override
65     public boolean neutronLoadBalancerPoolExists(String uuid) {
66         return loadBalancerPoolDB.containsKey(uuid);
67     }
68
69     @Override
70     public NeutronLoadBalancerPool getNeutronLoadBalancerPool(String uuid) {
71         if (!neutronLoadBalancerPoolExists(uuid)) {
72             logger.debug("No LoadBalancerPool has Been Defined");
73             return null;
74         }
75         return loadBalancerPoolDB.get(uuid);
76     }
77
78     @Override
79     public List<NeutronLoadBalancerPool> getAllNeutronLoadBalancerPools() {
80         Set<NeutronLoadBalancerPool> allLoadBalancerPools = new HashSet<NeutronLoadBalancerPool>();
81         for (Entry<String, NeutronLoadBalancerPool> entry : loadBalancerPoolDB.entrySet()) {
82             NeutronLoadBalancerPool loadBalancerPool = entry.getValue();
83             allLoadBalancerPools.add(loadBalancerPool);
84         }
85         logger.debug("Exiting getLoadBalancerPools, Found {} OpenStackLoadBalancerPool", allLoadBalancerPools.size());
86         List<NeutronLoadBalancerPool> ans = new ArrayList<NeutronLoadBalancerPool>();
87         ans.addAll(allLoadBalancerPools);
88         return ans;
89     }
90
91     @Override
92     public boolean addNeutronLoadBalancerPool(NeutronLoadBalancerPool input) {
93         if (neutronLoadBalancerPoolExists(input.getLoadBalancerPoolID())) {
94             return false;
95         }
96         loadBalancerPoolDB.putIfAbsent(input.getLoadBalancerPoolID(), input);
97         //TODO: add code to find INeutronLoadBalancerPoolAware services and call newtorkCreated on them
98         return true;
99     }
100
101     @Override
102     public boolean removeNeutronLoadBalancerPool(String uuid) {
103         if (!neutronLoadBalancerPoolExists(uuid)) {
104             return false;
105         }
106         loadBalancerPoolDB.remove(uuid);
107         //TODO: add code to find INeutronLoadBalancerPoolAware services and call newtorkDeleted on them
108         return true;
109     }
110
111     @Override
112     public boolean updateNeutronLoadBalancerPool(String uuid, NeutronLoadBalancerPool delta) {
113         if (!neutronLoadBalancerPoolExists(uuid)) {
114             return false;
115         }
116         NeutronLoadBalancerPool target = loadBalancerPoolDB.get(uuid);
117         return overwrite(target, delta);
118     }
119
120     @Override
121     public boolean neutronLoadBalancerPoolInUse(String loadBalancerPoolUUID) {
122         return !neutronLoadBalancerPoolExists(loadBalancerPoolUUID);
123     }
124
125     @Override
126     protected InstanceIdentifier createInstanceIdentifier(DataObject item) {
127         // TODO Auto-generated method stub
128         return null;
129     }
130
131     @Override
132     protected DataObject toMd(Object neutronObject) {
133         // TODO Auto-generated method stub
134         return null;
135     }
136
137     @Override
138     protected DataObject toMd(String uuid) {
139         // TODO Auto-generated method stub
140         return null;
141     }
142
143 }