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.opendaylight.controller.networkconfig.neutron.INeutronFirewallCRUD;
12 import org.opendaylight.controller.networkconfig.neutron.NeutronFirewall;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
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;
22 import java.util.concurrent.ConcurrentHashMap;
23 import java.util.concurrent.ConcurrentMap;
25 public class NeutronFirewallInterface implements INeutronFirewallCRUD {
26 private static final Logger logger = LoggerFactory.getLogger(NeutronFirewallInterface.class);
28 private ConcurrentMap<String, NeutronFirewall> firewallDB = new ConcurrentHashMap<String, NeutronFirewall>();
30 // this method uses reflection to update an object from it's delta.
32 private boolean overwrite(Object target, Object delta) {
33 Method[] methods = target.getClass().getMethods();
35 for (Method toMethod : methods) {
36 if (toMethod.getDeclaringClass().equals(target.getClass())
37 && toMethod.getName().startsWith("set")) {
39 String toName = toMethod.getName();
40 String fromName = toName.replace("set", "get");
43 Method fromMethod = delta.getClass().getMethod(fromName);
44 Object value = fromMethod.invoke(delta, (Object[]) null);
46 toMethod.invoke(target, value);
48 } catch (Exception e) {
58 public boolean neutronFirewallExists(String uuid) {
59 return firewallDB.containsKey(uuid);
63 public NeutronFirewall getNeutronFirewall(String uuid) {
64 if (!neutronFirewallExists(uuid)) {
65 logger.debug("No Firewall Have Been Defined");
68 return firewallDB.get(uuid);
72 public List<NeutronFirewall> getAllNeutronFirewalls() {
73 Set<NeutronFirewall> allFirewalls = new HashSet<NeutronFirewall>();
74 for (Entry<String, NeutronFirewall> entry : firewallDB.entrySet()) {
75 NeutronFirewall firewall = entry.getValue();
76 allFirewalls.add(firewall);
78 logger.debug("Exiting getFirewalls, Found {} OpenStackFirewall", allFirewalls.size());
79 List<NeutronFirewall> ans = new ArrayList<NeutronFirewall>();
80 ans.addAll(allFirewalls);
85 public boolean addNeutronFirewall(NeutronFirewall input) {
86 if (neutronFirewallExists(input.getFirewallUUID())) {
89 firewallDB.putIfAbsent(input.getFirewallUUID(), input);
94 public boolean removeNeutronFirewall(String uuid) {
95 if (!neutronFirewallExists(uuid)) {
98 firewallDB.remove(uuid);
103 public boolean updateNeutronFirewall(String uuid, NeutronFirewall delta) {
104 if (!neutronFirewallExists(uuid)) {
107 NeutronFirewall target = firewallDB.get(uuid);
108 return overwrite(target, delta);
112 public boolean neutronFirewallInUse(String firewallUUID) {
113 return !neutronFirewallExists(firewallUUID);