d59bf9e093689287b9be480868318bf2e0779d80
[controller.git] / opendaylight / networkconfiguration / neutron / implementation / src / main / java / org / opendaylight / controller / networkconfig / neutron / implementation / NeutronSecurityGroupInterface.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.INeutronSecurityGroupCRUD;
22 import org.opendaylight.controller.networkconfig.neutron.NeutronSecurityGroup;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26
27 public class NeutronSecurityGroupInterface implements INeutronSecurityGroupCRUD {
28     private static final Logger logger = LoggerFactory.getLogger(NeutronSecurityGroupInterface.class);
29     private ConcurrentMap<String, NeutronSecurityGroup> securityGroupDB  = new ConcurrentHashMap<String, NeutronSecurityGroup>();
30
31
32
33     // this method uses reflection to update an object from it's delta.
34
35     private boolean overwrite(Object target, Object delta) {
36         Method[] methods = target.getClass().getMethods();
37
38         for(Method toMethod: methods){
39             if(toMethod.getDeclaringClass().equals(target.getClass())
40                 && toMethod.getName().startsWith("set")){
41
42                 String toName = toMethod.getName();
43                 String fromName = toName.replace("set", "get");
44
45                 try {
46                     Method fromMethod = delta.getClass().getMethod(fromName);
47                     Object value = fromMethod.invoke(delta, (Object[])null);
48                     if(value != null){
49                         toMethod.invoke(target, value);
50                     }
51                 } catch (Exception e) {
52                     e.printStackTrace();
53                     return false;
54                 }
55             }
56         }
57         return true;
58     }
59
60     @Override
61     public boolean neutronSecurityGroupExists(String uuid) {
62         return securityGroupDB.containsKey(uuid);
63     }
64
65     @Override
66     public NeutronSecurityGroup getNeutronSecurityGroup(String uuid) {
67         if (!neutronSecurityGroupExists(uuid)) {
68             logger.debug("No Security Groups Have Been Defined");
69             return null;
70         }
71         return securityGroupDB.get(uuid);
72     }
73
74     @Override
75     public List<NeutronSecurityGroup> getAllNeutronSecurityGroups() {
76         Set<NeutronSecurityGroup> allSecurityGroups = new HashSet<NeutronSecurityGroup>();
77         for (Entry<String, NeutronSecurityGroup> entry : securityGroupDB.entrySet()) {
78             NeutronSecurityGroup securityGroup = entry.getValue();
79             allSecurityGroups.add(securityGroup);
80         }
81         logger.debug("Exiting getSecurityGroups, Found {} OpenStackSecurityGroup", allSecurityGroups.size());
82         List<NeutronSecurityGroup> ans = new ArrayList<NeutronSecurityGroup>();
83         ans.addAll(allSecurityGroups);
84         return ans;
85     }
86
87     @Override
88     public boolean addNeutronSecurityGroup(NeutronSecurityGroup input) {
89         if (neutronSecurityGroupExists(input.getSecurityGroupUUID())) {
90             return false;
91         }
92         securityGroupDB.putIfAbsent(input.getSecurityGroupUUID(), input);
93         return true;
94     }
95
96     @Override
97     public boolean removeNeutronSecurityGroup(String uuid) {
98         if (!neutronSecurityGroupExists(uuid)) {
99             return false;
100         }
101         securityGroupDB.remove(uuid);
102         return true;
103     }
104
105     @Override
106     public boolean updateNeutronSecurityGroup(String uuid, NeutronSecurityGroup delta) {
107         if (!neutronSecurityGroupExists(uuid)) {
108             return false;
109         }
110         NeutronSecurityGroup target = securityGroupDB.get(uuid);
111         return overwrite(target, delta);
112     }
113
114     @Override
115     public boolean neutronSecurityGroupInUse(String securityGroupUUID) {
116         return !neutronSecurityGroupExists(securityGroupUUID);
117     }
118
119 }