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 com.google.common.collect.ImmutableBiMap;
12 import java.util.List;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
17 import org.opendaylight.neutron.northbound.api.BadRequestException;
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.rev130715.IpPrefix;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeBase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV4;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV6;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.NeutronUtils.DirectionMapper;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.NeutronUtils.ProtocolMapper;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.SecurityRuleAttributes.Protocol;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.SecurityRules;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRule;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRuleBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRuleKey;
31 import org.ops4j.pax.cdi.api.OsgiServiceProvider;
34 @OsgiServiceProvider(classes = INeutronSecurityRuleCRUD.class)
35 public final class NeutronSecurityRuleInterface extends
36 AbstractNeutronInterface<SecurityRule, SecurityRules, SecurityRuleKey, NeutronSecurityRule>
37 implements INeutronSecurityRuleCRUD {
39 private static final ImmutableBiMap<Class<? extends EthertypeBase>,
40 String> ETHERTYPE_MAP = new ImmutableBiMap.Builder<Class<? extends EthertypeBase>, String>()
41 .put(EthertypeV4.class, "IPv4").put(EthertypeV6.class, "IPv6").build();
43 private final NeutronSecurityGroupInterface securityGroupInterface;
46 public NeutronSecurityRuleInterface(DataBroker db, NeutronSecurityGroupInterface securityGroupInterface) {
47 super(SecurityRuleBuilder.class, db);
48 this.securityGroupInterface = securityGroupInterface;
52 protected List<SecurityRule> getDataObjectList(SecurityRules rules) {
53 return rules.getSecurityRule();
57 protected NeutronSecurityRule fromMd(SecurityRule rule) {
58 final NeutronSecurityRule answer = new NeutronSecurityRule();
59 fromMdIds(rule, answer);
60 if (rule.getDirection() != null) {
61 answer.setSecurityRuleDirection(DirectionMapper.getDirectionString(rule.getDirection()));
63 if (rule.getSecurityGroupId() != null) {
64 answer.setSecurityRuleGroupID(rule.getSecurityGroupId().getValue());
66 if (rule.getRemoteGroupId() != null) {
67 answer.setSecurityRemoteGroupID(rule.getRemoteGroupId().getValue());
69 if (rule.getRemoteIpPrefix() != null) {
70 answer.setSecurityRuleRemoteIpPrefix(new String(rule.getRemoteIpPrefix().getValue()));
72 if (rule.getProtocol() != null) {
73 final Protocol protocol = rule.getProtocol();
74 if (protocol.getUint8() != null) {
76 answer.setSecurityRuleProtocol(protocol.getUint8().toString());
78 // symbolic protocol name
79 answer.setSecurityRuleProtocol(ProtocolMapper.getName(protocol.getIdentityref()));
82 if (rule.getEthertype() != null) {
83 answer.setSecurityRuleEthertype(ETHERTYPE_MAP.get(rule.getEthertype()));
85 if (rule.getPortRangeMin() != null) {
86 answer.setSecurityRulePortMin(rule.getPortRangeMin());
88 if (rule.getPortRangeMax() != null) {
89 answer.setSecurityRulePortMax(rule.getPortRangeMax());
95 @SuppressWarnings("checkstyle:AvoidHidingCauseException")
96 protected SecurityRule toMd(NeutronSecurityRule securityRule) {
97 final SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
98 toMdIds(securityRule, securityRuleBuilder);
99 if (securityRule.getSecurityRuleDirection() != null) {
101 .setDirection(DirectionMapper.get(securityRule.getSecurityRuleDirection()));
103 if (securityRule.getSecurityRuleGroupID() != null) {
104 securityRuleBuilder.setSecurityGroupId(toUuid(securityRule.getSecurityRuleGroupID()));
106 if (securityRule.getSecurityRemoteGroupID() != null) {
107 securityRuleBuilder.setRemoteGroupId(toUuid(securityRule.getSecurityRemoteGroupID()));
109 if (securityRule.getSecurityRuleRemoteIpPrefix() != null) {
110 final IpPrefix ipPrefix = new IpPrefix(securityRule.getSecurityRuleRemoteIpPrefix().toCharArray());
111 securityRuleBuilder.setRemoteIpPrefix(ipPrefix);
113 if (securityRule.getSecurityRuleProtocol() != null) {
114 final String protocolString = securityRule.getSecurityRuleProtocol();
116 final Protocol protocol = new Protocol(protocolString.toCharArray());
117 securityRuleBuilder.setProtocol(protocol);
118 } catch (NumberFormatException e) {
119 throw new BadRequestException("Protocol {" + securityRule.getSecurityRuleProtocol()
120 + "} is not supported");
123 if (securityRule.getSecurityRuleEthertype() != null) {
124 final ImmutableBiMap<String, Class<? extends EthertypeBase>> mapper = ETHERTYPE_MAP.inverse();
126 .setEthertype(mapper.get(securityRule.getSecurityRuleEthertype()));
128 if (securityRule.getSecurityRulePortMin() != null) {
129 securityRuleBuilder.setPortRangeMin(securityRule.getSecurityRulePortMin());
131 if (securityRule.getSecurityRulePortMax() != null) {
132 securityRuleBuilder.setPortRangeMax(securityRule.getSecurityRulePortMax());
134 return securityRuleBuilder.build();
138 protected boolean areAllDependenciesAvailable(ReadTransaction tx, NeutronSecurityRule securityRule) {
139 return securityGroupInterface.exists(securityRule.getSecurityRuleGroupID(), tx)
140 && ifNonNull(securityRule.getSecurityRemoteGroupID(),
141 remoteGroupID -> securityGroupInterface.exists(remoteGroupID, tx));