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