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 java.util.ArrayList;
12 import java.util.HashSet;
13 import java.util.List;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
17 import org.opendaylight.netvirt.openstack.netvirt.translator.crud.INeutronSecurityRuleCRUD;
18 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronSecurityGroup;
19 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronSecurityRule;
20 import org.opendaylight.netvirt.openstack.netvirt.translator.crud.INeutronSecurityGroupCRUD;
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.rev100924.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;
46 import com.google.common.collect.ImmutableBiMap;
49 public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<SecurityRule, NeutronSecurityRule> implements INeutronSecurityRuleCRUD {
51 private static final Logger LOGGER = LoggerFactory.getLogger(NeutronSecurityRuleInterface.class);
53 private static final ImmutableBiMap<Class<? extends DirectionBase>, String> DIRECTION_MAP = ImmutableBiMap.of(
54 DirectionEgress.class, NeutronSecurityRule.DIRECTION_EGRESS,
55 DirectionIngress.class, NeutronSecurityRule.DIRECTION_INGRESS);
56 private static final ImmutableBiMap<Class<? extends ProtocolBase>, String> PROTOCOL_MAP = ImmutableBiMap.of(
57 ProtocolIcmp.class, NeutronSecurityRule.PROTOCOL_ICMP,
58 ProtocolTcp.class, NeutronSecurityRule.PROTOCOL_TCP,
59 ProtocolUdp.class, NeutronSecurityRule.PROTOCOL_UDP,
60 ProtocolIcmpV6.class, NeutronSecurityRule.PROTOCOL_ICMPV6);
61 private static final ImmutableBiMap<Class<? extends EthertypeBase>,String> ETHERTYPE_MAP = ImmutableBiMap.of(
62 EthertypeV4.class, NeutronSecurityRule.ETHERTYPE_IPV4,
63 EthertypeV6.class, NeutronSecurityRule.ETHERTYPE_IPV6);
65 NeutronSecurityRuleInterface(ProviderContext providerContext) {
66 super(providerContext);
70 public boolean neutronSecurityRuleExists(String uuid) {
71 SecurityRule rule = readMd(createInstanceIdentifier(toMd(uuid)));
76 public NeutronSecurityRule getNeutronSecurityRule(String uuid) {
77 SecurityRule rule = readMd(createInstanceIdentifier(toMd(uuid)));
85 public List<NeutronSecurityRule> getAllNeutronSecurityRules() {
86 Set<NeutronSecurityRule> allSecurityRules = new HashSet<>();
87 SecurityRules rules = readMd(createInstanceIdentifier());
89 for (SecurityRule rule: rules.getSecurityRule()) {
90 allSecurityRules.add(fromMd(rule));
93 LOGGER.debug("Exiting getSecurityRule, Found {} OpenStackSecurityRule", allSecurityRules.size());
94 return new ArrayList<>(allSecurityRules);
98 public boolean addNeutronSecurityRule(NeutronSecurityRule input) {
99 if (neutronSecurityRuleExists(input.getID())) {
107 public boolean removeNeutronSecurityRule(String uuid) {
108 if (!neutronSecurityRuleExists(uuid)) {
111 removeMd(toMd(uuid));
116 public boolean updateNeutronSecurityRule(String uuid, NeutronSecurityRule delta) {
117 if (!neutronSecurityRuleExists(uuid)) {
125 public boolean neutronSecurityRuleInUse(String securityRuleUUID) {
126 return !neutronSecurityRuleExists(securityRuleUUID);
129 protected NeutronSecurityRule fromMd(SecurityRule rule) {
130 NeutronSecurityRule answer = new NeutronSecurityRule();
131 if (rule.getTenantId() != null) {
132 answer.setSecurityRuleTenantID(rule.getTenantId().getValue().replace("-",""));
134 if (rule.getDirection() != null) {
135 answer.setSecurityRuleDirection(DIRECTION_MAP.get(rule.getDirection()));
137 if (rule.getSecurityGroupId() != null) {
138 answer.setSecurityRuleGroupID(rule.getSecurityGroupId().getValue());
140 if (rule.getRemoteGroupId() != null) {
141 answer.setSecurityRemoteGroupID(rule.getRemoteGroupId().getValue());
143 if (rule.getRemoteIpPrefix() != null) {
144 answer.setSecurityRuleRemoteIpPrefix(rule.getRemoteIpPrefix().getIpv4Prefix() != null?
145 rule.getRemoteIpPrefix().getIpv4Prefix().getValue():rule.getRemoteIpPrefix().getIpv6Prefix().getValue());
147 if (rule.getProtocol() != null) {
148 SecurityRuleAttributes.Protocol protocol = rule.getProtocol();
149 if (protocol.getUint8() != null) {
151 answer.setSecurityRuleProtocol(protocol.getUint8().toString());
153 // symbolic protocol name
154 answer.setSecurityRuleProtocol(NeutronUtils.ProtocolMapper.getName(protocol.getIdentityref()));
157 if (rule.getEthertype() != null) {
158 answer.setSecurityRuleEthertype(ETHERTYPE_MAP.get(rule.getEthertype()));
160 if (rule.getPortRangeMin() != null) {
161 answer.setSecurityRulePortMin(rule.getPortRangeMin());
163 if (rule.getPortRangeMax() != null) {
164 answer.setSecurityRulePortMax(rule.getPortRangeMax());
166 if (rule.getUuid() != null) {
167 answer.setID(rule.getUuid().getValue());
173 protected SecurityRule toMd(NeutronSecurityRule securityRule) {
174 SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
176 if (securityRule.getSecurityRuleTenantID() != null) {
177 securityRuleBuilder.setTenantId(toUuid(securityRule.getSecurityRuleTenantID()));
179 if (securityRule.getSecurityRuleDirection() != null) {
180 ImmutableBiMap<String, Class<? extends DirectionBase>> mapper =
181 DIRECTION_MAP.inverse();
182 securityRuleBuilder.setDirection(mapper.get(securityRule.getSecurityRuleDirection()));
184 if (securityRule.getSecurityRuleGroupID() != null) {
185 securityRuleBuilder.setSecurityGroupId(toUuid(securityRule.getSecurityRuleGroupID()));
187 if (securityRule.getSecurityRemoteGroupID() != null) {
188 securityRuleBuilder.setRemoteGroupId(toUuid(securityRule.getSecurityRemoteGroupID()));
190 if (securityRule.getSecurityRuleRemoteIpPrefix() != null) {
191 securityRuleBuilder.setRemoteIpPrefix(new IpPrefix(securityRule.getSecurityRuleRemoteIpPrefix().toCharArray()));
193 if (securityRule.getSecurityRuleProtocol() != null) {
194 String protocolString = securityRule.getSecurityRuleProtocol();
195 SecurityRuleAttributes.Protocol protocol = new SecurityRuleAttributes.Protocol(protocolString.toCharArray());
196 securityRuleBuilder.setProtocol(protocol);
198 if (securityRule.getSecurityRuleEthertype() != null) {
199 ImmutableBiMap<String, Class<? extends EthertypeBase>> mapper =
200 ETHERTYPE_MAP.inverse();
201 securityRuleBuilder.setEthertype(mapper.get(securityRule.getSecurityRuleEthertype()));
203 if (securityRule.getSecurityRulePortMin() != null) {
204 securityRuleBuilder.setPortRangeMin(securityRule.getSecurityRulePortMin());
206 if (securityRule.getSecurityRulePortMax() != null) {
207 securityRuleBuilder.setPortRangeMax(securityRule.getSecurityRulePortMax());
209 if (securityRule.getID() != null) {
210 securityRuleBuilder.setUuid(toUuid(securityRule.getID()));
212 LOGGER.warn("Attempting to write neutron securityRule without UUID");
214 return securityRuleBuilder.build();
218 protected InstanceIdentifier<SecurityRule> createInstanceIdentifier(SecurityRule securityRule) {
219 return InstanceIdentifier.create(Neutron.class)
220 .child(SecurityRules.class).child(SecurityRule.class,
221 securityRule.getKey());
224 protected InstanceIdentifier<SecurityRules> createInstanceIdentifier() {
225 return InstanceIdentifier.create(Neutron.class)
226 .child(SecurityRules.class);
230 protected SecurityRule toMd(String uuid) {
231 SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
232 securityRuleBuilder.setUuid(toUuid(uuid));
233 return securityRuleBuilder.build();
236 public static void registerNewInterface(BundleContext context,
237 ProviderContext providerContext,
238 List<ServiceRegistration<?>> registrations) {
239 NeutronSecurityRuleInterface neutronSecurityRuleInterface = new NeutronSecurityRuleInterface(providerContext);
240 ServiceRegistration<INeutronSecurityRuleCRUD> neutronSecurityRuleInterfaceRegistration = context.registerService(INeutronSecurityRuleCRUD.class, neutronSecurityRuleInterface, null);
241 if(neutronSecurityRuleInterfaceRegistration != null) {
242 registrations.add(neutronSecurityRuleInterfaceRegistration);