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.netvirt.openstack.netvirt.translator.crud.impl;
11 import com.google.common.collect.ImmutableBiMap;
12 import java.util.ArrayList;
13 import java.util.HashSet;
14 import java.util.List;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronSecurityGroup;
18 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronSecurityRule;
19 import org.opendaylight.netvirt.openstack.netvirt.translator.crud.INeutronSecurityGroupCRUD;
20 import org.opendaylight.netvirt.openstack.netvirt.translator.crud.INeutronSecurityRuleCRUD;
21 import org.opendaylight.netvirt.openstack.netvirt.translator.crud.NeutronCRUDInterfaces;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionBase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionEgress;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionIngress;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeBase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV4;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV6;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolBase;
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.constants.rev150712.NeutronUtils;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.SecurityRuleAttributes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.SecurityRules;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRule;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRuleBuilder;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.osgi.framework.BundleContext;
42 import org.osgi.framework.ServiceRegistration;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<SecurityRule, NeutronSecurityRule> implements INeutronSecurityRuleCRUD {
49 private static final Logger LOG = LoggerFactory.getLogger(NeutronSecurityRuleInterface.class);
51 private static final ImmutableBiMap<Class<? extends DirectionBase>, String> DIRECTION_MAP = ImmutableBiMap.of(
52 DirectionEgress.class, NeutronSecurityRule.DIRECTION_EGRESS,
53 DirectionIngress.class, NeutronSecurityRule.DIRECTION_INGRESS);
54 private static final ImmutableBiMap<Class<? extends ProtocolBase>, String> PROTOCOL_MAP = ImmutableBiMap.of(
55 ProtocolIcmp.class, NeutronSecurityRule.PROTOCOL_ICMP,
56 ProtocolTcp.class, NeutronSecurityRule.PROTOCOL_TCP,
57 ProtocolUdp.class, NeutronSecurityRule.PROTOCOL_UDP,
58 ProtocolIcmpV6.class, NeutronSecurityRule.PROTOCOL_ICMPV6);
59 private static final ImmutableBiMap<Class<? extends EthertypeBase>,String> ETHERTYPE_MAP = ImmutableBiMap.of(
60 EthertypeV4.class, NeutronSecurityRule.ETHERTYPE_IPV4,
61 EthertypeV6.class, NeutronSecurityRule.ETHERTYPE_IPV6);
63 NeutronSecurityRuleInterface(final DataBroker dataBroker) {
68 public boolean neutronSecurityRuleExists(String uuid) {
69 SecurityRule rule = readMd(createInstanceIdentifier(toMd(uuid)));
74 public NeutronSecurityRule getNeutronSecurityRule(String uuid) {
75 SecurityRule rule = readMd(createInstanceIdentifier(toMd(uuid)));
83 public List<NeutronSecurityRule> getAllNeutronSecurityRules() {
84 Set<NeutronSecurityRule> allSecurityRules = new HashSet<>();
85 SecurityRules rules = readMd(createInstanceIdentifier());
87 for (SecurityRule rule: rules.getSecurityRule()) {
88 allSecurityRules.add(fromMd(rule));
91 LOG.debug("Exiting getSecurityRule, Found {} OpenStackSecurityRule", allSecurityRules.size());
92 return new ArrayList<>(allSecurityRules);
96 public boolean addNeutronSecurityRule(NeutronSecurityRule input) {
97 if (neutronSecurityRuleExists(input.getID())) {
105 public boolean removeNeutronSecurityRule(String uuid) {
106 if (!neutronSecurityRuleExists(uuid)) {
109 removeMd(toMd(uuid));
114 public boolean updateNeutronSecurityRule(String uuid, NeutronSecurityRule delta) {
115 if (!neutronSecurityRuleExists(uuid)) {
123 public boolean neutronSecurityRuleInUse(String securityRuleUUID) {
124 return !neutronSecurityRuleExists(securityRuleUUID);
127 protected NeutronSecurityRule fromMd(SecurityRule rule) {
128 NeutronSecurityRule answer = new NeutronSecurityRule();
129 if (rule.getTenantId() != null) {
130 answer.setSecurityRuleTenantID(rule.getTenantId().getValue().replace("-",""));
132 if (rule.getDirection() != null) {
133 answer.setSecurityRuleDirection(DIRECTION_MAP.get(rule.getDirection()));
135 if (rule.getSecurityGroupId() != null) {
136 answer.setSecurityRuleGroupID(rule.getSecurityGroupId().getValue());
138 if (rule.getRemoteGroupId() != null) {
139 answer.setSecurityRemoteGroupID(rule.getRemoteGroupId().getValue());
141 if (rule.getRemoteIpPrefix() != null) {
142 answer.setSecurityRuleRemoteIpPrefix(rule.getRemoteIpPrefix().getIpv4Prefix() != null?
143 rule.getRemoteIpPrefix().getIpv4Prefix().getValue():rule.getRemoteIpPrefix().getIpv6Prefix().getValue());
145 if (rule.getProtocol() != null) {
146 SecurityRuleAttributes.Protocol protocol = rule.getProtocol();
147 if (protocol.getUint8() != null) {
149 answer.setSecurityRuleProtocol(protocol.getUint8().toString());
151 // symbolic protocol name
152 answer.setSecurityRuleProtocol(NeutronUtils.ProtocolMapper.getName(protocol.getIdentityref()));
155 if (rule.getEthertype() != null) {
156 answer.setSecurityRuleEthertype(ETHERTYPE_MAP.get(rule.getEthertype()));
158 if (rule.getPortRangeMin() != null) {
159 answer.setSecurityRulePortMin(rule.getPortRangeMin());
161 if (rule.getPortRangeMax() != null) {
162 answer.setSecurityRulePortMax(rule.getPortRangeMax());
164 if (rule.getUuid() != null) {
165 answer.setID(rule.getUuid().getValue());
171 protected SecurityRule toMd(NeutronSecurityRule securityRule) {
172 SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
174 if (securityRule.getSecurityRuleTenantID() != null) {
175 securityRuleBuilder.setTenantId(toUuid(securityRule.getSecurityRuleTenantID()));
177 if (securityRule.getSecurityRuleDirection() != null) {
178 ImmutableBiMap<String, Class<? extends DirectionBase>> mapper =
179 DIRECTION_MAP.inverse();
180 securityRuleBuilder.setDirection(mapper.get(securityRule.getSecurityRuleDirection()));
182 if (securityRule.getSecurityRuleGroupID() != null) {
183 securityRuleBuilder.setSecurityGroupId(toUuid(securityRule.getSecurityRuleGroupID()));
185 if (securityRule.getSecurityRemoteGroupID() != null) {
186 securityRuleBuilder.setRemoteGroupId(toUuid(securityRule.getSecurityRemoteGroupID()));
188 if (securityRule.getSecurityRuleRemoteIpPrefix() != null) {
189 securityRuleBuilder.setRemoteIpPrefix(new IpPrefix(securityRule.getSecurityRuleRemoteIpPrefix().toCharArray()));
191 if (securityRule.getSecurityRuleProtocol() != null) {
192 String protocolString = securityRule.getSecurityRuleProtocol();
193 SecurityRuleAttributes.Protocol protocol = new SecurityRuleAttributes.Protocol(protocolString.toCharArray());
194 securityRuleBuilder.setProtocol(protocol);
196 if (securityRule.getSecurityRuleEthertype() != null) {
197 ImmutableBiMap<String, Class<? extends EthertypeBase>> mapper =
198 ETHERTYPE_MAP.inverse();
199 securityRuleBuilder.setEthertype(mapper.get(securityRule.getSecurityRuleEthertype()));
201 if (securityRule.getSecurityRulePortMin() != null) {
202 securityRuleBuilder.setPortRangeMin(securityRule.getSecurityRulePortMin());
204 if (securityRule.getSecurityRulePortMax() != null) {
205 securityRuleBuilder.setPortRangeMax(securityRule.getSecurityRulePortMax());
207 if (securityRule.getID() != null) {
208 securityRuleBuilder.setUuid(toUuid(securityRule.getID()));
210 LOG.warn("Attempting to write neutron securityRule without UUID");
212 return securityRuleBuilder.build();
216 protected InstanceIdentifier<SecurityRule> createInstanceIdentifier(SecurityRule securityRule) {
217 return InstanceIdentifier.create(Neutron.class)
218 .child(SecurityRules.class).child(SecurityRule.class,
219 securityRule.getKey());
222 protected InstanceIdentifier<SecurityRules> createInstanceIdentifier() {
223 return InstanceIdentifier.create(Neutron.class)
224 .child(SecurityRules.class);
228 protected SecurityRule toMd(String uuid) {
229 SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
230 securityRuleBuilder.setUuid(toUuid(uuid));
231 return securityRuleBuilder.build();
234 public static void registerNewInterface(BundleContext context,
235 final DataBroker dataBroker,
236 List<ServiceRegistration<?>> registrations) {
237 NeutronSecurityRuleInterface neutronSecurityRuleInterface = new NeutronSecurityRuleInterface(dataBroker);
238 ServiceRegistration<INeutronSecurityRuleCRUD> neutronSecurityRuleInterfaceRegistration = context.registerService(INeutronSecurityRuleCRUD.class, neutronSecurityRuleInterface, null);
239 if(neutronSecurityRuleInterfaceRegistration != null) {
240 registrations.add(neutronSecurityRuleInterfaceRegistration);