2 * Copyright (C) 2014 Red Hat, Inc.
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
10 package org.opendaylight.neutron.transcriber;
12 import java.util.ArrayList;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Map.Entry;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
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;
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>();
42 NeutronSecurityRuleInterface(ProviderContext providerContext) {
43 super(providerContext);
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);
60 sg.getSecurityRules().add(input);
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())) {
76 sg.getSecurityRules().removeAll(toRemove);
81 public boolean neutronSecurityRuleExists(String uuid) {
82 return securityRuleDB.containsKey(uuid);
86 public NeutronSecurityRule getNeutronSecurityRule(String uuid) {
87 if (!neutronSecurityRuleExists(uuid)) {
88 LOGGER.debug("No Security Rules Have Been Defined");
91 return securityRuleDB.get(uuid);
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);
101 LOGGER.debug("Exiting getSecurityRule, Found {} OpenStackSecurityRule", allSecurityRules.size());
102 List<NeutronSecurityRule> ans = new ArrayList<NeutronSecurityRule>();
103 ans.addAll(allSecurityRules);
108 public boolean addNeutronSecurityRule(NeutronSecurityRule input) {
109 if (neutronSecurityRuleExists(input.getSecurityRuleUUID())) {
112 securityRuleDB.putIfAbsent(input.getSecurityRuleUUID(), input);
113 updateSecGroupRuleInSecurityGroup(input);
119 public boolean removeNeutronSecurityRule(String uuid) {
120 if (!neutronSecurityRuleExists(uuid)) {
123 removeSecGroupRuleFromSecurityGroup(securityRuleDB.get(uuid));
124 securityRuleDB.remove(uuid);
125 removeMd(toMd(uuid));
130 public boolean updateNeutronSecurityRule(String uuid, NeutronSecurityRule delta) {
131 if (!neutronSecurityRuleExists(uuid)) {
134 NeutronSecurityRule target = securityRuleDB.get(uuid);
135 boolean rc = overwrite(target, delta);
136 updateSecGroupRuleInSecurityGroup(securityRuleDB.get(uuid));
138 updateMd(securityRuleDB.get(uuid));
144 public boolean neutronSecurityRuleInUse(String securityRuleUUID) {
145 return !neutronSecurityRuleExists(securityRuleUUID);
149 protected SecurityRule toMd(NeutronSecurityRule securityRule) {
150 SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
152 if (securityRule.getSecurityRuleTenantID() != null) {
153 securityRuleBuilder.setTenantId(toUuid(securityRule.getSecurityRuleTenantID()));
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);
165 LOGGER.warn("Unable to find direction value for {}", securityRule.getSecurityRuleDirection());
168 if (securityRule.getSecurityRuleGroupID() != null) {
169 securityRuleBuilder.setSecurityGroupId(toUuid(securityRule.getSecurityRuleGroupID()));
171 if (securityRule.getSecurityRemoteGroupID() != null) {
172 securityRuleBuilder.setRemoteGroupId(toUuid(securityRule.getSecurityRemoteGroupID()));
174 if (securityRule.getSecurityRuleRemoteIpPrefix() != null) {
175 IpAddress ipAddress = new IpAddress(securityRule.getSecurityRuleRemoteIpPrefix().toCharArray());
176 securityRuleBuilder.setRemoteIpPrefix(ipAddress);
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));
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());
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);
206 LOGGER.warn("Unable to find ethertype value for {}", securityRule.getSecurityRuleEthertype());
209 if (securityRule.getSecurityRulePortMin() != null) {
210 securityRuleBuilder.setPortRangeMin(new Integer(securityRule.getSecurityRulePortMin()));
212 if (securityRule.getSecurityRulePortMax() != null) {
213 securityRuleBuilder.setPortRangeMax(new Integer(securityRule.getSecurityRulePortMax()));
215 if (securityRule.getSecurityRuleUUID() != null) {
216 securityRuleBuilder.setId(toUuid(securityRule.getSecurityRuleUUID()));
218 LOGGER.warn("Attempting to write neutron securityRule without UUID");
220 return securityRuleBuilder.build();
224 protected InstanceIdentifier<SecurityRule> createInstanceIdentifier(SecurityRule securityRule) {
225 return InstanceIdentifier.create(Neutron.class).child(SecurityRules.class).child(SecurityRule.class,
226 securityRule.getKey());
230 protected SecurityRule toMd(String uuid) {
231 SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
232 securityRuleBuilder.setId(toUuid(uuid));
233 return securityRuleBuilder.build();