Migrate to stringValue()
[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.controller.md.sal.binding.api.ReadTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
18 import org.opendaylight.neutron.northbound.api.BadRequestException;
19 import org.opendaylight.neutron.spi.INeutronSecurityRuleCRUD;
20 import org.opendaylight.neutron.spi.NeutronSecurityRule;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefixBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeBase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV4;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV6;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.NeutronUtils.DirectionMapper;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.NeutronUtils.ProtocolMapper;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.SecurityRuleAttributes.Protocol;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.SecurityRuleAttributesProtocolBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.SecurityRules;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRule;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRuleBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRuleKey;
34 import org.ops4j.pax.cdi.api.OsgiServiceProvider;
35
36 @Singleton
37 @OsgiServiceProvider(classes = INeutronSecurityRuleCRUD.class)
38 public final class NeutronSecurityRuleInterface extends
39         AbstractNeutronInterface<SecurityRule, SecurityRules, SecurityRuleKey, NeutronSecurityRule>
40         implements INeutronSecurityRuleCRUD {
41
42     private static final ImmutableBiMap<Class<? extends EthertypeBase>,
43             String> ETHERTYPE_MAP = new ImmutableBiMap.Builder<Class<? extends EthertypeBase>, String>()
44                     .put(EthertypeV4.class, "IPv4").put(EthertypeV6.class, "IPv6").build();
45
46     private final NeutronSecurityGroupInterface securityGroupInterface;
47
48     @Inject
49     public NeutronSecurityRuleInterface(DataBroker db, NeutronSecurityGroupInterface securityGroupInterface) {
50         super(SecurityRuleBuilder.class, db);
51         this.securityGroupInterface = securityGroupInterface;
52     }
53
54     @Override
55     protected List<SecurityRule> getDataObjectList(SecurityRules rules) {
56         return rules.getSecurityRule();
57     }
58
59     @Override
60     protected NeutronSecurityRule fromMd(SecurityRule rule) {
61         final NeutronSecurityRule answer = new NeutronSecurityRule();
62         fromMdIds(rule, answer);
63         if (rule.getDirection() != null) {
64             answer.setSecurityRuleDirection(DirectionMapper.getDirectionString(rule.getDirection()));
65         }
66         if (rule.getSecurityGroupId() != null) {
67             answer.setSecurityRuleGroupID(rule.getSecurityGroupId().getValue());
68         }
69         if (rule.getRemoteGroupId() != null) {
70             answer.setSecurityRemoteGroupID(rule.getRemoteGroupId().getValue());
71         }
72         if (rule.getRemoteIpPrefix() != null) {
73             answer.setSecurityRuleRemoteIpPrefix(rule.getRemoteIpPrefix().stringValue());
74         }
75         if (rule.getProtocol() != null) {
76             final Protocol protocol = rule.getProtocol();
77             if (protocol.getUint8() != null) {
78                 // uint8
79                 answer.setSecurityRuleProtocol(protocol.getUint8().toString());
80             } else {
81                 // symbolic protocol name
82                 answer.setSecurityRuleProtocol(ProtocolMapper.getName(protocol.getIdentityref()));
83             }
84         }
85         if (rule.getEthertype() != null) {
86             answer.setSecurityRuleEthertype(ETHERTYPE_MAP.get(rule.getEthertype()));
87         }
88         if (rule.getPortRangeMin() != null) {
89             answer.setSecurityRulePortMin(rule.getPortRangeMin());
90         }
91         if (rule.getPortRangeMax() != null) {
92             answer.setSecurityRulePortMax(rule.getPortRangeMax());
93         }
94         return answer;
95     }
96
97     @Override
98     @SuppressWarnings("checkstyle:AvoidHidingCauseException")
99     protected SecurityRule toMd(NeutronSecurityRule securityRule) {
100         final SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
101         toMdIds(securityRule, securityRuleBuilder);
102         if (securityRule.getSecurityRuleDirection() != null) {
103             securityRuleBuilder
104                     .setDirection(DirectionMapper.get(securityRule.getSecurityRuleDirection()));
105         }
106         if (securityRule.getSecurityRuleGroupID() != null) {
107             securityRuleBuilder.setSecurityGroupId(toUuid(securityRule.getSecurityRuleGroupID()));
108         }
109         if (securityRule.getSecurityRemoteGroupID() != null) {
110             securityRuleBuilder.setRemoteGroupId(toUuid(securityRule.getSecurityRemoteGroupID()));
111         }
112         if (securityRule.getSecurityRuleRemoteIpPrefix() != null) {
113             final IpPrefix ipPrefix = IpPrefixBuilder.getDefaultInstance(securityRule.getSecurityRuleRemoteIpPrefix());
114             securityRuleBuilder.setRemoteIpPrefix(ipPrefix);
115         }
116         if (securityRule.getSecurityRuleProtocol() != null) {
117             final String protocolString = securityRule.getSecurityRuleProtocol();
118             try {
119                 final Protocol protocol = SecurityRuleAttributesProtocolBuilder.getDefaultInstance(protocolString);
120                 securityRuleBuilder.setProtocol(protocol);
121             } catch (NumberFormatException e) {
122                 throw new BadRequestException("Protocol {" + securityRule.getSecurityRuleProtocol()
123                         + "} is not supported");
124             }
125         }
126         if (securityRule.getSecurityRuleEthertype() != null) {
127             final ImmutableBiMap<String, Class<? extends EthertypeBase>> mapper = ETHERTYPE_MAP.inverse();
128             securityRuleBuilder
129                     .setEthertype(mapper.get(securityRule.getSecurityRuleEthertype()));
130         }
131         if (securityRule.getSecurityRulePortMin() != null) {
132             securityRuleBuilder.setPortRangeMin(securityRule.getSecurityRulePortMin());
133         }
134         if (securityRule.getSecurityRulePortMax() != null) {
135             securityRuleBuilder.setPortRangeMax(securityRule.getSecurityRulePortMax());
136         }
137         return securityRuleBuilder.build();
138     }
139
140     @Override
141     protected boolean areAllDependenciesAvailable(ReadTransaction tx, NeutronSecurityRule securityRule)
142             throws ReadFailedException {
143         return ifNonNull(securityRule.getSecurityRuleGroupID(),
144             groupID -> securityGroupInterface.exists(groupID, tx))
145             && ifNonNull(securityRule.getSecurityRemoteGroupID(),
146                 remoteGroupID -> securityGroupInterface.exists(remoteGroupID, tx));
147     }
148 }