7418bb2f3d40da14624d60688019ef1a3891ca99
[controller.git] / opendaylight / networkconfiguration / neutron / implementation / src / main / java / org / opendaylight / controller / networkconfig / neutron / implementation / NeutronLoadBalancerPoolMemberInterface.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.apache.felix.dm.Component;
12 import org.opendaylight.controller.clustering.services.CacheConfigException;
13 import org.opendaylight.controller.clustering.services.CacheExistException;
14 import org.opendaylight.controller.clustering.services.IClusterContainerServices;
15 import org.opendaylight.controller.clustering.services.IClusterServices;
16 import org.opendaylight.controller.configuration.ConfigurationObject;
17 import org.opendaylight.controller.configuration.IConfigurationContainerAware;
18 import org.opendaylight.controller.configuration.IConfigurationContainerService;
19 import org.opendaylight.controller.networkconfig.neutron.INeutronLoadBalancerPoolMemberCRUD;
20 import org.opendaylight.controller.networkconfig.neutron.NeutronLoadBalancerPoolMember;
21 import org.opendaylight.controller.sal.utils.IObjectReader;
22 import org.opendaylight.controller.sal.utils.Status;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.io.ObjectInputStream;
29 import java.lang.reflect.Method;
30 import java.util.ArrayList;
31 import java.util.Dictionary;
32 import java.util.EnumSet;
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Set;
37 import java.util.concurrent.ConcurrentMap;
38
39 public class NeutronLoadBalancerPoolMemberInterface
40         implements INeutronLoadBalancerPoolMemberCRUD, IConfigurationContainerAware,
41         IObjectReader {
42     private static final Logger logger = LoggerFactory.getLogger(NeutronLoadBalancerPoolMemberInterface.class);
43     private static final String FILE_NAME = "neutron.loadBalancerPoolMember.conf";
44     private String containerName = null;
45
46     private IClusterContainerServices clusterContainerService = null;
47     private IConfigurationContainerService configurationService;
48     private ConcurrentMap<String, NeutronLoadBalancerPoolMember> loadBalancerPoolMemberDB;
49
50     // methods needed for creating caches
51     void setClusterContainerService(IClusterContainerServices s) {
52         logger.debug("Cluster Service set");
53         clusterContainerService = s;
54     }
55
56     void unsetClusterContainerService(IClusterContainerServices s) {
57         if (clusterContainerService == s) {
58             logger.debug("Cluster Service removed!");
59             clusterContainerService = null;
60         }
61     }
62
63     public void setConfigurationContainerService(IConfigurationContainerService service) {
64         logger.trace("Configuration service set: {}", service);
65         configurationService = service;
66     }
67
68     public void unsetConfigurationContainerService(IConfigurationContainerService service) {
69         logger.trace("Configuration service removed: {}", service);
70         configurationService = null;
71     }
72
73     private void allocateCache() {
74         if (this.clusterContainerService == null) {
75             logger.error("un-initialized clusterContainerService, can't create cache");
76             return;
77         }
78         logger.debug("Creating Cache for Neutron LoadBalancerPoolMember");
79         try {
80             // neutron caches
81             this.clusterContainerService.createCache("neutronLoadBalancerPoolMembers",
82                     EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));
83         } catch(CacheConfigException cce) {
84             logger.error("Cache couldn't be created for Neutron LoadBalancerPoolMember -  check cache mode");
85         } catch(CacheExistException cce) {
86             logger.error("Cache for Neutron LoadBalancerPoolMember already exists, destroy and recreate");
87         }
88         logger.debug("Cache successfully created for Neutron LoadBalancerPoolMember");
89     }
90
91     @SuppressWarnings({"unchecked"})
92     private void retrieveCache() {
93         if (clusterContainerService == null) {
94             logger.error("un-initialized clusterContainerService, can't retrieve cache");
95             return;
96         }
97
98         logger.debug("Retrieving cache for Neutron LoadBalancerPoolMember");
99         loadBalancerPoolMemberDB = (ConcurrentMap<String, NeutronLoadBalancerPoolMember>) clusterContainerService
100                 .getCache("neutronLoadBalancerPoolMembers");
101         if (loadBalancerPoolMemberDB == null) {
102             logger.error("Cache couldn't be retrieved for Neutron LoadBalancerPoolMember");
103         }
104         logger.debug("Cache was successfully retrieved for Neutron LoadBalancerPoolMember");
105     }
106
107     private void destroyCache() {
108         if (clusterContainerService == null) {
109             logger.error("un-initialized clusterMger, can't destroy cache");
110             return;
111         }
112         logger.debug("Destroying Cache for HostTracker");
113         clusterContainerService.destroyCache("neutronLoadBalancerPoolMembers");
114     }
115
116     private void startUp() {
117         allocateCache();
118         retrieveCache();
119         loadConfiguration();
120     }
121
122     /**
123      * Function called by the dependency manager when all the required
124      * dependencies are satisfied
125      */
126     void init(Component c) {
127         Dictionary<?, ?> props = c.getServiceProperties();
128         if (props != null) {
129             this.containerName = (String) props.get("containerName");
130             logger.debug("Running containerName: {}", this.containerName);
131         } else {
132             // In the Global instance case the containerName is empty
133             this.containerName = "";
134         }
135         startUp();
136     }
137
138     /**
139      * Function called by the dependency manager when at least one dependency
140      * become unsatisfied or when the component is shutting down because for
141      * example bundle is being stopped.
142      */
143     void destroy() {
144         destroyCache();
145     }
146
147     /**
148      * Function called by dependency manager after "init ()" is called and after
149      * the services provided by the class are registered in the service registry
150      */
151     void start() {
152     }
153
154     /**
155      * Function called by the dependency manager before the services exported by
156      * the component are unregistered, this will be followed by a "destroy ()"
157      * calls
158      */
159     void stop() {
160     }
161
162     // this method uses reflection to update an object from it's delta.
163
164     private boolean overwrite(Object target, Object delta) {
165         Method[] methods = target.getClass().getMethods();
166
167         for (Method toMethod : methods) {
168             if (toMethod.getDeclaringClass().equals(target.getClass())
169                     && toMethod.getName().startsWith("set")) {
170
171                 String toName = toMethod.getName();
172                 String fromName = toName.replace("set", "get");
173
174                 try {
175                     Method fromMethod = delta.getClass().getMethod(fromName);
176                     Object value = fromMethod.invoke(delta, (Object[]) null);
177                     if (value != null) {
178                         toMethod.invoke(target, value);
179                     }
180                 } catch(Exception e) {
181                     e.printStackTrace();
182                     return false;
183                 }
184             }
185         }
186         return true;
187     }
188
189     @Override
190     public boolean neutronLoadBalancerPoolMemberExists(String uuid) {
191         return loadBalancerPoolMemberDB.containsKey(uuid);
192     }
193
194     @Override
195     public NeutronLoadBalancerPoolMember getNeutronLoadBalancerPoolMember(String uuid) {
196         if (!neutronLoadBalancerPoolMemberExists(uuid)) {
197             logger.debug("No LoadBalancerPoolMember Have Been Defined");
198             return null;
199         }
200         return loadBalancerPoolMemberDB.get(uuid);
201     }
202
203     @Override
204     public List<NeutronLoadBalancerPoolMember> getAllNeutronLoadBalancerPoolMembers() {
205         Set<NeutronLoadBalancerPoolMember> allLoadBalancerPoolMembers = new HashSet<NeutronLoadBalancerPoolMember>();
206         for (Map.Entry<String, NeutronLoadBalancerPoolMember> entry : loadBalancerPoolMemberDB.entrySet()) {
207             NeutronLoadBalancerPoolMember loadBalancerPoolMember = entry.getValue();
208             allLoadBalancerPoolMembers.add(loadBalancerPoolMember);
209         }
210         logger.debug("Exiting getLoadBalancerPoolMembers, Found {} OpenStackLoadBalancerPoolMember",
211                 allLoadBalancerPoolMembers.size());
212         List<NeutronLoadBalancerPoolMember> ans = new ArrayList<NeutronLoadBalancerPoolMember>();
213         ans.addAll(allLoadBalancerPoolMembers);
214         return ans;
215     }
216
217     @Override
218     public boolean addNeutronLoadBalancerPoolMember(NeutronLoadBalancerPoolMember input) {
219         if (neutronLoadBalancerPoolMemberExists(input.getPoolMemberID())) {
220             return false;
221         }
222         loadBalancerPoolMemberDB.putIfAbsent(input.getPoolMemberID(), input);
223         return true;
224     }
225
226     @Override
227     public boolean removeNeutronLoadBalancerPoolMember(String uuid) {
228         if (!neutronLoadBalancerPoolMemberExists(uuid)) {
229             return false;
230         }
231         loadBalancerPoolMemberDB.remove(uuid);
232         return true;
233     }
234
235     @Override
236     public boolean updateNeutronLoadBalancerPoolMember(String uuid, NeutronLoadBalancerPoolMember delta) {
237         if (!neutronLoadBalancerPoolMemberExists(uuid)) {
238             return false;
239         }
240         NeutronLoadBalancerPoolMember target = loadBalancerPoolMemberDB.get(uuid);
241         return overwrite(target, delta);
242     }
243
244     @Override
245     public boolean neutronLoadBalancerPoolMemberInUse(String loadBalancerPoolMemberID) {
246         return !neutronLoadBalancerPoolMemberExists(loadBalancerPoolMemberID);
247     }
248
249     private void loadConfiguration() {
250         for (ConfigurationObject conf : configurationService.retrieveConfiguration(this, FILE_NAME)) {
251             NeutronLoadBalancerPoolMember nn = (NeutronLoadBalancerPoolMember) conf;
252             loadBalancerPoolMemberDB.put(nn.getPoolMemberID(), nn);
253         }
254     }
255
256     @Override
257     public Status saveConfiguration() {
258         return configurationService.persistConfiguration(
259                 new ArrayList<ConfigurationObject>(loadBalancerPoolMemberDB.values()),
260                 FILE_NAME);
261     }
262
263     @Override
264     public Object readObject(ObjectInputStream ois) throws FileNotFoundException, IOException, ClassNotFoundException {
265         return ois.readObject();
266     }
267 }