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