dd2981288f41d4715688e07f6fc9f584f759040f
[netvirt.git] /
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.netvirt.openstack.netvirt.translator.crud.impl;
10
11 import com.google.common.collect.ImmutableBiMap;
12 import java.util.ArrayList;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Set;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronSecurityGroup;
18 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronSecurityRule;
19 import org.opendaylight.netvirt.openstack.netvirt.translator.crud.INeutronSecurityGroupCRUD;
20 import org.opendaylight.netvirt.openstack.netvirt.translator.crud.INeutronSecurityRuleCRUD;
21 import org.opendaylight.netvirt.openstack.netvirt.translator.crud.NeutronCRUDInterfaces;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionBase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionEgress;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionIngress;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeBase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV4;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV6;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolBase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolIcmp;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolIcmpV6;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolTcp;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolUdp;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.NeutronUtils;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.SecurityRuleAttributes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.SecurityRules;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRule;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRuleBuilder;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.osgi.framework.BundleContext;
42 import org.osgi.framework.ServiceRegistration;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46
47 public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<SecurityRule, NeutronSecurityRule> implements INeutronSecurityRuleCRUD {
48
49     private static final Logger LOG = LoggerFactory.getLogger(NeutronSecurityRuleInterface.class);
50
51     private static final ImmutableBiMap<Class<? extends DirectionBase>, String> DIRECTION_MAP = ImmutableBiMap.of(
52             DirectionEgress.class, NeutronSecurityRule.DIRECTION_EGRESS,
53             DirectionIngress.class, NeutronSecurityRule.DIRECTION_INGRESS);
54     private static final ImmutableBiMap<Class<? extends ProtocolBase>, String> PROTOCOL_MAP = ImmutableBiMap.of(
55             ProtocolIcmp.class, NeutronSecurityRule.PROTOCOL_ICMP,
56             ProtocolTcp.class, NeutronSecurityRule.PROTOCOL_TCP,
57             ProtocolUdp.class, NeutronSecurityRule.PROTOCOL_UDP,
58             ProtocolIcmpV6.class, NeutronSecurityRule.PROTOCOL_ICMPV6);
59     private static final ImmutableBiMap<Class<? extends EthertypeBase>,String> ETHERTYPE_MAP = ImmutableBiMap.of(
60             EthertypeV4.class, NeutronSecurityRule.ETHERTYPE_IPV4,
61             EthertypeV6.class, NeutronSecurityRule.ETHERTYPE_IPV6);
62
63     NeutronSecurityRuleInterface(final DataBroker dataBroker) {
64         super(dataBroker);
65     }
66
67     @Override
68     public boolean neutronSecurityRuleExists(String uuid) {
69         SecurityRule rule = readMd(createInstanceIdentifier(toMd(uuid)));
70         return rule != null;
71     }
72
73     @Override
74     public NeutronSecurityRule getNeutronSecurityRule(String uuid) {
75         SecurityRule rule = readMd(createInstanceIdentifier(toMd(uuid)));
76         if (rule == null) {
77             return null;
78         }
79         return fromMd(rule);
80     }
81
82     @Override
83     public List<NeutronSecurityRule> getAllNeutronSecurityRules() {
84         Set<NeutronSecurityRule> allSecurityRules = new HashSet<>();
85         SecurityRules rules = readMd(createInstanceIdentifier());
86         if (rules != null) {
87             for (SecurityRule rule: rules.getSecurityRule()) {
88                 allSecurityRules.add(fromMd(rule));
89             }
90         }
91         LOG.debug("Exiting getSecurityRule, Found {} OpenStackSecurityRule", allSecurityRules.size());
92         return new ArrayList<>(allSecurityRules);
93     }
94
95     @Override
96     public boolean addNeutronSecurityRule(NeutronSecurityRule input) {
97         if (neutronSecurityRuleExists(input.getID())) {
98             return false;
99         }
100         addMd(input);
101         return true;
102     }
103
104     @Override
105     public boolean removeNeutronSecurityRule(String uuid) {
106         if (!neutronSecurityRuleExists(uuid)) {
107             return false;
108         }
109         removeMd(toMd(uuid));
110         return true;
111     }
112
113     @Override
114     public boolean updateNeutronSecurityRule(String uuid, NeutronSecurityRule delta) {
115         if (!neutronSecurityRuleExists(uuid)) {
116             return false;
117         }
118         updateMd(delta);
119         return true;
120     }
121
122     @Override
123     public boolean neutronSecurityRuleInUse(String securityRuleUUID) {
124         return !neutronSecurityRuleExists(securityRuleUUID);
125     }
126
127     protected NeutronSecurityRule fromMd(SecurityRule rule) {
128         NeutronSecurityRule answer = new NeutronSecurityRule();
129         if (rule.getTenantId() != null) {
130             answer.setSecurityRuleTenantID(rule.getTenantId().getValue().replace("-",""));
131         }
132         if (rule.getDirection() != null) {
133             answer.setSecurityRuleDirection(DIRECTION_MAP.get(rule.getDirection()));
134         }
135         if (rule.getSecurityGroupId() != null) {
136             answer.setSecurityRuleGroupID(rule.getSecurityGroupId().getValue());
137         }
138         if (rule.getRemoteGroupId() != null) {
139             answer.setSecurityRemoteGroupID(rule.getRemoteGroupId().getValue());
140         }
141         if (rule.getRemoteIpPrefix() != null) {
142             answer.setSecurityRuleRemoteIpPrefix(rule.getRemoteIpPrefix().getIpv4Prefix() != null?
143                     rule.getRemoteIpPrefix().getIpv4Prefix().getValue():rule.getRemoteIpPrefix().getIpv6Prefix().getValue());
144         }
145         if (rule.getProtocol() != null) {
146             SecurityRuleAttributes.Protocol protocol = rule.getProtocol();
147             if (protocol.getUint8() != null) {
148                 // uint8
149                 answer.setSecurityRuleProtocol(protocol.getUint8().toString());
150             } else {
151                // symbolic protocol name
152                answer.setSecurityRuleProtocol(NeutronUtils.ProtocolMapper.getName(protocol.getIdentityref()));
153             }
154         }
155         if (rule.getEthertype() != null) {
156             answer.setSecurityRuleEthertype(ETHERTYPE_MAP.get(rule.getEthertype()));
157         }
158         if (rule.getPortRangeMin() != null) {
159             answer.setSecurityRulePortMin(rule.getPortRangeMin());
160         }
161         if (rule.getPortRangeMax() != null) {
162             answer.setSecurityRulePortMax(rule.getPortRangeMax());
163         }
164         if (rule.getUuid() != null) {
165             answer.setID(rule.getUuid().getValue());
166         }
167         return answer;
168     }
169
170     @Override
171     protected SecurityRule toMd(NeutronSecurityRule securityRule) {
172         SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
173
174         if (securityRule.getSecurityRuleTenantID() != null) {
175             securityRuleBuilder.setTenantId(toUuid(securityRule.getSecurityRuleTenantID()));
176         }
177         if (securityRule.getSecurityRuleDirection() != null) {
178             ImmutableBiMap<String, Class<? extends DirectionBase>> mapper =
179                     DIRECTION_MAP.inverse();
180             securityRuleBuilder.setDirection(mapper.get(securityRule.getSecurityRuleDirection()));
181         }
182         if (securityRule.getSecurityRuleGroupID() != null) {
183             securityRuleBuilder.setSecurityGroupId(toUuid(securityRule.getSecurityRuleGroupID()));
184         }
185         if (securityRule.getSecurityRemoteGroupID() != null) {
186             securityRuleBuilder.setRemoteGroupId(toUuid(securityRule.getSecurityRemoteGroupID()));
187         }
188         if (securityRule.getSecurityRuleRemoteIpPrefix() != null) {
189             securityRuleBuilder.setRemoteIpPrefix(new IpPrefix(securityRule.getSecurityRuleRemoteIpPrefix().toCharArray()));
190         }
191         if (securityRule.getSecurityRuleProtocol() != null) {
192             String protocolString = securityRule.getSecurityRuleProtocol();
193             SecurityRuleAttributes.Protocol protocol = new SecurityRuleAttributes.Protocol(protocolString.toCharArray());
194             securityRuleBuilder.setProtocol(protocol);
195         }
196         if (securityRule.getSecurityRuleEthertype() != null) {
197             ImmutableBiMap<String, Class<? extends EthertypeBase>> mapper =
198                     ETHERTYPE_MAP.inverse();
199             securityRuleBuilder.setEthertype(mapper.get(securityRule.getSecurityRuleEthertype()));
200         }
201         if (securityRule.getSecurityRulePortMin() != null) {
202             securityRuleBuilder.setPortRangeMin(securityRule.getSecurityRulePortMin());
203         }
204         if (securityRule.getSecurityRulePortMax() != null) {
205             securityRuleBuilder.setPortRangeMax(securityRule.getSecurityRulePortMax());
206         }
207         if (securityRule.getID() != null) {
208             securityRuleBuilder.setUuid(toUuid(securityRule.getID()));
209         } else {
210             LOG.warn("Attempting to write neutron securityRule without UUID");
211         }
212         return securityRuleBuilder.build();
213     }
214
215     @Override
216     protected InstanceIdentifier<SecurityRule> createInstanceIdentifier(SecurityRule securityRule) {
217         return InstanceIdentifier.create(Neutron.class)
218             .child(SecurityRules.class).child(SecurityRule.class,
219                                               securityRule.getKey());
220     }
221
222     protected InstanceIdentifier<SecurityRules> createInstanceIdentifier() {
223         return InstanceIdentifier.create(Neutron.class)
224             .child(SecurityRules.class);
225     }
226
227     @Override
228     protected SecurityRule toMd(String uuid) {
229         SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
230         securityRuleBuilder.setUuid(toUuid(uuid));
231         return securityRuleBuilder.build();
232     }
233
234     public static void registerNewInterface(BundleContext context,
235                                             final DataBroker dataBroker,
236                                             List<ServiceRegistration<?>> registrations) {
237         NeutronSecurityRuleInterface neutronSecurityRuleInterface = new NeutronSecurityRuleInterface(dataBroker);
238         ServiceRegistration<INeutronSecurityRuleCRUD> neutronSecurityRuleInterfaceRegistration = context.registerService(INeutronSecurityRuleCRUD.class, neutronSecurityRuleInterface, null);
239         if(neutronSecurityRuleInterfaceRegistration != null) {
240             registrations.add(neutronSecurityRuleInterfaceRegistration);
241         }
242     }
243 }