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