Replace LOGGER by LOG
[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.DirectionBase;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionEgress;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionIngress;
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.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.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public final class NeutronSecurityRuleInterface extends
34         AbstractNeutronInterface<SecurityRule, SecurityRules, SecurityRuleKey, NeutronSecurityRule>
35         implements INeutronSecurityRuleCRUD {
36     private static final Logger LOG = LoggerFactory.getLogger(NeutronSecurityRuleInterface.class);
37
38     private static final ImmutableBiMap<Class<? extends DirectionBase>,
39             String> DIRECTION_MAP = new ImmutableBiMap.Builder<Class<? extends DirectionBase>, String>()
40                     .put(DirectionEgress.class, "egress").put(DirectionIngress.class, "ingress").build();
41     private static final ImmutableBiMap<Class<? extends EthertypeBase>,
42             String> ETHERTYPE_MAP = new ImmutableBiMap.Builder<Class<? extends EthertypeBase>, String>()
43                     .put(EthertypeV4.class, "IPv4").put(EthertypeV6.class, "IPv6").build();
44
45     NeutronSecurityRuleInterface(DataBroker db) {
46         super(SecurityRuleBuilder.class, db);
47     }
48
49     @Override
50     protected List<SecurityRule> getDataObjectList(SecurityRules rules) {
51         return rules.getSecurityRule();
52     }
53
54     protected NeutronSecurityRule fromMd(SecurityRule rule) {
55         final NeutronSecurityRule answer = new NeutronSecurityRule();
56         if (rule.getTenantId() != null) {
57             answer.setTenantID(rule.getTenantId());
58         }
59         if (rule.getDirection() != null) {
60             answer.setSecurityRuleDirection(DIRECTION_MAP.get(rule.getDirection()));
61         }
62         if (rule.getSecurityGroupId() != null) {
63             answer.setSecurityRuleGroupID(rule.getSecurityGroupId().getValue());
64         }
65         if (rule.getRemoteGroupId() != null) {
66             answer.setSecurityRemoteGroupID(rule.getRemoteGroupId().getValue());
67         }
68         if (rule.getRemoteIpPrefix() != null) {
69             answer.setSecurityRuleRemoteIpPrefix(new String(rule.getRemoteIpPrefix().getValue()));
70         }
71         if (rule.getProtocol() != null) {
72             final Protocol protocol = rule.getProtocol();
73             if (protocol.getUint8() != null) {
74                 // uint8
75                 answer.setSecurityRuleProtocol(protocol.getUint8().toString());
76             } else {
77                 // symbolic protocol name
78                 answer.setSecurityRuleProtocol(ProtocolMapper.getName(protocol.getIdentityref()));
79             }
80         }
81         if (rule.getEthertype() != null) {
82             answer.setSecurityRuleEthertype(ETHERTYPE_MAP.get(rule.getEthertype()));
83         }
84         if (rule.getPortRangeMin() != null) {
85             answer.setSecurityRulePortMin(Integer.valueOf(rule.getPortRangeMin()));
86         }
87         if (rule.getPortRangeMax() != null) {
88             answer.setSecurityRulePortMax(Integer.valueOf(rule.getPortRangeMax()));
89         }
90         if (rule.getUuid() != null) {
91             answer.setID(rule.getUuid().getValue());
92         }
93         return answer;
94     }
95
96     @Override
97     protected SecurityRule toMd(NeutronSecurityRule securityRule) {
98         final SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
99
100         if (securityRule.getTenantID() != null) {
101             securityRuleBuilder.setTenantId(toUuid(securityRule.getTenantID()));
102         }
103         if (securityRule.getSecurityRuleDirection() != null) {
104             final ImmutableBiMap<String, Class<? extends DirectionBase>> mapper = DIRECTION_MAP.inverse();
105             securityRuleBuilder
106                     .setDirection((Class<? extends DirectionBase>) mapper.get(securityRule.getSecurityRuleDirection()));
107         }
108         if (securityRule.getSecurityRuleGroupID() != null) {
109             securityRuleBuilder.setSecurityGroupId(toUuid(securityRule.getSecurityRuleGroupID()));
110         }
111         if (securityRule.getSecurityRemoteGroupID() != null) {
112             securityRuleBuilder.setRemoteGroupId(toUuid(securityRule.getSecurityRemoteGroupID()));
113         }
114         if (securityRule.getSecurityRuleRemoteIpPrefix() != null) {
115             final IpPrefix ipPrefix = new IpPrefix(securityRule.getSecurityRuleRemoteIpPrefix().toCharArray());
116             securityRuleBuilder.setRemoteIpPrefix(ipPrefix);
117         }
118         if (securityRule.getSecurityRuleProtocol() != null) {
119             final String protocolString = securityRule.getSecurityRuleProtocol();
120             try {
121                 final Protocol protocol = new Protocol(protocolString.toCharArray());
122                 securityRuleBuilder.setProtocol(protocol);
123             } catch (NumberFormatException e) {
124                 throw new BadRequestException("Protocol {" + securityRule.getSecurityRuleProtocol()
125                         + "} is not supported");
126             }
127         }
128         if (securityRule.getSecurityRuleEthertype() != null) {
129             final ImmutableBiMap<String, Class<? extends EthertypeBase>> mapper = ETHERTYPE_MAP.inverse();
130             securityRuleBuilder
131                     .setEthertype((Class<? extends EthertypeBase>) mapper.get(securityRule.getSecurityRuleEthertype()));
132         }
133         if (securityRule.getSecurityRulePortMin() != null) {
134             securityRuleBuilder.setPortRangeMin(Integer.valueOf(securityRule.getSecurityRulePortMin()));
135         }
136         if (securityRule.getSecurityRulePortMax() != null) {
137             securityRuleBuilder.setPortRangeMax(Integer.valueOf(securityRule.getSecurityRulePortMax()));
138         }
139         if (securityRule.getID() != null) {
140             securityRuleBuilder.setUuid(toUuid(securityRule.getID()));
141         } else {
142             LOG.warn("Attempting to write neutron securityRule without UUID");
143         }
144         return securityRuleBuilder.build();
145     }
146 }