a6d16b9589c4f1044bd83a81fd0ee99c22c3c2be
[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 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;
31
32 @Singleton
33 @OsgiServiceProvider(classes = INeutronSecurityRuleCRUD.class)
34 public final class NeutronSecurityRuleInterface extends
35         AbstractNeutronInterface<SecurityRule, SecurityRules, SecurityRuleKey, NeutronSecurityRule>
36         implements INeutronSecurityRuleCRUD {
37
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();
41
42     @Inject
43     public NeutronSecurityRuleInterface(DataBroker db) {
44         super(SecurityRuleBuilder.class, db);
45     }
46
47     @Override
48     protected List<SecurityRule> getDataObjectList(SecurityRules rules) {
49         return rules.getSecurityRule();
50     }
51
52     @Override
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()));
58         }
59         if (rule.getSecurityGroupId() != null) {
60             answer.setSecurityRuleGroupID(rule.getSecurityGroupId().getValue());
61         }
62         if (rule.getRemoteGroupId() != null) {
63             answer.setSecurityRemoteGroupID(rule.getRemoteGroupId().getValue());
64         }
65         if (rule.getRemoteIpPrefix() != null) {
66             answer.setSecurityRuleRemoteIpPrefix(new String(rule.getRemoteIpPrefix().getValue()));
67         }
68         if (rule.getProtocol() != null) {
69             final Protocol protocol = rule.getProtocol();
70             if (protocol.getUint8() != null) {
71                 // uint8
72                 answer.setSecurityRuleProtocol(protocol.getUint8().toString());
73             } else {
74                 // symbolic protocol name
75                 answer.setSecurityRuleProtocol(ProtocolMapper.getName(protocol.getIdentityref()));
76             }
77         }
78         if (rule.getEthertype() != null) {
79             answer.setSecurityRuleEthertype(ETHERTYPE_MAP.get(rule.getEthertype()));
80         }
81         if (rule.getPortRangeMin() != null) {
82             answer.setSecurityRulePortMin(rule.getPortRangeMin());
83         }
84         if (rule.getPortRangeMax() != null) {
85             answer.setSecurityRulePortMax(rule.getPortRangeMax());
86         }
87         return answer;
88     }
89
90     @Override
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) {
96             securityRuleBuilder
97                     .setDirection(DirectionMapper.get(securityRule.getSecurityRuleDirection()));
98         }
99         if (securityRule.getSecurityRuleGroupID() != null) {
100             securityRuleBuilder.setSecurityGroupId(toUuid(securityRule.getSecurityRuleGroupID()));
101         }
102         if (securityRule.getSecurityRemoteGroupID() != null) {
103             securityRuleBuilder.setRemoteGroupId(toUuid(securityRule.getSecurityRemoteGroupID()));
104         }
105         if (securityRule.getSecurityRuleRemoteIpPrefix() != null) {
106             final IpPrefix ipPrefix = new IpPrefix(securityRule.getSecurityRuleRemoteIpPrefix().toCharArray());
107             securityRuleBuilder.setRemoteIpPrefix(ipPrefix);
108         }
109         if (securityRule.getSecurityRuleProtocol() != null) {
110             final String protocolString = securityRule.getSecurityRuleProtocol();
111             try {
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");
117             }
118         }
119         if (securityRule.getSecurityRuleEthertype() != null) {
120             final ImmutableBiMap<String, Class<? extends EthertypeBase>> mapper = ETHERTYPE_MAP.inverse();
121             securityRuleBuilder
122                     .setEthertype(mapper.get(securityRule.getSecurityRuleEthertype()));
123         }
124         if (securityRule.getSecurityRulePortMin() != null) {
125             securityRuleBuilder.setPortRangeMin(securityRule.getSecurityRulePortMin());
126         }
127         if (securityRule.getSecurityRulePortMax() != null) {
128             securityRuleBuilder.setPortRangeMax(securityRule.getSecurityRulePortMax());
129         }
130         return securityRuleBuilder.build();
131     }
132 }