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.controller.md.sal.common.api.data.ReadFailedException;
18 import org.opendaylight.neutron.northbound.api.BadRequestException;
19 import org.opendaylight.neutron.spi.INeutronSecurityRuleCRUD;
20 import org.opendaylight.neutron.spi.NeutronSecurityRule;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeBase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV4;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV6;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.NeutronUtils.DirectionMapper;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.NeutronUtils.ProtocolMapper;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.SecurityRuleAttributes.Protocol;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.SecurityRules;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRule;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRuleBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRuleKey;
32 import org.ops4j.pax.cdi.api.OsgiServiceProvider;
35 @OsgiServiceProvider(classes = INeutronSecurityRuleCRUD.class)
36 public final class NeutronSecurityRuleInterface extends
37 AbstractNeutronInterface<SecurityRule, SecurityRules, SecurityRuleKey, NeutronSecurityRule>
38 implements INeutronSecurityRuleCRUD {
40 private static final ImmutableBiMap<Class<? extends EthertypeBase>,
41 String> ETHERTYPE_MAP = new ImmutableBiMap.Builder<Class<? extends EthertypeBase>, String>()
42 .put(EthertypeV4.class, "IPv4").put(EthertypeV6.class, "IPv6").build();
44 private final NeutronSecurityGroupInterface securityGroupInterface;
47 public NeutronSecurityRuleInterface(DataBroker db, NeutronSecurityGroupInterface securityGroupInterface) {
48 super(SecurityRuleBuilder.class, db);
49 this.securityGroupInterface = securityGroupInterface;
53 protected List<SecurityRule> getDataObjectList(SecurityRules rules) {
54 return rules.getSecurityRule();
58 protected NeutronSecurityRule fromMd(SecurityRule rule) {
59 final NeutronSecurityRule answer = new NeutronSecurityRule();
60 fromMdIds(rule, answer);
61 if (rule.getDirection() != null) {
62 answer.setSecurityRuleDirection(DirectionMapper.getDirectionString(rule.getDirection()));
64 if (rule.getSecurityGroupId() != null) {
65 answer.setSecurityRuleGroupID(rule.getSecurityGroupId().getValue());
67 if (rule.getRemoteGroupId() != null) {
68 answer.setSecurityRemoteGroupID(rule.getRemoteGroupId().getValue());
70 if (rule.getRemoteIpPrefix() != null) {
71 answer.setSecurityRuleRemoteIpPrefix(new String(rule.getRemoteIpPrefix().getValue()));
73 if (rule.getProtocol() != null) {
74 final Protocol protocol = rule.getProtocol();
75 if (protocol.getUint8() != null) {
77 answer.setSecurityRuleProtocol(protocol.getUint8().toString());
79 // symbolic protocol name
80 answer.setSecurityRuleProtocol(ProtocolMapper.getName(protocol.getIdentityref()));
83 if (rule.getEthertype() != null) {
84 answer.setSecurityRuleEthertype(ETHERTYPE_MAP.get(rule.getEthertype()));
86 if (rule.getPortRangeMin() != null) {
87 answer.setSecurityRulePortMin(rule.getPortRangeMin());
89 if (rule.getPortRangeMax() != null) {
90 answer.setSecurityRulePortMax(rule.getPortRangeMax());
96 @SuppressWarnings("checkstyle:AvoidHidingCauseException")
97 protected SecurityRule toMd(NeutronSecurityRule securityRule) {
98 final SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
99 toMdIds(securityRule, securityRuleBuilder);
100 if (securityRule.getSecurityRuleDirection() != null) {
102 .setDirection(DirectionMapper.get(securityRule.getSecurityRuleDirection()));
104 if (securityRule.getSecurityRuleGroupID() != null) {
105 securityRuleBuilder.setSecurityGroupId(toUuid(securityRule.getSecurityRuleGroupID()));
107 if (securityRule.getSecurityRemoteGroupID() != null) {
108 securityRuleBuilder.setRemoteGroupId(toUuid(securityRule.getSecurityRemoteGroupID()));
110 if (securityRule.getSecurityRuleRemoteIpPrefix() != null) {
111 final IpPrefix ipPrefix = new IpPrefix(securityRule.getSecurityRuleRemoteIpPrefix().toCharArray());
112 securityRuleBuilder.setRemoteIpPrefix(ipPrefix);
114 if (securityRule.getSecurityRuleProtocol() != null) {
115 final String protocolString = securityRule.getSecurityRuleProtocol();
117 final Protocol protocol = new Protocol(protocolString.toCharArray());
118 securityRuleBuilder.setProtocol(protocol);
119 } catch (NumberFormatException e) {
120 throw new BadRequestException("Protocol {" + securityRule.getSecurityRuleProtocol()
121 + "} is not supported");
124 if (securityRule.getSecurityRuleEthertype() != null) {
125 final ImmutableBiMap<String, Class<? extends EthertypeBase>> mapper = ETHERTYPE_MAP.inverse();
127 .setEthertype(mapper.get(securityRule.getSecurityRuleEthertype()));
129 if (securityRule.getSecurityRulePortMin() != null) {
130 securityRuleBuilder.setPortRangeMin(securityRule.getSecurityRulePortMin());
132 if (securityRule.getSecurityRulePortMax() != null) {
133 securityRuleBuilder.setPortRangeMax(securityRule.getSecurityRulePortMax());
135 return securityRuleBuilder.build();
139 protected boolean areAllDependenciesAvailable(ReadTransaction tx, NeutronSecurityRule securityRule)
140 throws ReadFailedException {
141 return ifNonNull(securityRule.getSecurityRuleGroupID(),
142 groupID -> securityGroupInterface.exists(groupID, tx))
143 && ifNonNull(securityRule.getSecurityRemoteGroupID(),
144 remoteGroupID -> securityGroupInterface.exists(remoteGroupID, tx));