Merge "Fix logger constant name and remove unused logger constants"
[neutron.git] / transcriber / src / main / java / org / opendaylight / neutron / transcriber / 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.neutron.transcriber;
11
12 import java.lang.reflect.Method;
13 import java.util.ArrayList;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Map.Entry;
17 import java.util.Set;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.ConcurrentMap;
20
21 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
22 import org.opendaylight.neutron.spi.INeutronSecurityRuleCRUD;
23 import org.opendaylight.neutron.spi.NeutronSecurityRule;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150325.Neutron;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev141002.SecurityRuleAttrs;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev141002.security.rules.attributes.SecurityRules;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev141002.security.rules.attributes.security.rules.SecurityRule;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev141002.security.rules.attributes.security.rules.SecurityRuleBuilder;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34
35 public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<SecurityRule, NeutronSecurityRule> implements INeutronSecurityRuleCRUD {
36     private static final Logger LOGGER = LoggerFactory.getLogger(NeutronSecurityRuleInterface.class);
37     private ConcurrentMap<String, NeutronSecurityRule> securityRuleDB = new ConcurrentHashMap<String, NeutronSecurityRule>();
38
39
40     NeutronSecurityRuleInterface(ProviderContext providerContext) {
41         super(providerContext);
42     }
43
44     // this method uses reflection to update an object from it's delta.
45     private boolean overwrite(Object target, Object delta) {
46         Method[] methods = target.getClass().getMethods();
47
48         for (Method toMethod : methods) {
49             if (toMethod.getDeclaringClass().equals(target.getClass())
50                 && toMethod.getName().startsWith("set")) {
51
52                 String toName = toMethod.getName();
53                 String fromName = toName.replace("set", "get");
54
55                 try {
56                     Method fromMethod = delta.getClass().getMethod(fromName);
57                     Object value = fromMethod.invoke(delta, (Object[]) null);
58                     if (value != null) {
59                         toMethod.invoke(target, value);
60                     }
61                 } catch (Exception e) {
62                     LOGGER.error(e.getMessage());
63                     return false;
64                 }
65             }
66         }
67         return true;
68     }
69
70     @Override
71     public boolean neutronSecurityRuleExists(String uuid) {
72         return securityRuleDB.containsKey(uuid);
73     }
74
75     @Override
76     public NeutronSecurityRule getNeutronSecurityRule(String uuid) {
77         if (!neutronSecurityRuleExists(uuid)) {
78             LOGGER.debug("No Security Rules Have Been Defined");
79             return null;
80         }
81         return securityRuleDB.get(uuid);
82     }
83
84     @Override
85     public List<NeutronSecurityRule> getAllNeutronSecurityRules() {
86         Set<NeutronSecurityRule> allSecurityRules = new HashSet<NeutronSecurityRule>();
87         for (Entry<String, NeutronSecurityRule> entry : securityRuleDB.entrySet()) {
88             NeutronSecurityRule securityRule = entry.getValue();
89             allSecurityRules.add(securityRule);
90         }
91         LOGGER.debug("Exiting getSecurityRule, Found {} OpenStackSecurityRule", allSecurityRules.size());
92         List<NeutronSecurityRule> ans = new ArrayList<NeutronSecurityRule>();
93         ans.addAll(allSecurityRules);
94         return ans;
95     }
96
97     @Override
98     public boolean addNeutronSecurityRule(NeutronSecurityRule input) {
99         if (neutronSecurityRuleExists(input.getSecurityRuleUUID())) {
100             return false;
101         }
102         securityRuleDB.putIfAbsent(input.getSecurityRuleUUID(), input);
103         addMd(input);
104         return true;
105     }
106
107     @Override
108     public boolean removeNeutronSecurityRule(String uuid) {
109         if (!neutronSecurityRuleExists(uuid)) {
110             return false;
111         }
112         securityRuleDB.remove(uuid);
113         removeMd(toMd(uuid));
114         return true;
115     }
116
117     @Override
118     public boolean updateNeutronSecurityRule(String uuid, NeutronSecurityRule delta) {
119         if (!neutronSecurityRuleExists(uuid)) {
120             return false;
121         }
122         NeutronSecurityRule target = securityRuleDB.get(uuid);
123         boolean rc = overwrite(target, delta);
124         if (rc) {
125             updateMd(securityRuleDB.get(uuid));
126         }
127         return rc;
128     }
129
130     @Override
131     public boolean neutronSecurityRuleInUse(String securityRuleUUID) {
132         return !neutronSecurityRuleExists(securityRuleUUID);
133     }
134
135     @Override
136     protected SecurityRule toMd(NeutronSecurityRule securityRule) {
137         SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
138
139         if (securityRule.getSecurityRuleTenantID() != null) {
140             securityRuleBuilder.setTenantId(toUuid(securityRule.getSecurityRuleTenantID()));
141         }
142         if (securityRule.getSecurityRuleDirection() != null) {
143             boolean foundMatch = false;
144             for (SecurityRuleAttrs.Direction direction : SecurityRuleAttrs.Direction.values()) {
145                 if (direction.toString().equalsIgnoreCase(securityRule.getSecurityRuleDirection())) {
146                     securityRuleBuilder.setDirection(direction);
147                     foundMatch = true;
148                     break;
149                 }
150             }
151             if (!foundMatch) {
152                 LOGGER.warn("Unable to find direction value for {}", securityRule.getSecurityRuleDirection());
153             }
154         }
155         if (securityRule.getSecurityRuleGroupID() != null) {
156             securityRuleBuilder.setSecurityGroupId(toUuid(securityRule.getSecurityRuleGroupID()));
157         }
158         if (securityRule.getSecurityRemoteGroupID() != null) {
159             securityRuleBuilder.setRemoteGroupId(toUuid(securityRule.getSecurityRemoteGroupID()));
160         }
161         if (securityRule.getSecurityRuleRemoteIpPrefix() != null) {
162             IpAddress ipAddress = new IpAddress(securityRule.getSecurityRuleRemoteIpPrefix().toCharArray());
163             securityRuleBuilder.setRemoteIpPrefix(ipAddress);
164         }
165         if (securityRule.getSecurityRuleProtocol() != null) {
166             boolean foundMatch = false;
167             for (SecurityRuleAttrs.Protocol protocol : SecurityRuleAttrs.Protocol.values()) {
168                 if (protocol.toString().equalsIgnoreCase(securityRule.getSecurityRuleProtocol())) {
169                     securityRuleBuilder.setProtocol(protocol);
170                     foundMatch = true;
171                     break;
172                 }
173             }
174             if (!foundMatch) {
175                 LOGGER.warn("Unable to find protocol value for {}", securityRule.getSecurityRuleProtocol());
176             }
177         }
178         if (securityRule.getSecurityRuleEthertype() != null) {
179             boolean foundMatch = false;
180             for (SecurityRuleAttrs.Ethertype etherType : SecurityRuleAttrs.Ethertype.values()) {
181                 if (etherType.toString().equalsIgnoreCase(securityRule.getSecurityRuleEthertype())) {
182                     securityRuleBuilder.setEthertype(etherType);
183                     foundMatch = true;
184                     break;
185                 }
186             }
187             if (!foundMatch) {
188                 LOGGER.warn("Unable to find ethertype value for {}", securityRule.getSecurityRuleEthertype());
189             }
190         }
191         if (securityRule.getSecurityRulePortMin() != null) {
192             securityRuleBuilder.setPortRangeMin(new Long(securityRule.getSecurityRulePortMin()));
193         }
194         if (securityRule.getSecurityRulePortMax() != null) {
195             securityRuleBuilder.setPortRangeMax(new Long(securityRule.getSecurityRulePortMax()));
196         }
197         if (securityRule.getSecurityRuleUUID() != null) {
198             securityRuleBuilder.setId(toUuid(securityRule.getSecurityRuleUUID()));
199         } else {
200             LOGGER.warn("Attempting to write neutron securityRule without UUID");
201         }
202         return securityRuleBuilder.build();
203     }
204
205     @Override
206     protected InstanceIdentifier<SecurityRule> createInstanceIdentifier(SecurityRule securityRule) {
207         return InstanceIdentifier.create(Neutron.class).child(SecurityRules.class).child(SecurityRule.class);
208     }
209
210     @Override
211     protected SecurityRule toMd(String uuid) {
212         SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
213         securityRuleBuilder.setId(toUuid(uuid));
214         return securityRuleBuilder.build();
215     }
216 }