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
10 package org.opendaylight.controller.networkconfig.neutron.implementation;
12 import java.io.FileNotFoundException;
13 import java.io.IOException;
14 import java.io.ObjectInputStream;
15 import java.lang.reflect.Method;
16 import java.util.ArrayList;
17 import java.util.Dictionary;
18 import java.util.EnumSet;
19 import java.util.HashSet;
20 import java.util.Map.Entry;
21 import java.util.List;
23 import java.util.concurrent.ConcurrentMap;
24 import org.apache.felix.dm.Component;
25 import org.opendaylight.controller.clustering.services.CacheConfigException;
26 import org.opendaylight.controller.clustering.services.CacheExistException;
27 import org.opendaylight.controller.clustering.services.IClusterContainerServices;
28 import org.opendaylight.controller.clustering.services.IClusterServices;
29 import org.opendaylight.controller.configuration.ConfigurationObject;
30 import org.opendaylight.controller.configuration.IConfigurationContainerAware;
31 import org.opendaylight.controller.configuration.IConfigurationContainerService;
32 import org.opendaylight.controller.networkconfig.neutron.INeutronSecurityGroupCRUD;
33 import org.opendaylight.controller.networkconfig.neutron.NeutronSecurityGroup;
34 import org.opendaylight.controller.sal.utils.IObjectReader;
35 import org.opendaylight.controller.sal.utils.Status;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 public class NeutronSecurityGroupInterface implements INeutronSecurityGroupCRUD, IConfigurationContainerAware, IObjectReader {
41 private static final Logger logger = LoggerFactory.getLogger(NeutronSecurityGroupInterface.class);
42 private static final String FILE_NAME ="neutron.securitygroup.conf";
43 private String containerName = null;
45 private IClusterContainerServices clusterContainerService = null;
46 private IConfigurationContainerService configurationService;
47 private ConcurrentMap<String, NeutronSecurityGroup> securityGroupDB;
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 Security Groups");
80 this.clusterContainerService.createCache("neutronSecurityGroups",
81 EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));
82 } catch (CacheConfigException cce) {
83 logger.error("Cache couldn't be created for Neutron Security Groups - check cache mode");
84 } catch (CacheExistException cce) {
85 logger.error("Cache for Neutron Security Groups already exists, destroy and recreate");
87 logger.debug("Cache successfully created for Neutron Security Groups");
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 Security Groups");
98 securityGroupDB = (ConcurrentMap<String, NeutronSecurityGroup>) clusterContainerService
99 .getCache("neutronSecurityGroups");
100 if (securityGroupDB == null) {
101 logger.error("Cache couldn't be retrieved for Neutron Security Groups");
103 logger.debug("Cache was successfully retrieved for Neutron Security Groups");
106 private void destroyCache() {
107 if (clusterContainerService == null) {
108 logger.error("un-initialized clusterMger, can't destroy cache");
111 logger.debug("Destroying Cache for Neutron Security Groups");
112 clusterContainerService.destroyCache("neutronSecurityGroups");
115 private void startUp() {
122 * Function called by the dependency manager when all the required
123 * dependencies are satisfied
126 void init(Component c) {
127 Dictionary<?, ?> props = c.getServiceProperties();
129 this.containerName = (String) props.get("containerName");
130 logger.debug("Running containerName: {}", this.containerName);
132 // In the Global instance case the containerName is empty
133 this.containerName = "";
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.
149 * Function called by dependency manager after "init ()" is called and after
150 * the services provided by the class are registered in the service registry
157 * Function called by the dependency manager before the services exported by
158 * the component are unregistered, this will be followed by a "destroy ()"
165 // this method uses reflection to update an object from it's delta.
167 private boolean overwrite(Object target, Object delta) {
168 Method[] methods = target.getClass().getMethods();
170 for(Method toMethod: methods){
171 if(toMethod.getDeclaringClass().equals(target.getClass())
172 && toMethod.getName().startsWith("set")){
174 String toName = toMethod.getName();
175 String fromName = toName.replace("set", "get");
178 Method fromMethod = delta.getClass().getMethod(fromName);
179 Object value = fromMethod.invoke(delta, (Object[])null);
181 toMethod.invoke(target, value);
183 } catch (Exception e) {
193 public boolean neutronSecurityGroupExists(String uuid) {
194 return securityGroupDB.containsKey(uuid);
198 public NeutronSecurityGroup getNeutronSecurityGroup(String uuid) {
199 if (!neutronSecurityGroupExists(uuid)) {
200 logger.debug("No Security Groups Have Been Defined");
203 return securityGroupDB.get(uuid);
207 public List<NeutronSecurityGroup> getAllNeutronSecurityGroups() {
208 Set<NeutronSecurityGroup> allSecurityGroups = new HashSet<NeutronSecurityGroup>();
209 for (Entry<String, NeutronSecurityGroup> entry : securityGroupDB.entrySet()) {
210 NeutronSecurityGroup securityGroup = entry.getValue();
211 allSecurityGroups.add(securityGroup);
213 logger.debug("Exiting getSecurityGroups, Found {} OpenStackSecurityGroup", allSecurityGroups.size());
214 List<NeutronSecurityGroup> ans = new ArrayList<NeutronSecurityGroup>();
215 ans.addAll(allSecurityGroups);
220 public boolean addNeutronSecurityGroup(NeutronSecurityGroup input) {
221 if (neutronSecurityGroupExists(input.getSecurityGroupUUID())) {
224 securityGroupDB.putIfAbsent(input.getSecurityGroupUUID(), input);
229 public boolean removeNeutronSecurityGroup(String uuid) {
230 if (!neutronSecurityGroupExists(uuid)) {
233 securityGroupDB.remove(uuid);
238 public boolean updateNeutronSecurityGroup(String uuid, NeutronSecurityGroup delta) {
239 if (!neutronSecurityGroupExists(uuid)) {
242 NeutronSecurityGroup target = securityGroupDB.get(uuid);
243 return overwrite(target, delta);
247 public boolean neutronSecurityGroupInUse(String securityGroupUUID) {
248 return !neutronSecurityGroupExists(securityGroupUUID);
251 private void loadConfiguration() {
252 for (ConfigurationObject conf : configurationService.retrieveConfiguration(this, FILE_NAME)) {
253 NeutronSecurityGroup nn = (NeutronSecurityGroup) conf;
254 securityGroupDB.put(nn.getSecurityGroupUUID(), nn);
259 public Status saveConfiguration() {
260 return configurationService.persistConfiguration(new ArrayList<ConfigurationObject>(securityGroupDB.values()),
265 public Object readObject(ObjectInputStream ois) throws FileNotFoundException, IOException, ClassNotFoundException {
266 return ois.readObject();