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.neutron.transcriber;
12 import java.lang.reflect.Method;
13 import java.util.ArrayList;
14 import java.util.HashSet;
15 import java.util.Map.Entry;
16 import java.util.List;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.ConcurrentMap;
21 import org.opendaylight.neutron.spi.INeutronSecurityRuleCRUD;
22 import org.opendaylight.neutron.spi.NeutronSecurityRule;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 public class NeutronSecurityRuleInterface implements INeutronSecurityRuleCRUD {
28 private static final Logger logger = LoggerFactory.getLogger(NeutronSecurityRuleInterface.class);
29 private ConcurrentMap<String, NeutronSecurityRule> securityRuleDB = new ConcurrentHashMap<String, NeutronSecurityRule>();
31 // 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 neutronSecurityRuleExists(String uuid) {
59 return securityRuleDB.containsKey(uuid);
63 public NeutronSecurityRule getNeutronSecurityRule(String uuid) {
64 if (!neutronSecurityRuleExists(uuid)) {
65 logger.debug("No Security Rules Have Been Defined");
68 return securityRuleDB.get(uuid);
72 public List<NeutronSecurityRule> getAllNeutronSecurityRules() {
73 Set<NeutronSecurityRule> allSecurityRules = new HashSet<NeutronSecurityRule>();
74 for (Entry<String, NeutronSecurityRule> entry : securityRuleDB.entrySet()) {
75 NeutronSecurityRule securityRule = entry.getValue();
76 allSecurityRules.add(securityRule);
78 logger.debug("Exiting getSecurityRule, Found {} OpenStackSecurityRule", allSecurityRules.size());
79 List<NeutronSecurityRule> ans = new ArrayList<NeutronSecurityRule>();
80 ans.addAll(allSecurityRules);
85 public boolean addNeutronSecurityRule(NeutronSecurityRule input) {
86 if (neutronSecurityRuleExists(input.getSecurityRuleUUID())) {
89 securityRuleDB.putIfAbsent(input.getSecurityRuleUUID(), input);
94 public boolean removeNeutronSecurityRule(String uuid) {
95 if (!neutronSecurityRuleExists(uuid)) {
98 securityRuleDB.remove(uuid);
103 public boolean updateNeutronSecurityRule(String uuid, NeutronSecurityRule delta) {
104 if (!neutronSecurityRuleExists(uuid)) {
107 NeutronSecurityRule target = securityRuleDB.get(uuid);
108 return overwrite(target, delta);
112 public boolean neutronSecurityRuleInUse(String securityRuleUUID) {
113 return !neutronSecurityRuleExists(securityRuleUUID);