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.lang.reflect.Method;
13 import java.util.ArrayList;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Map.Entry;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.ConcurrentMap;
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;
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>();
43 NeutronSecurityRuleInterface(ProviderContext providerContext) {
44 super(providerContext);
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();
51 for (Method toMethod : methods) {
52 if (toMethod.getDeclaringClass().equals(target.getClass())
53 && toMethod.getName().startsWith("set")) {
55 String toName = toMethod.getName();
56 String fromName = toName.replace("set", "get");
59 Method fromMethod = delta.getClass().getMethod(fromName);
60 Object value = fromMethod.invoke(delta, (Object[]) null);
62 toMethod.invoke(target, value);
64 } catch (Exception e) {
65 LOGGER.error(e.getMessage());
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);
85 sg.getSecurityRules().add(input);
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())) {
99 sg.getSecurityRules().removeAll(toRemove);
104 public boolean neutronSecurityRuleExists(String uuid) {
105 return securityRuleDB.containsKey(uuid);
109 public NeutronSecurityRule getNeutronSecurityRule(String uuid) {
110 if (!neutronSecurityRuleExists(uuid)) {
111 LOGGER.debug("No Security Rules Have Been Defined");
114 return securityRuleDB.get(uuid);
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);
124 LOGGER.debug("Exiting getSecurityRule, Found {} OpenStackSecurityRule", allSecurityRules.size());
125 List<NeutronSecurityRule> ans = new ArrayList<NeutronSecurityRule>();
126 ans.addAll(allSecurityRules);
131 public boolean addNeutronSecurityRule(NeutronSecurityRule input) {
132 if (neutronSecurityRuleExists(input.getSecurityRuleUUID())) {
135 securityRuleDB.putIfAbsent(input.getSecurityRuleUUID(), input);
136 updateSecGroupRuleInSecurityGroup(input);
142 public boolean removeNeutronSecurityRule(String uuid) {
143 if (!neutronSecurityRuleExists(uuid)) {
146 removeSecGroupRuleFromSecurityGroup(securityRuleDB.get(uuid));
147 securityRuleDB.remove(uuid);
148 removeMd(toMd(uuid));
153 public boolean updateNeutronSecurityRule(String uuid, NeutronSecurityRule delta) {
154 if (!neutronSecurityRuleExists(uuid)) {
157 NeutronSecurityRule target = securityRuleDB.get(uuid);
158 boolean rc = overwrite(target, delta);
159 updateSecGroupRuleInSecurityGroup(securityRuleDB.get(uuid));
161 updateMd(securityRuleDB.get(uuid));
167 public boolean neutronSecurityRuleInUse(String securityRuleUUID) {
168 return !neutronSecurityRuleExists(securityRuleUUID);
172 protected SecurityRule toMd(NeutronSecurityRule securityRule) {
173 SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
175 if (securityRule.getSecurityRuleTenantID() != null) {
176 securityRuleBuilder.setTenantId(toUuid(securityRule.getSecurityRuleTenantID()));
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);
188 LOGGER.warn("Unable to find direction value for {}", securityRule.getSecurityRuleDirection());
191 if (securityRule.getSecurityRuleGroupID() != null) {
192 securityRuleBuilder.setSecurityGroupId(toUuid(securityRule.getSecurityRuleGroupID()));
194 if (securityRule.getSecurityRemoteGroupID() != null) {
195 securityRuleBuilder.setRemoteGroupId(toUuid(securityRule.getSecurityRemoteGroupID()));
197 if (securityRule.getSecurityRuleRemoteIpPrefix() != null) {
198 IpAddress ipAddress = new IpAddress(securityRule.getSecurityRuleRemoteIpPrefix().toCharArray());
199 securityRuleBuilder.setRemoteIpPrefix(ipAddress);
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);
211 LOGGER.warn("Unable to find protocol value for {}", securityRule.getSecurityRuleProtocol());
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);
224 LOGGER.warn("Unable to find ethertype value for {}", securityRule.getSecurityRuleEthertype());
227 if (securityRule.getSecurityRulePortMin() != null) {
228 securityRuleBuilder.setPortRangeMin(new Long(securityRule.getSecurityRulePortMin()));
230 if (securityRule.getSecurityRulePortMax() != null) {
231 securityRuleBuilder.setPortRangeMax(new Long(securityRule.getSecurityRulePortMax()));
233 if (securityRule.getSecurityRuleUUID() != null) {
234 securityRuleBuilder.setId(toUuid(securityRule.getSecurityRuleUUID()));
236 LOGGER.warn("Attempting to write neutron securityRule without UUID");
238 return securityRuleBuilder.build();
242 protected InstanceIdentifier<SecurityRule> createInstanceIdentifier(SecurityRule securityRule) {
243 return InstanceIdentifier.create(Neutron.class).child(SecurityRules.class).child(SecurityRule.class,
244 securityRule.getKey());
248 protected SecurityRule toMd(String uuid) {
249 SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
250 securityRuleBuilder.setId(toUuid(uuid));
251 return securityRuleBuilder.build();