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.DirectionBase;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionEgress;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionIngress;
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.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.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public final class NeutronSecurityRuleInterface extends
34 AbstractNeutronInterface<SecurityRule, SecurityRules, SecurityRuleKey, NeutronSecurityRule>
35 implements INeutronSecurityRuleCRUD {
36 private static final Logger LOG = LoggerFactory.getLogger(NeutronSecurityRuleInterface.class);
38 private static final ImmutableBiMap<Class<? extends DirectionBase>,
39 String> DIRECTION_MAP = new ImmutableBiMap.Builder<Class<? extends DirectionBase>, String>()
40 .put(DirectionEgress.class, "egress").put(DirectionIngress.class, "ingress").build();
41 private static final ImmutableBiMap<Class<? extends EthertypeBase>,
42 String> ETHERTYPE_MAP = new ImmutableBiMap.Builder<Class<? extends EthertypeBase>, String>()
43 .put(EthertypeV4.class, "IPv4").put(EthertypeV6.class, "IPv6").build();
45 NeutronSecurityRuleInterface(DataBroker db) {
46 super(SecurityRuleBuilder.class, db);
50 protected List<SecurityRule> getDataObjectList(SecurityRules rules) {
51 return rules.getSecurityRule();
54 protected NeutronSecurityRule fromMd(SecurityRule rule) {
55 final NeutronSecurityRule answer = new NeutronSecurityRule();
56 if (rule.getTenantId() != null) {
57 answer.setTenantID(rule.getTenantId());
59 if (rule.getDirection() != null) {
60 answer.setSecurityRuleDirection(DIRECTION_MAP.get(rule.getDirection()));
62 if (rule.getSecurityGroupId() != null) {
63 answer.setSecurityRuleGroupID(rule.getSecurityGroupId().getValue());
65 if (rule.getRemoteGroupId() != null) {
66 answer.setSecurityRemoteGroupID(rule.getRemoteGroupId().getValue());
68 if (rule.getRemoteIpPrefix() != null) {
69 answer.setSecurityRuleRemoteIpPrefix(new String(rule.getRemoteIpPrefix().getValue()));
71 if (rule.getProtocol() != null) {
72 final Protocol protocol = rule.getProtocol();
73 if (protocol.getUint8() != null) {
75 answer.setSecurityRuleProtocol(protocol.getUint8().toString());
77 // symbolic protocol name
78 answer.setSecurityRuleProtocol(ProtocolMapper.getName(protocol.getIdentityref()));
81 if (rule.getEthertype() != null) {
82 answer.setSecurityRuleEthertype(ETHERTYPE_MAP.get(rule.getEthertype()));
84 if (rule.getPortRangeMin() != null) {
85 answer.setSecurityRulePortMin(Integer.valueOf(rule.getPortRangeMin()));
87 if (rule.getPortRangeMax() != null) {
88 answer.setSecurityRulePortMax(Integer.valueOf(rule.getPortRangeMax()));
90 if (rule.getUuid() != null) {
91 answer.setID(rule.getUuid().getValue());
97 protected SecurityRule toMd(NeutronSecurityRule securityRule) {
98 final SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
100 if (securityRule.getTenantID() != null) {
101 securityRuleBuilder.setTenantId(toUuid(securityRule.getTenantID()));
103 if (securityRule.getSecurityRuleDirection() != null) {
104 final ImmutableBiMap<String, Class<? extends DirectionBase>> mapper = DIRECTION_MAP.inverse();
106 .setDirection((Class<? extends DirectionBase>) mapper.get(securityRule.getSecurityRuleDirection()));
108 if (securityRule.getSecurityRuleGroupID() != null) {
109 securityRuleBuilder.setSecurityGroupId(toUuid(securityRule.getSecurityRuleGroupID()));
111 if (securityRule.getSecurityRemoteGroupID() != null) {
112 securityRuleBuilder.setRemoteGroupId(toUuid(securityRule.getSecurityRemoteGroupID()));
114 if (securityRule.getSecurityRuleRemoteIpPrefix() != null) {
115 final IpPrefix ipPrefix = new IpPrefix(securityRule.getSecurityRuleRemoteIpPrefix().toCharArray());
116 securityRuleBuilder.setRemoteIpPrefix(ipPrefix);
118 if (securityRule.getSecurityRuleProtocol() != null) {
119 final String protocolString = securityRule.getSecurityRuleProtocol();
121 final Protocol protocol = new Protocol(protocolString.toCharArray());
122 securityRuleBuilder.setProtocol(protocol);
123 } catch (NumberFormatException e) {
124 throw new BadRequestException("Protocol {" + securityRule.getSecurityRuleProtocol()
125 + "} is not supported");
128 if (securityRule.getSecurityRuleEthertype() != null) {
129 final ImmutableBiMap<String, Class<? extends EthertypeBase>> mapper = ETHERTYPE_MAP.inverse();
131 .setEthertype((Class<? extends EthertypeBase>) mapper.get(securityRule.getSecurityRuleEthertype()));
133 if (securityRule.getSecurityRulePortMin() != null) {
134 securityRuleBuilder.setPortRangeMin(Integer.valueOf(securityRule.getSecurityRulePortMin()));
136 if (securityRule.getSecurityRulePortMax() != null) {
137 securityRuleBuilder.setPortRangeMax(Integer.valueOf(securityRule.getSecurityRulePortMax()));
139 if (securityRule.getID() != null) {
140 securityRuleBuilder.setUuid(toUuid(securityRule.getID()));
142 LOG.warn("Attempting to write neutron securityRule without UUID");
144 return securityRuleBuilder.build();