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.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefixBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeBase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV4;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV6;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.NeutronUtils.DirectionMapper;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.NeutronUtils.ProtocolMapper;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.SecurityRuleAttributes.Protocol;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.SecurityRuleAttributesProtocolBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.SecurityRules;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRule;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRuleBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRuleKey;
34 import org.ops4j.pax.cdi.api.OsgiServiceProvider;
37 @OsgiServiceProvider(classes = INeutronSecurityRuleCRUD.class)
38 public final class NeutronSecurityRuleInterface extends
39 AbstractNeutronInterface<SecurityRule, SecurityRules, SecurityRuleKey, NeutronSecurityRule>
40 implements INeutronSecurityRuleCRUD {
42 private static final ImmutableBiMap<Class<? extends EthertypeBase>,
43 String> ETHERTYPE_MAP = new ImmutableBiMap.Builder<Class<? extends EthertypeBase>, String>()
44 .put(EthertypeV4.class, "IPv4").put(EthertypeV6.class, "IPv6").build();
46 private final NeutronSecurityGroupInterface securityGroupInterface;
49 public NeutronSecurityRuleInterface(DataBroker db, NeutronSecurityGroupInterface securityGroupInterface) {
50 super(SecurityRuleBuilder.class, db);
51 this.securityGroupInterface = securityGroupInterface;
55 protected List<SecurityRule> getDataObjectList(SecurityRules rules) {
56 return rules.getSecurityRule();
60 protected NeutronSecurityRule fromMd(SecurityRule rule) {
61 final NeutronSecurityRule answer = new NeutronSecurityRule();
62 fromMdIds(rule, answer);
63 if (rule.getDirection() != null) {
64 answer.setSecurityRuleDirection(DirectionMapper.getDirectionString(rule.getDirection()));
66 if (rule.getSecurityGroupId() != null) {
67 answer.setSecurityRuleGroupID(rule.getSecurityGroupId().getValue());
69 if (rule.getRemoteGroupId() != null) {
70 answer.setSecurityRemoteGroupID(rule.getRemoteGroupId().getValue());
72 if (rule.getRemoteIpPrefix() != null) {
73 answer.setSecurityRuleRemoteIpPrefix(rule.getRemoteIpPrefix().stringValue());
75 if (rule.getProtocol() != null) {
76 final Protocol protocol = rule.getProtocol();
77 if (protocol.getUint8() != null) {
79 answer.setSecurityRuleProtocol(protocol.getUint8().toString());
81 // symbolic protocol name
82 answer.setSecurityRuleProtocol(ProtocolMapper.getName(protocol.getIdentityref()));
85 if (rule.getEthertype() != null) {
86 answer.setSecurityRuleEthertype(ETHERTYPE_MAP.get(rule.getEthertype()));
88 if (rule.getPortRangeMin() != null) {
89 answer.setSecurityRulePortMin(rule.getPortRangeMin());
91 if (rule.getPortRangeMax() != null) {
92 answer.setSecurityRulePortMax(rule.getPortRangeMax());
98 @SuppressWarnings("checkstyle:AvoidHidingCauseException")
99 protected SecurityRule toMd(NeutronSecurityRule securityRule) {
100 final SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
101 toMdIds(securityRule, securityRuleBuilder);
102 if (securityRule.getSecurityRuleDirection() != null) {
104 .setDirection(DirectionMapper.get(securityRule.getSecurityRuleDirection()));
106 if (securityRule.getSecurityRuleGroupID() != null) {
107 securityRuleBuilder.setSecurityGroupId(toUuid(securityRule.getSecurityRuleGroupID()));
109 if (securityRule.getSecurityRemoteGroupID() != null) {
110 securityRuleBuilder.setRemoteGroupId(toUuid(securityRule.getSecurityRemoteGroupID()));
112 if (securityRule.getSecurityRuleRemoteIpPrefix() != null) {
113 final IpPrefix ipPrefix = IpPrefixBuilder.getDefaultInstance(securityRule.getSecurityRuleRemoteIpPrefix());
114 securityRuleBuilder.setRemoteIpPrefix(ipPrefix);
116 if (securityRule.getSecurityRuleProtocol() != null) {
117 final String protocolString = securityRule.getSecurityRuleProtocol();
119 final Protocol protocol = SecurityRuleAttributesProtocolBuilder.getDefaultInstance(protocolString);
120 securityRuleBuilder.setProtocol(protocol);
121 } catch (NumberFormatException e) {
122 throw new BadRequestException("Protocol {" + securityRule.getSecurityRuleProtocol()
123 + "} is not supported");
126 if (securityRule.getSecurityRuleEthertype() != null) {
127 final ImmutableBiMap<String, Class<? extends EthertypeBase>> mapper = ETHERTYPE_MAP.inverse();
129 .setEthertype(mapper.get(securityRule.getSecurityRuleEthertype()));
131 if (securityRule.getSecurityRulePortMin() != null) {
132 securityRuleBuilder.setPortRangeMin(securityRule.getSecurityRulePortMin());
134 if (securityRule.getSecurityRulePortMax() != null) {
135 securityRuleBuilder.setPortRangeMax(securityRule.getSecurityRulePortMax());
137 return securityRuleBuilder.build();
141 protected boolean areAllDependenciesAvailable(ReadTransaction tx, NeutronSecurityRule securityRule)
142 throws ReadFailedException {
143 return ifNonNull(securityRule.getSecurityRuleGroupID(),
144 groupID -> securityGroupInterface.exists(groupID, tx))
145 && ifNonNull(securityRule.getSecurityRemoteGroupID(),
146 remoteGroupID -> securityGroupInterface.exists(remoteGroupID, tx));