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.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.INeutronLoadBalancerHealthMonitorCRUD;
20 import org.opendaylight.controller.networkconfig.neutron.NeutronLoadBalancerHealthMonitor;
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;
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.Entry;
37 import java.util.concurrent.ConcurrentMap;
39 public class NeutronLoadBalancerHealthMonitorInterface implements INeutronLoadBalancerHealthMonitorCRUD, IConfigurationContainerAware,
41 private static final Logger logger = LoggerFactory.getLogger(NeutronLoadBalancerHealthMonitorInterface.class);
42 private static final String FILE_NAME = "neutron.loadBalancerHealthMonitor.conf";
43 private String containerName = null;
45 private IClusterContainerServices clusterContainerService = null;
46 private IConfigurationContainerService configurationService;
47 private ConcurrentMap<String, NeutronLoadBalancerHealthMonitor> loadBalancerHealthMonitorDB;
49 // methods needed for creating caches
50 void setClusterContainerService(IClusterContainerServices s) {
51 logger.debug("Cluster Service set");
52 clusterContainerService = s;
55 void unsetClusterContainerService(IClusterContainerServices s) {
56 if (clusterContainerService == s) {
57 logger.debug("Cluster Service removed!");
58 clusterContainerService = null;
62 public void setConfigurationContainerService(IConfigurationContainerService service) {
63 logger.trace("Configuration service set: {}", service);
64 configurationService = service;
67 public void unsetConfigurationContainerService(IConfigurationContainerService service) {
68 logger.trace("Configuration service removed: {}", service);
69 configurationService = null;
72 private void allocateCache() {
73 if (this.clusterContainerService == null) {
74 logger.error("un-initialized clusterContainerService, can't create cache");
77 logger.debug("Creating Cache for Neutron LoadBalancerHealthMonitor");
80 this.clusterContainerService.createCache("neutronLoadBalancerHealthMonitors",
81 EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));
82 } catch (CacheConfigException cce) {
83 logger.error("Cache couldn't be created for Neutron LoadBalancerHealthMonitor - check cache mode");
84 } catch (CacheExistException cce) {
85 logger.error("Cache for Neutron LoadBalancerHealthMonitor already exists, destroy and recreate");
87 logger.debug("Cache successfully created for Neutron LoadBalancerHealthMonitor");
90 @SuppressWarnings ({"unchecked"})
91 private void retrieveCache() {
92 if (clusterContainerService == null) {
93 logger.error("un-initialized clusterContainerService, can't retrieve cache");
97 logger.debug("Retrieving cache for Neutron LoadBalancerHealthMonitor");
98 loadBalancerHealthMonitorDB = (ConcurrentMap<String, NeutronLoadBalancerHealthMonitor>) clusterContainerService
99 .getCache("neutronLoadBalancerHealthMonitors");
100 if (loadBalancerHealthMonitorDB == null) {
101 logger.error("Cache couldn't be retrieved for Neutron LoadBalancerHealthMonitor");
103 logger.debug("Cache was successfully retrieved for Neutron LoadBalancerHealthMonitor");
106 private void destroyCache() {
107 if (clusterContainerService == null) {
108 logger.error("un-initialized clusterMger, can't destroy cache");
111 logger.debug("Destroying Cache for LoadBalancerHealthMonitor");
112 clusterContainerService.destroyCache("neutronLoadBalancerHealthMonitors");
115 private void startUp() {
122 * Function called by the dependency manager when all the required
123 * dependencies are satisfied
125 void init(Component c) {
126 Dictionary<?, ?> props = c.getServiceProperties();
128 this.containerName = (String) props.get("containerName");
129 logger.debug("Running containerName: {}", this.containerName);
131 // In the Global instance case the containerName is empty
132 this.containerName = "";
138 * Function called by the dependency manager when at least one dependency
139 * become unsatisfied or when the component is shutting down because for
140 * example bundle is being stopped.
147 * Function called by dependency manager after "init ()" is called and after
148 * the services provided by the class are registered in the service registry
154 * Function called by the dependency manager before the services exported by
155 * the component are unregistered, this will be followed by a "destroy ()"
161 // this method uses reflection to update an object from it's delta.
163 private boolean overwrite(Object target, Object delta) {
164 Method[] methods = target.getClass().getMethods();
166 for (Method toMethod : methods) {
167 if (toMethod.getDeclaringClass().equals(target.getClass())
168 && toMethod.getName().startsWith("set")) {
170 String toName = toMethod.getName();
171 String fromName = toName.replace("set", "get");
174 Method fromMethod = delta.getClass().getMethod(fromName);
175 Object value = fromMethod.invoke(delta, (Object[]) null);
177 toMethod.invoke(target, value);
179 } catch (Exception e) {
189 public boolean neutronLoadBalancerHealthMonitorExists(String uuid) {
190 return loadBalancerHealthMonitorDB.containsKey(uuid);
194 public NeutronLoadBalancerHealthMonitor getNeutronLoadBalancerHealthMonitor(String uuid) {
195 if (!neutronLoadBalancerHealthMonitorExists(uuid)) {
196 logger.debug("No LoadBalancerHealthMonitor has Been Defined");
199 return loadBalancerHealthMonitorDB.get(uuid);
203 public List<NeutronLoadBalancerHealthMonitor> getAllNeutronLoadBalancerHealthMonitors() {
204 Set<NeutronLoadBalancerHealthMonitor> allLoadBalancerHealthMonitors = new HashSet<NeutronLoadBalancerHealthMonitor>();
205 for (Entry<String, NeutronLoadBalancerHealthMonitor> entry : loadBalancerHealthMonitorDB.entrySet()) {
206 NeutronLoadBalancerHealthMonitor loadBalancerHealthMonitor = entry.getValue();
207 allLoadBalancerHealthMonitors.add(loadBalancerHealthMonitor);
209 logger.debug("Exiting getLoadBalancerHealthMonitors, Found {} OpenStackLoadBalancerHealthMonitor", allLoadBalancerHealthMonitors.size());
210 List<NeutronLoadBalancerHealthMonitor> ans = new ArrayList<NeutronLoadBalancerHealthMonitor>();
211 ans.addAll(allLoadBalancerHealthMonitors);
216 public boolean addNeutronLoadBalancerHealthMonitor(NeutronLoadBalancerHealthMonitor input) {
217 if (neutronLoadBalancerHealthMonitorExists(input.getLoadBalancerHealthMonitorID())) {
220 loadBalancerHealthMonitorDB.putIfAbsent(input.getLoadBalancerHealthMonitorID(), input);
221 //TODO: add code to find INeutronLoadBalancerHealthMonitorAware services and call newtorkCreated on them
226 public boolean removeNeutronLoadBalancerHealthMonitor(String uuid) {
227 if (!neutronLoadBalancerHealthMonitorExists(uuid)) {
230 loadBalancerHealthMonitorDB.remove(uuid);
231 //TODO: add code to find INeutronLoadBalancerHealthMonitorAware services and call newtorkDeleted on them
236 public boolean updateNeutronLoadBalancerHealthMonitor(String uuid, NeutronLoadBalancerHealthMonitor delta) {
237 if (!neutronLoadBalancerHealthMonitorExists(uuid)) {
240 NeutronLoadBalancerHealthMonitor target = loadBalancerHealthMonitorDB.get(uuid);
241 return overwrite(target, delta);
245 public boolean neutronLoadBalancerHealthMonitorInUse(String loadBalancerHealthMonitorUUID) {
246 return !neutronLoadBalancerHealthMonitorExists(loadBalancerHealthMonitorUUID);
249 private void loadConfiguration() {
250 for (ConfigurationObject conf : configurationService.retrieveConfiguration(this, FILE_NAME)) {
251 NeutronLoadBalancerHealthMonitor nn = (NeutronLoadBalancerHealthMonitor) conf;
252 loadBalancerHealthMonitorDB.put(nn.getLoadBalancerHealthMonitorID(), nn);
257 public Status saveConfiguration() {
258 return configurationService.persistConfiguration(new ArrayList<ConfigurationObject>(loadBalancerHealthMonitorDB.values()),
263 public Object readObject(ObjectInputStream ois) throws FileNotFoundException, IOException, ClassNotFoundException {
264 return ois.readObject();