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.neutron.northbound.api.BadRequestException;
17 import org.opendaylight.neutron.spi.INeutronSecurityRuleCRUD;
18 import org.opendaylight.neutron.spi.NeutronSecurityRule;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeBase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV4;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV6;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.NeutronUtils.DirectionMapper;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.NeutronUtils.ProtocolMapper;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.SecurityRuleAttributes.Protocol;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.SecurityRules;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRule;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRuleBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRuleKey;
30 import org.ops4j.pax.cdi.api.OsgiServiceProvider;
33 @OsgiServiceProvider(classes = INeutronSecurityRuleCRUD.class)
34 public final class NeutronSecurityRuleInterface extends
35 AbstractNeutronInterface<SecurityRule, SecurityRules, SecurityRuleKey, NeutronSecurityRule>
36 implements INeutronSecurityRuleCRUD {
38 private static final ImmutableBiMap<Class<? extends EthertypeBase>,
39 String> ETHERTYPE_MAP = new ImmutableBiMap.Builder<Class<? extends EthertypeBase>, String>()
40 .put(EthertypeV4.class, "IPv4").put(EthertypeV6.class, "IPv6").build();
43 public NeutronSecurityRuleInterface(DataBroker db) {
44 super(SecurityRuleBuilder.class, db);
48 protected List<SecurityRule> getDataObjectList(SecurityRules rules) {
49 return rules.getSecurityRule();
53 protected NeutronSecurityRule fromMd(SecurityRule rule) {
54 final NeutronSecurityRule answer = new NeutronSecurityRule();
55 fromMdIds(rule, answer);
56 if (rule.getDirection() != null) {
57 answer.setSecurityRuleDirection(DirectionMapper.getDirectionString(rule.getDirection()));
59 if (rule.getSecurityGroupId() != null) {
60 answer.setSecurityRuleGroupID(rule.getSecurityGroupId().getValue());
62 if (rule.getRemoteGroupId() != null) {
63 answer.setSecurityRemoteGroupID(rule.getRemoteGroupId().getValue());
65 if (rule.getRemoteIpPrefix() != null) {
66 answer.setSecurityRuleRemoteIpPrefix(new String(rule.getRemoteIpPrefix().getValue()));
68 if (rule.getProtocol() != null) {
69 final Protocol protocol = rule.getProtocol();
70 if (protocol.getUint8() != null) {
72 answer.setSecurityRuleProtocol(protocol.getUint8().toString());
74 // symbolic protocol name
75 answer.setSecurityRuleProtocol(ProtocolMapper.getName(protocol.getIdentityref()));
78 if (rule.getEthertype() != null) {
79 answer.setSecurityRuleEthertype(ETHERTYPE_MAP.get(rule.getEthertype()));
81 if (rule.getPortRangeMin() != null) {
82 answer.setSecurityRulePortMin(rule.getPortRangeMin());
84 if (rule.getPortRangeMax() != null) {
85 answer.setSecurityRulePortMax(rule.getPortRangeMax());
91 @SuppressWarnings("checkstyle:AvoidHidingCauseException")
92 protected SecurityRule toMd(NeutronSecurityRule securityRule) {
93 final SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
94 toMdIds(securityRule, securityRuleBuilder);
95 if (securityRule.getSecurityRuleDirection() != null) {
97 .setDirection(DirectionMapper.get(securityRule.getSecurityRuleDirection()));
99 if (securityRule.getSecurityRuleGroupID() != null) {
100 securityRuleBuilder.setSecurityGroupId(toUuid(securityRule.getSecurityRuleGroupID()));
102 if (securityRule.getSecurityRemoteGroupID() != null) {
103 securityRuleBuilder.setRemoteGroupId(toUuid(securityRule.getSecurityRemoteGroupID()));
105 if (securityRule.getSecurityRuleRemoteIpPrefix() != null) {
106 final IpPrefix ipPrefix = new IpPrefix(securityRule.getSecurityRuleRemoteIpPrefix().toCharArray());
107 securityRuleBuilder.setRemoteIpPrefix(ipPrefix);
109 if (securityRule.getSecurityRuleProtocol() != null) {
110 final String protocolString = securityRule.getSecurityRuleProtocol();
112 final Protocol protocol = new Protocol(protocolString.toCharArray());
113 securityRuleBuilder.setProtocol(protocol);
114 } catch (NumberFormatException e) {
115 throw new BadRequestException("Protocol {" + securityRule.getSecurityRuleProtocol()
116 + "} is not supported");
119 if (securityRule.getSecurityRuleEthertype() != null) {
120 final ImmutableBiMap<String, Class<? extends EthertypeBase>> mapper = ETHERTYPE_MAP.inverse();
122 .setEthertype(mapper.get(securityRule.getSecurityRuleEthertype()));
124 if (securityRule.getSecurityRulePortMin() != null) {
125 securityRuleBuilder.setPortRangeMin(securityRule.getSecurityRulePortMin());
127 if (securityRule.getSecurityRulePortMax() != null) {
128 securityRuleBuilder.setPortRangeMax(securityRule.getSecurityRulePortMax());
130 return securityRuleBuilder.build();