Merge "Added Unit Tests for ovsdb/southbound"
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / impl / SecurityServicesImpl.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.ovsdb.openstack.netvirt.impl;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import org.opendaylight.neutron.spi.INeutronPortCRUD;
15 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
16 import org.opendaylight.neutron.spi.NeutronPort;
17 import org.opendaylight.neutron.spi.NeutronSecurityGroup;
18 import org.opendaylight.neutron.spi.NeutronSubnet;
19 import org.opendaylight.neutron.spi.Neutron_IPs;
20 import org.opendaylight.ovsdb.openstack.netvirt.ConfigInterface;
21 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
22 import org.opendaylight.ovsdb.openstack.netvirt.api.SecurityServicesManager;
23 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
24 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
28 import org.osgi.framework.BundleContext;
29 import org.osgi.framework.ServiceReference;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class SecurityServicesImpl implements ConfigInterface, SecurityServicesManager {
34     private static final Logger LOG = LoggerFactory.getLogger(TenantNetworkManagerImpl.class);
35     private volatile INeutronPortCRUD neutronPortCache;
36     private volatile INeutronSubnetCRUD neutronSubnetCache;
37     private volatile Southbound southbound;
38
39     @Override
40     public boolean isPortSecurityReady(OvsdbTerminationPointAugmentation terminationPointAugmentation) {
41         if (neutronPortCache == null) {
42             LOG.error("neutron port is null");
43             return false;
44         }
45         LOG.trace("isPortSecurityReady for {}", terminationPointAugmentation.getName());
46         String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
47                                                                        Constants.EXTERNAL_ID_INTERFACE_ID);
48         if (neutronPortId == null) {
49             return false;
50         }
51         NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
52         if (neutronPort == null) {
53             return false;
54         }
55         String deviceOwner = neutronPort.getDeviceOwner();
56         if (!deviceOwner.contains("compute")) {
57             LOG.debug("Port {} is not a compute host, it is a: {}", neutronPortId, deviceOwner);
58         }
59         LOG.debug("isPortSecurityReady() is a {} ", deviceOwner);
60         List<NeutronSecurityGroup> securityGroups = neutronPort.getSecurityGroups();
61         if (securityGroups.isEmpty()) {
62             LOG.debug("Check for device: {} does not contain a Security Group for port: {}", deviceOwner,
63                       neutronPortId);
64             return false;
65         }
66         LOG.debug("Security Group Check {} does contain a Neutron Security Group", neutronPortId);
67         return true;
68     }
69
70     @Override
71     public List<NeutronSecurityGroup> getSecurityGroupInPortList(OvsdbTerminationPointAugmentation
72                                                              terminationPointAugmentation) {
73         List<NeutronSecurityGroup> neutronSecurityGroups = new ArrayList<NeutronSecurityGroup>();
74         if (neutronPortCache == null) {
75             LOG.error("neutron port is null");
76             return neutronSecurityGroups;
77         }
78         LOG.trace("isPortSecurityReady for {}", terminationPointAugmentation.getName());
79         String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
80                                                                        Constants.EXTERNAL_ID_INTERFACE_ID);
81         if (neutronPortId == null) {
82             return neutronSecurityGroups;
83         }
84         NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
85         if (neutronPort == null) {
86             return neutronSecurityGroups;
87         }
88         neutronSecurityGroups = neutronPort.getSecurityGroups();
89         return neutronSecurityGroups;
90
91     }
92
93     @Override
94     public NeutronPort getDhcpServerPort(OvsdbTerminationPointAugmentation terminationPointAugmentation) {
95         if (neutronPortCache == null) {
96             LOG.error("getDHCPServerPort: neutron port is null");
97             return null;
98         }
99         LOG.trace("getDHCPServerPort for {}",
100                   terminationPointAugmentation.getName());
101         try {
102             String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
103                                                                            Constants.EXTERNAL_ID_INTERFACE_ID);
104             if (neutronPortId == null) {
105                 return null;
106             }
107             NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
108             if (neutronPort == null) {
109                 LOG.error("getDHCPServerPort: neutron port of {} is not found", neutronPortId);
110                 return null;
111             }
112             /*Since all the fixed ip assigned to a port should be
113              *from the same network, first port is sufficient.*/
114             List<Neutron_IPs> fixedIps = neutronPort.getFixedIPs();
115             if (null == fixedIps || 0 == fixedIps.size() ) {
116                 LOG.error("getDHCPServerPort: No fixed ip is assigned");
117                 return null;
118             }
119             /* Get all the ports in the subnet and identify the dhcp port*/
120             String subnetUuid = fixedIps.iterator().next().getSubnetUUID();
121             NeutronSubnet neutronSubnet = neutronSubnetCache.getSubnet(subnetUuid);
122             List<NeutronPort> ports = neutronSubnet.getPortsInSubnet();
123             for (NeutronPort port : ports) {
124                 if (port.getDeviceOwner().contains("dhcp")) {
125                     return port;
126                 }
127             }
128         } catch (Exception e) {
129             LOG.error("getDHCPServerPort:getDHCPServerPort failed due to ", e);
130             return null;
131         }
132
133         return null;
134
135     }
136
137     @Override
138     public boolean isComputePort(OvsdbTerminationPointAugmentation terminationPointAugmentation) {
139         if (neutronPortCache == null) {
140             LOG.error("neutron port is null");
141             return false;
142         }
143         LOG.trace("isComputePort for {}", terminationPointAugmentation.getName());
144         String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
145                                                                        Constants.EXTERNAL_ID_INTERFACE_ID);
146         if (neutronPortId == null) {
147             return false;
148         }
149         NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
150         if (neutronPort == null) {
151             return false;
152         }
153         /*Check the device owner and if it contains compute to identify
154          * whether it is a compute port.*/
155         String deviceOwner = neutronPort.getDeviceOwner();
156         if (!deviceOwner.contains("compute")) {
157             LOG.debug("isComputePort : Port {} is not a DHCP server port for device owner {}",
158                       neutronPortId,deviceOwner);
159             return false;
160         }
161         return true;
162     }
163
164     @Override
165     public boolean isLastPortinSubnet(Node node, OvsdbTerminationPointAugmentation terminationPointAugmentation) {
166         if (neutronPortCache == null) {
167             LOG.error("isLastPortinSubnet: neutron port is null");
168             return false;
169         }
170         try {
171             LOG.trace("isLastPortinSubnet: for {}", terminationPointAugmentation.getName());
172             String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
173                                                                            Constants.EXTERNAL_ID_INTERFACE_ID);
174             if (neutronPortId == null) {
175                 return false;
176             }
177             NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
178             if (neutronPort == null) {
179                 LOG.error("isLastPortinSubnet: neutron port of {} is not found", neutronPortId);
180                 return false;
181             }
182             List<Neutron_IPs> neutronPortFixedIp = neutronPort.getFixedIPs();
183             if (null == neutronPortFixedIp || neutronPortFixedIp.isEmpty()) {
184                 return false;
185             }
186             /*Get all the ports in the current node and check whether there
187              * is any port belonging to the same subnet of the input
188              */
189             List<TerminationPoint> terminationPoints = node.getTerminationPoint();
190             if (terminationPoints != null && !terminationPoints.isEmpty()) {
191                 for (TerminationPoint tp : terminationPoints) {
192                     OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation =
193                             tp.getAugmentation(OvsdbTerminationPointAugmentation.class);
194                     if (ovsdbTerminationPointAugmentation != null && !ovsdbTerminationPointAugmentation
195                             .getName().equals(Constants.INTEGRATION_BRIDGE)) {
196                         String portId = southbound.getInterfaceExternalIdsValue(ovsdbTerminationPointAugmentation,
197                                                                                 Constants.EXTERNAL_ID_INTERFACE_ID);
198                         if (null != portId) {
199                             NeutronPort port = neutronPortCache.getPort(portId);
200                             if (null != port) {
201                                 if (!(port.getID().equals(neutronPort.getID()))
202                                         && port.getDeviceOwner().contains("compute")) {
203                                     List<Neutron_IPs> portFixedIp = port.getFixedIPs();
204                                     if (null == portFixedIp || portFixedIp.isEmpty()) {
205                                         return false;
206                                     }
207                                     if (portFixedIp.iterator().next().getSubnetUUID()
208                                             .equals(neutronPort.getFixedIPs().iterator().next().getSubnetUUID())) {
209                                         LOG.trace("isLastPortinSubnet: Port is not the only port.");
210                                         return false;
211                                     }
212                                 }
213                             }
214                         }
215                     }
216                 }
217             }
218         } catch (Exception e) {
219             LOG.error("isLastPortinSubnet: isLastPortinSubnet failed due to ", e);
220             return false;
221         }
222         return true;
223     }
224
225     @Override
226     public boolean isLastPortinBridge(Node node, OvsdbTerminationPointAugmentation terminationPointAugmentation) {
227         LOG.trace("isLastPortinBridge: for {}", terminationPointAugmentation.getName());
228         List<TerminationPoint> terminationPoints = node.getTerminationPoint();
229         /*Check whether the node has any port other than br-int*/
230         if (terminationPoints != null && !terminationPoints.isEmpty()) {
231             for (TerminationPoint tp : terminationPoints) {
232                 OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation =
233                         tp.getAugmentation(OvsdbTerminationPointAugmentation.class);
234                 if (null != ovsdbTerminationPointAugmentation) {
235                     if (!(ovsdbTerminationPointAugmentation.getName().equals(Constants.INTEGRATION_BRIDGE))
236                             && !(terminationPointAugmentation.getInterfaceUuid()
237                                     .equals(ovsdbTerminationPointAugmentation.getInterfaceUuid()))) {
238                         LOG.debug("isLastPortinBridge: it the last port in bridge {}",
239                                   terminationPointAugmentation.getName());
240                         return false;
241                     }
242                 }
243             }
244         }
245         return true;
246     }
247
248     @Override
249     public List<Neutron_IPs> getIpAddressList(Node node,
250                                           OvsdbTerminationPointAugmentation terminationPointAugmentation) {
251         if (neutronPortCache == null) {
252             LOG.error("getIpAddress: neutron port is null");
253             return null;
254         }
255         LOG.trace("getIpAddress: for {}", terminationPointAugmentation.getName());
256         String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
257                                                                        Constants.EXTERNAL_ID_INTERFACE_ID);
258         if (neutronPortId == null) {
259             return null;
260         }
261         NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
262         if (neutronPort == null) {
263             LOG.error("getIpAddress: neutron port of {} is not found", neutronPortId);
264             return null;
265         }
266         return neutronPort.getFixedIPs();
267     }
268
269     @Override
270     public List<Neutron_IPs> getVmListForSecurityGroup(List<Neutron_IPs> srcAddressList, String securityGroupUuid) {
271         List<Neutron_IPs> vmListForSecurityGroup = new ArrayList<Neutron_IPs>();
272         /*For every port check whether security grouplist contains the current
273          * security group.*/
274         try {
275             for (NeutronPort neutronPort:neutronPortCache.getAllPorts()) {
276                 if (!neutronPort.getDeviceOwner().contains("compute")) {
277                     LOG.debug("getVMListForSecurityGroup : the port {} is not "
278                             + "compute port belongs to {}", neutronPort.getID(), neutronPort.getDeviceOwner());
279                     continue;
280                 }
281                 List<NeutronSecurityGroup> securityGroups = neutronPort.getSecurityGroups();
282                 if (null != securityGroups) {
283                     for (NeutronSecurityGroup securityGroup:securityGroups) {
284                         if (securityGroup.getSecurityGroupUUID().equals(securityGroupUuid)
285                                 && !neutronPort.getFixedIPs().containsAll(srcAddressList)) {
286                             LOG.debug("getVMListForSecurityGroup : adding ports with ips {} "
287                                     + "compute port", neutronPort.getFixedIPs());
288                             vmListForSecurityGroup.addAll(neutronPort.getFixedIPs());
289                         }
290                     }
291                 }
292
293             }
294         } catch (Exception e) {
295             LOG.error("getVMListForSecurityGroup: getVMListForSecurityGroup"
296                     + " failed due to ", e);
297             return null;
298         }
299         return vmListForSecurityGroup;
300
301     }
302
303     @Override
304     public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
305         southbound =
306                 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
307     }
308
309     @Override
310     public void setDependencies(Object impl) {
311         if (impl instanceof INeutronPortCRUD) {
312             neutronPortCache = (INeutronPortCRUD)impl;
313         } else if (impl instanceof INeutronSubnetCRUD) {
314             neutronSubnetCache = (INeutronSubnetCRUD) impl;
315         }
316     }
317 }