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