4808007bbc9255149e2b6f1dbce2814362695da4
[neutron.git] / transcriber / src / main / java / org / opendaylight / neutron / transcriber / NeutronSecurityRuleInterface.java
1 /*
2  * Copyright (c) 2014, 2015 Red Hat, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.neutron.transcriber;
10
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;
30
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);
35
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();
39
40     NeutronSecurityRuleInterface(DataBroker db) {
41         super(SecurityRuleBuilder.class, db);
42     }
43
44     @Override
45     protected List<SecurityRule> getDataObjectList(SecurityRules rules) {
46         return rules.getSecurityRule();
47     }
48
49     @Override
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()));
55         }
56         if (rule.getSecurityGroupId() != null) {
57             answer.setSecurityRuleGroupID(rule.getSecurityGroupId().getValue());
58         }
59         if (rule.getRemoteGroupId() != null) {
60             answer.setSecurityRemoteGroupID(rule.getRemoteGroupId().getValue());
61         }
62         if (rule.getRemoteIpPrefix() != null) {
63             answer.setSecurityRuleRemoteIpPrefix(new String(rule.getRemoteIpPrefix().getValue()));
64         }
65         if (rule.getProtocol() != null) {
66             final Protocol protocol = rule.getProtocol();
67             if (protocol.getUint8() != null) {
68                 // uint8
69                 answer.setSecurityRuleProtocol(protocol.getUint8().toString());
70             } else {
71                 // symbolic protocol name
72                 answer.setSecurityRuleProtocol(ProtocolMapper.getName(protocol.getIdentityref()));
73             }
74         }
75         if (rule.getEthertype() != null) {
76             answer.setSecurityRuleEthertype(ETHERTYPE_MAP.get(rule.getEthertype()));
77         }
78         if (rule.getPortRangeMin() != null) {
79             answer.setSecurityRulePortMin(Integer.valueOf(rule.getPortRangeMin()));
80         }
81         if (rule.getPortRangeMax() != null) {
82             answer.setSecurityRulePortMax(Integer.valueOf(rule.getPortRangeMax()));
83         }
84         return answer;
85     }
86
87     @Override
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) {
93             securityRuleBuilder
94                     .setDirection(DirectionMapper.get(securityRule.getSecurityRuleDirection()));
95         }
96         if (securityRule.getSecurityRuleGroupID() != null) {
97             securityRuleBuilder.setSecurityGroupId(toUuid(securityRule.getSecurityRuleGroupID()));
98         }
99         if (securityRule.getSecurityRemoteGroupID() != null) {
100             securityRuleBuilder.setRemoteGroupId(toUuid(securityRule.getSecurityRemoteGroupID()));
101         }
102         if (securityRule.getSecurityRuleRemoteIpPrefix() != null) {
103             final IpPrefix ipPrefix = new IpPrefix(securityRule.getSecurityRuleRemoteIpPrefix().toCharArray());
104             securityRuleBuilder.setRemoteIpPrefix(ipPrefix);
105         }
106         if (securityRule.getSecurityRuleProtocol() != null) {
107             final String protocolString = securityRule.getSecurityRuleProtocol();
108             try {
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");
114             }
115         }
116         if (securityRule.getSecurityRuleEthertype() != null) {
117             final ImmutableBiMap<String, Class<? extends EthertypeBase>> mapper = ETHERTYPE_MAP.inverse();
118             securityRuleBuilder
119                     .setEthertype(mapper.get(securityRule.getSecurityRuleEthertype()));
120         }
121         if (securityRule.getSecurityRulePortMin() != null) {
122             securityRuleBuilder.setPortRangeMin(Integer.valueOf(securityRule.getSecurityRulePortMin()));
123         }
124         if (securityRule.getSecurityRulePortMax() != null) {
125             securityRuleBuilder.setPortRangeMax(Integer.valueOf(securityRule.getSecurityRulePortMax()));
126         }
127         return securityRuleBuilder.build();
128     }
129 }