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