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.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;
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>();
40 NeutronSecurityRuleInterface(ProviderContext providerContext) {
41 super(providerContext);
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();
48 for (Method toMethod : methods) {
49 if (toMethod.getDeclaringClass().equals(target.getClass())
50 && toMethod.getName().startsWith("set")) {
52 String toName = toMethod.getName();
53 String fromName = toName.replace("set", "get");
56 Method fromMethod = delta.getClass().getMethod(fromName);
57 Object value = fromMethod.invoke(delta, (Object[]) null);
59 toMethod.invoke(target, value);
61 } catch (Exception e) {
62 LOGGER.error(e.getMessage());
71 public boolean neutronSecurityRuleExists(String uuid) {
72 return securityRuleDB.containsKey(uuid);
76 public NeutronSecurityRule getNeutronSecurityRule(String uuid) {
77 if (!neutronSecurityRuleExists(uuid)) {
78 LOGGER.debug("No Security Rules Have Been Defined");
81 return securityRuleDB.get(uuid);
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);
91 LOGGER.debug("Exiting getSecurityRule, Found {} OpenStackSecurityRule", allSecurityRules.size());
92 List<NeutronSecurityRule> ans = new ArrayList<NeutronSecurityRule>();
93 ans.addAll(allSecurityRules);
98 public boolean addNeutronSecurityRule(NeutronSecurityRule input) {
99 if (neutronSecurityRuleExists(input.getSecurityRuleUUID())) {
102 securityRuleDB.putIfAbsent(input.getSecurityRuleUUID(), input);
108 public boolean removeNeutronSecurityRule(String uuid) {
109 if (!neutronSecurityRuleExists(uuid)) {
112 securityRuleDB.remove(uuid);
113 removeMd(toMd(uuid));
118 public boolean updateNeutronSecurityRule(String uuid, NeutronSecurityRule delta) {
119 if (!neutronSecurityRuleExists(uuid)) {
122 NeutronSecurityRule target = securityRuleDB.get(uuid);
123 boolean rc = overwrite(target, delta);
125 updateMd(securityRuleDB.get(uuid));
131 public boolean neutronSecurityRuleInUse(String securityRuleUUID) {
132 return !neutronSecurityRuleExists(securityRuleUUID);
136 protected SecurityRule toMd(NeutronSecurityRule securityRule) {
137 SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
139 if (securityRule.getSecurityRuleTenantID() != null) {
140 securityRuleBuilder.setTenantId(toUuid(securityRule.getSecurityRuleTenantID()));
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);
152 LOGGER.warn("Unable to find direction value for {}", securityRule.getSecurityRuleDirection());
155 if (securityRule.getSecurityRuleGroupID() != null) {
156 securityRuleBuilder.setSecurityGroupId(toUuid(securityRule.getSecurityRuleGroupID()));
158 if (securityRule.getSecurityRemoteGroupID() != null) {
159 securityRuleBuilder.setRemoteGroupId(toUuid(securityRule.getSecurityRemoteGroupID()));
161 if (securityRule.getSecurityRuleRemoteIpPrefix() != null) {
162 IpAddress ipAddress = new IpAddress(securityRule.getSecurityRuleRemoteIpPrefix().toCharArray());
163 securityRuleBuilder.setRemoteIpPrefix(ipAddress);
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);
175 LOGGER.warn("Unable to find protocol value for {}", securityRule.getSecurityRuleProtocol());
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);
188 LOGGER.warn("Unable to find ethertype value for {}", securityRule.getSecurityRuleEthertype());
191 if (securityRule.getSecurityRulePortMin() != null) {
192 securityRuleBuilder.setPortRangeMin(new Long(securityRule.getSecurityRulePortMin()));
194 if (securityRule.getSecurityRulePortMax() != null) {
195 securityRuleBuilder.setPortRangeMax(new Long(securityRule.getSecurityRulePortMax()));
197 if (securityRule.getSecurityRuleUUID() != null) {
198 securityRuleBuilder.setId(toUuid(securityRule.getSecurityRuleUUID()));
200 LOGGER.warn("Attempting to write neutron securityRule without UUID");
202 return securityRuleBuilder.build();
206 protected InstanceIdentifier<SecurityRule> createInstanceIdentifier(SecurityRule securityRule) {
207 return InstanceIdentifier.create(Neutron.class).child(SecurityRules.class).child(SecurityRule.class,
208 securityRule.getKey());
212 protected SecurityRule toMd(String uuid) {
213 SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
214 securityRuleBuilder.setId(toUuid(uuid));
215 return securityRuleBuilder.build();