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 org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.neutron.northbound.api.BadRequestException;
15 import org.opendaylight.neutron.spi.INeutronSecurityRuleCRUD;
16 import org.opendaylight.neutron.spi.NeutronSecurityRule;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeBase;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV4;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV6;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.NeutronUtils.DirectionMapper;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.NeutronUtils.ProtocolMapper;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.SecurityRuleAttributes.Protocol;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.SecurityRules;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRule;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRuleBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRuleKey;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public final class NeutronSecurityRuleInterface extends
32 AbstractNeutronInterface<SecurityRule, SecurityRules, SecurityRuleKey, NeutronSecurityRule>
33 implements INeutronSecurityRuleCRUD {
34 private static final Logger LOG = LoggerFactory.getLogger(NeutronSecurityRuleInterface.class);
36 private static final ImmutableBiMap<Class<? extends EthertypeBase>,
37 String> ETHERTYPE_MAP = new ImmutableBiMap.Builder<Class<? extends EthertypeBase>, String>()
38 .put(EthertypeV4.class, "IPv4").put(EthertypeV6.class, "IPv6").build();
40 NeutronSecurityRuleInterface(DataBroker db) {
41 super(SecurityRuleBuilder.class, db);
45 protected List<SecurityRule> getDataObjectList(SecurityRules rules) {
46 return rules.getSecurityRule();
50 protected NeutronSecurityRule fromMd(SecurityRule rule) {
51 final NeutronSecurityRule answer = new NeutronSecurityRule();
52 fromMdIds(rule, answer);
53 if (rule.getDirection() != null) {
54 answer.setSecurityRuleDirection(DirectionMapper.getName(rule.getDirection()));
56 if (rule.getSecurityGroupId() != null) {
57 answer.setSecurityRuleGroupID(rule.getSecurityGroupId().getValue());
59 if (rule.getRemoteGroupId() != null) {
60 answer.setSecurityRemoteGroupID(rule.getRemoteGroupId().getValue());
62 if (rule.getRemoteIpPrefix() != null) {
63 answer.setSecurityRuleRemoteIpPrefix(new String(rule.getRemoteIpPrefix().getValue()));
65 if (rule.getProtocol() != null) {
66 final Protocol protocol = rule.getProtocol();
67 if (protocol.getUint8() != null) {
69 answer.setSecurityRuleProtocol(protocol.getUint8().toString());
71 // symbolic protocol name
72 answer.setSecurityRuleProtocol(ProtocolMapper.getName(protocol.getIdentityref()));
75 if (rule.getEthertype() != null) {
76 answer.setSecurityRuleEthertype(ETHERTYPE_MAP.get(rule.getEthertype()));
78 if (rule.getPortRangeMin() != null) {
79 answer.setSecurityRulePortMin(rule.getPortRangeMin());
81 if (rule.getPortRangeMax() != null) {
82 answer.setSecurityRulePortMax(rule.getPortRangeMax());
88 @SuppressWarnings("checkstyle:AvoidHidingCauseException")
89 protected SecurityRule toMd(NeutronSecurityRule securityRule) {
90 final SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
91 toMdIds(securityRule, securityRuleBuilder);
92 if (securityRule.getSecurityRuleDirection() != null) {
94 .setDirection(DirectionMapper.get(securityRule.getSecurityRuleDirection()));
96 if (securityRule.getSecurityRuleGroupID() != null) {
97 securityRuleBuilder.setSecurityGroupId(toUuid(securityRule.getSecurityRuleGroupID()));
99 if (securityRule.getSecurityRemoteGroupID() != null) {
100 securityRuleBuilder.setRemoteGroupId(toUuid(securityRule.getSecurityRemoteGroupID()));
102 if (securityRule.getSecurityRuleRemoteIpPrefix() != null) {
103 final IpPrefix ipPrefix = new IpPrefix(securityRule.getSecurityRuleRemoteIpPrefix().toCharArray());
104 securityRuleBuilder.setRemoteIpPrefix(ipPrefix);
106 if (securityRule.getSecurityRuleProtocol() != null) {
107 final String protocolString = securityRule.getSecurityRuleProtocol();
109 final Protocol protocol = new Protocol(protocolString.toCharArray());
110 securityRuleBuilder.setProtocol(protocol);
111 } catch (NumberFormatException e) {
112 throw new BadRequestException("Protocol {" + securityRule.getSecurityRuleProtocol()
113 + "} is not supported");
116 if (securityRule.getSecurityRuleEthertype() != null) {
117 final ImmutableBiMap<String, Class<? extends EthertypeBase>> mapper = ETHERTYPE_MAP.inverse();
119 .setEthertype(mapper.get(securityRule.getSecurityRuleEthertype()));
121 if (securityRule.getSecurityRulePortMin() != null) {
122 securityRuleBuilder.setPortRangeMin(securityRule.getSecurityRulePortMin());
124 if (securityRule.getSecurityRulePortMax() != null) {
125 securityRuleBuilder.setPortRangeMax(securityRule.getSecurityRulePortMax());
127 return securityRuleBuilder.build();