Make neutron a simple osgi app
[controller.git] / opendaylight / networkconfiguration / neutron / implementation / src / main / java / org / opendaylight / controller / networkconfig / neutron / implementation / NeutronSecurityRuleInterface.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
10 package org.opendaylight.controller.networkconfig.neutron.implementation;
11
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;
17 import java.util.Set;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.ConcurrentMap;
20
21 import org.opendaylight.controller.networkconfig.neutron.INeutronSecurityRuleCRUD;
22 import org.opendaylight.controller.networkconfig.neutron.NeutronSecurityRule;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26
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>();
30
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();
34
35         for (Method toMethod : methods) {
36             if (toMethod.getDeclaringClass().equals(target.getClass())
37                 && toMethod.getName().startsWith("set")) {
38
39                 String toName = toMethod.getName();
40                 String fromName = toName.replace("set", "get");
41
42                 try {
43                     Method fromMethod = delta.getClass().getMethod(fromName);
44                     Object value = fromMethod.invoke(delta, (Object[]) null);
45                     if (value != null) {
46                         toMethod.invoke(target, value);
47                     }
48                 } catch (Exception e) {
49                     e.printStackTrace();
50                     return false;
51                 }
52             }
53         }
54         return true;
55     }
56
57     @Override
58     public boolean neutronSecurityRuleExists(String uuid) {
59         return securityRuleDB.containsKey(uuid);
60     }
61
62     @Override
63     public NeutronSecurityRule getNeutronSecurityRule(String uuid) {
64         if (!neutronSecurityRuleExists(uuid)) {
65             logger.debug("No Security Rules Have Been Defined");
66             return null;
67         }
68         return securityRuleDB.get(uuid);
69     }
70
71     @Override
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);
77         }
78         logger.debug("Exiting getSecurityRule, Found {} OpenStackSecurityRule", allSecurityRules.size());
79         List<NeutronSecurityRule> ans = new ArrayList<NeutronSecurityRule>();
80         ans.addAll(allSecurityRules);
81         return ans;
82     }
83
84     @Override
85     public boolean addNeutronSecurityRule(NeutronSecurityRule input) {
86         if (neutronSecurityRuleExists(input.getSecurityRuleUUID())) {
87             return false;
88         }
89         securityRuleDB.putIfAbsent(input.getSecurityRuleUUID(), input);
90         return true;
91     }
92
93     @Override
94     public boolean removeNeutronSecurityRule(String uuid) {
95         if (!neutronSecurityRuleExists(uuid)) {
96             return false;
97         }
98         securityRuleDB.remove(uuid);
99         return true;
100     }
101
102     @Override
103     public boolean updateNeutronSecurityRule(String uuid, NeutronSecurityRule delta) {
104         if (!neutronSecurityRuleExists(uuid)) {
105             return false;
106         }
107         NeutronSecurityRule target = securityRuleDB.get(uuid);
108         return overwrite(target, delta);
109     }
110
111     @Override
112     public boolean neutronSecurityRuleInUse(String securityRuleUUID) {
113         return !neutronSecurityRuleExists(securityRuleUUID);
114     }
115
116 }