2 * Copyright (c) 2014, 2015 Red Hat, Inc. and others. All rights reserved.
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
9 package org.opendaylight.neutron.transcriber;
11 import java.util.ArrayList;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Map.Entry;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
18 import org.opendaylight.neutron.spi.INeutronSecurityRuleCRUD;
19 import org.opendaylight.neutron.spi.NeutronSecurityRule;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionBase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionEgress;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionIngress;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeBase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV4;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV6;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolBase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolHttp;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolHttps;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolIcmp;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolIcmpV6;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolTcp;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolUdp;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.SecurityRules;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRule;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRuleBuilder;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.osgi.framework.BundleContext;
40 import org.osgi.framework.ServiceRegistration;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 import com.google.common.collect.ImmutableBiMap;
47 public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<SecurityRule, SecurityRules, NeutronSecurityRule> implements INeutronSecurityRuleCRUD {
48 private static final Logger LOGGER = LoggerFactory.getLogger(NeutronSecurityRuleInterface.class);
50 private static final ImmutableBiMap<Class<? extends DirectionBase>,String> DIRECTION_MAP
51 = new ImmutableBiMap.Builder<Class<? extends DirectionBase>,String>()
52 .put(DirectionEgress.class,"egress")
53 .put(DirectionIngress.class,"ingress")
55 private static final ImmutableBiMap<Class<? extends ProtocolBase>,String> PROTOCOL_MAP
56 = new ImmutableBiMap.Builder<Class<? extends ProtocolBase>,String>()
57 .put(ProtocolIcmp.class,"icmp")
58 .put(ProtocolTcp.class,"tcp")
59 .put(ProtocolUdp.class,"udp")
60 .put(ProtocolIcmpV6.class,"icmpv6")
62 private static final ImmutableBiMap<Class<? extends EthertypeBase>,String> ETHERTYPE_MAP
63 = new ImmutableBiMap.Builder<Class<? extends EthertypeBase>,String>()
64 .put(EthertypeV4.class,"IPv4")
65 .put(EthertypeV6.class,"IPv6")
68 NeutronSecurityRuleInterface(ProviderContext providerContext) {
69 super(providerContext);
73 public boolean neutronSecurityRuleExists(String uuid) {
78 public NeutronSecurityRule getNeutronSecurityRule(String uuid) {
83 protected List<SecurityRule> getDataObjectList(SecurityRules rules) {
84 return rules.getSecurityRule();
88 public List<NeutronSecurityRule> getAllNeutronSecurityRules() {
93 public boolean addNeutronSecurityRule(NeutronSecurityRule input) {
98 public boolean removeNeutronSecurityRule(String uuid) {
103 public boolean updateNeutronSecurityRule(String uuid, NeutronSecurityRule delta) {
104 return update(uuid, delta);
108 public boolean neutronSecurityRuleInUse(String securityRuleUUID) {
109 return !exists(securityRuleUUID);
112 protected NeutronSecurityRule fromMd(SecurityRule rule) {
113 NeutronSecurityRule answer = new NeutronSecurityRule();
114 if (rule.getTenantId() != null) {
115 answer.setTenantID(rule.getTenantId());
117 if (rule.getDirection() != null) {
118 answer.setSecurityRuleDirection(DIRECTION_MAP.get(rule.getDirection()));
120 if (rule.getSecurityGroupId() != null) {
121 answer.setSecurityRuleGroupID(rule.getSecurityGroupId().getValue());
123 if (rule.getRemoteGroupId() != null) {
124 answer.setSecurityRemoteGroupID(rule.getRemoteGroupId().getValue());
126 if (rule.getRemoteIpPrefix() != null) {
127 answer.setSecurityRuleRemoteIpPrefix(new String(rule.getRemoteIpPrefix().getValue()));
129 if (rule.getProtocol() != null) {
130 answer.setSecurityRuleProtocol(PROTOCOL_MAP.get(rule.getProtocol()));
132 if (rule.getEthertype() != null) {
133 answer.setSecurityRuleEthertype(ETHERTYPE_MAP.get(rule.getEthertype()));
135 if (rule.getPortRangeMin() != null) {
136 answer.setSecurityRulePortMin(Integer.valueOf(rule.getPortRangeMin()));
138 if (rule.getPortRangeMax() != null) {
139 answer.setSecurityRulePortMax(Integer.valueOf(rule.getPortRangeMax()));
141 if (rule.getId() != null) {
142 answer.setID(rule.getId().getValue());
148 protected SecurityRule toMd(NeutronSecurityRule securityRule) {
149 SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
151 if (securityRule.getTenantID() != null) {
152 securityRuleBuilder.setTenantId(toUuid(securityRule.getTenantID()));
154 if (securityRule.getSecurityRuleDirection() != null) {
155 ImmutableBiMap<String, Class<? extends DirectionBase>> mapper =
156 DIRECTION_MAP.inverse();
157 securityRuleBuilder.setDirection((Class<? extends DirectionBase>) mapper.get(securityRule.getSecurityRuleDirection()));
159 if (securityRule.getSecurityRuleGroupID() != null) {
160 securityRuleBuilder.setSecurityGroupId(toUuid(securityRule.getSecurityRuleGroupID()));
162 if (securityRule.getSecurityRemoteGroupID() != null) {
163 securityRuleBuilder.setRemoteGroupId(toUuid(securityRule.getSecurityRemoteGroupID()));
165 if (securityRule.getSecurityRuleRemoteIpPrefix() != null) {
166 IpPrefix ipPrefix = new IpPrefix(securityRule.getSecurityRuleRemoteIpPrefix().toCharArray());
167 securityRuleBuilder.setRemoteIpPrefix(ipPrefix);
169 if (securityRule.getSecurityRuleProtocol() != null) {
170 ImmutableBiMap<String, Class<? extends ProtocolBase>> mapper =
171 PROTOCOL_MAP.inverse();
172 securityRuleBuilder.setProtocol((Class<? extends ProtocolBase>) mapper.get(securityRule.getSecurityRuleProtocol()));
174 if (securityRule.getSecurityRuleEthertype() != null) {
175 ImmutableBiMap<String, Class<? extends EthertypeBase>> mapper =
176 ETHERTYPE_MAP.inverse();
177 securityRuleBuilder.setEthertype((Class<? extends EthertypeBase>) mapper.get(securityRule.getSecurityRuleEthertype()));
179 if (securityRule.getSecurityRulePortMin() != null) {
180 securityRuleBuilder.setPortRangeMin(Integer.valueOf(securityRule.getSecurityRulePortMin()));
182 if (securityRule.getSecurityRulePortMax() != null) {
183 securityRuleBuilder.setPortRangeMax(Integer.valueOf(securityRule.getSecurityRulePortMax()));
185 if (securityRule.getID() != null) {
186 securityRuleBuilder.setId(toUuid(securityRule.getID()));
188 LOGGER.warn("Attempting to write neutron securityRule without UUID");
190 return securityRuleBuilder.build();
194 protected InstanceIdentifier<SecurityRule> createInstanceIdentifier(SecurityRule securityRule) {
195 return InstanceIdentifier.create(Neutron.class)
196 .child(SecurityRules.class).child(SecurityRule.class,
197 securityRule.getKey());
201 protected InstanceIdentifier<SecurityRules> createInstanceIdentifier() {
202 return InstanceIdentifier.create(Neutron.class)
203 .child(SecurityRules.class);
207 protected SecurityRule toMd(String uuid) {
208 SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
209 securityRuleBuilder.setId(toUuid(uuid));
210 return securityRuleBuilder.build();
213 public static void registerNewInterface(BundleContext context,
214 ProviderContext providerContext,
215 List<ServiceRegistration<?>> registrations) {
216 NeutronSecurityRuleInterface neutronSecurityRuleInterface = new NeutronSecurityRuleInterface(providerContext);
217 ServiceRegistration<INeutronSecurityRuleCRUD> neutronSecurityRuleInterfaceRegistration = context.registerService(INeutronSecurityRuleCRUD.class, neutronSecurityRuleInterface, null);
218 if(neutronSecurityRuleInterfaceRegistration != null) {
219 registrations.add(neutronSecurityRuleInterfaceRegistration);