Merge "Revert "Remove incorrect or unnecessary relativePaths""
[ovsdb.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.List;
12
13 import org.opendaylight.neutron.spi.INeutronPortCRUD;
14 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
15 import org.opendaylight.neutron.spi.NeutronPort;
16 import org.opendaylight.neutron.spi.NeutronSecurityGroup;
17 import org.opendaylight.neutron.spi.NeutronSubnet;
18 import org.opendaylight.neutron.spi.Neutron_IPs;
19 import org.opendaylight.ovsdb.openstack.netvirt.ConfigInterface;
20 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
21 import org.opendaylight.ovsdb.openstack.netvirt.api.SecurityServicesManager;
22 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
23 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.*;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
27 import org.osgi.framework.BundleContext;
28 import org.osgi.framework.ServiceReference;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class SecurityServicesImpl implements ConfigInterface, SecurityServicesManager {
33     private static final Logger LOG = LoggerFactory.getLogger(TenantNetworkManagerImpl.class);
34     private volatile INeutronPortCRUD neutronPortCache;
35     private volatile INeutronSubnetCRUD neutronSubnetCache;
36     private volatile Southbound southbound;
37
38     /**
39      * Is security group ready.
40      *
41      * @param terminationPointAugmentation the intf
42      * @return the boolean
43      */
44     public boolean isPortSecurityReady(OvsdbTerminationPointAugmentation terminationPointAugmentation) {
45         if (neutronPortCache == null) {
46             LOG.error("neutron port is null");
47             return false;
48         }
49         LOG.trace("isPortSecurityReady for {}", terminationPointAugmentation.getName());
50         String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
51                 Constants.EXTERNAL_ID_INTERFACE_ID);
52         if (neutronPortId == null) {
53             return false;
54         }
55         NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
56         if (neutronPort == null) {
57             return false;
58         }
59         String deviceOwner = neutronPort.getDeviceOwner();
60         if (!deviceOwner.contains("compute")) {
61             LOG.debug("Port {} is not a compute host, it is a: {}", neutronPortId, deviceOwner);
62         }
63         LOG.debug("isPortSecurityReady() is a {} ", deviceOwner);
64         List<NeutronSecurityGroup> securityGroups = neutronPort.getSecurityGroups();
65         if (securityGroups.isEmpty()) {
66             LOG.debug("Check for device: {} does not contain a Security Group for port: {}", deviceOwner,
67                     neutronPortId);
68             return false;
69         }
70         LOG.debug("Security Group Check {} DOES contain a Neutron Security Group", neutronPortId);
71         return true;
72     }
73
74     /**
75      * Gets security group in port.
76      *
77      * @param terminationPointAugmentation the intf
78      * @return the security group in port
79      */
80     public NeutronSecurityGroup getSecurityGroupInPort(OvsdbTerminationPointAugmentation terminationPointAugmentation) {
81         if (neutronPortCache == null) {
82             LOG.error("neutron port is null");
83             return null;
84         }
85         LOG.trace("isPortSecurityReady for {}", terminationPointAugmentation.getName());
86         String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
87                 Constants.EXTERNAL_ID_INTERFACE_ID);
88         if (neutronPortId == null) {
89             return null;
90         }
91         NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
92         if (neutronPort == null) {
93             return null;
94         }
95
96         List<NeutronSecurityGroup> neutronSecurityGroups = neutronPort.getSecurityGroups();
97         if (neutronSecurityGroups != null) {
98             return (NeutronSecurityGroup) neutronSecurityGroups.toArray()[0];
99         } else {
100             return null;
101         }
102     }
103
104     @Override
105     public NeutronPort getDHCPServerPort(
106             OvsdbTerminationPointAugmentation terminationPointAugmentation) {
107         if (neutronPortCache == null) {
108             LOG.error("getDHCPServerPort: neutron port is null");
109             return null;
110         }
111         LOG.trace("getDHCPServerPort for {}",
112                 terminationPointAugmentation.getName());
113         String neutronPortId = southbound.getInterfaceExternalIdsValue(
114                 terminationPointAugmentation,
115                 Constants.EXTERNAL_ID_INTERFACE_ID);
116         if (neutronPortId == null) {
117             return null;
118         }
119         NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
120         //Since all the fixed ip assigned to a port should be from the same network, first port is sufficient.
121         List<Neutron_IPs> fixedIps = neutronPort.getFixedIPs();
122         if(null==fixedIps || 0 == fixedIps.size() )
123         {
124             LOG.error("getDHCPServerPort: No fixed ip is assigned");
125             return null;
126         }
127         String subnetUUID = fixedIps.iterator().next().getSubnetUUID();
128         NeutronSubnet neutronSubnet = neutronSubnetCache.getSubnet(subnetUUID);
129         List<NeutronPort> ports = neutronSubnet.getPortsInSubnet();
130         for (NeutronPort port : ports) {
131             if (port.getDeviceOwner().contains("dhcp")) {
132                 return port;
133             }
134         }
135
136         return null;
137
138     }
139
140     @Override
141     public boolean isComputePort(OvsdbTerminationPointAugmentation terminationPointAugmentation) {
142         if (neutronPortCache == null) {
143             LOG.error("neutron port is null");
144             return false;
145         }
146         LOG.trace("isComputePort for {}", terminationPointAugmentation.getName());
147         String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
148                 Constants.EXTERNAL_ID_INTERFACE_ID);
149         if (neutronPortId == null) {
150             return false;
151         }
152         NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
153         if (neutronPort == null) {
154             return false;
155         }
156         String deviceOwner = neutronPort.getDeviceOwner();
157         if (!deviceOwner.contains("compute")) {
158             LOG.debug("isComputePort : Port {} is not a DHCP server port", 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         LOG.trace("isLastPortinSubnet: for {}", terminationPointAugmentation.getName());
171         String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
172                                                                        Constants.EXTERNAL_ID_INTERFACE_ID);
173         if (neutronPortId == null) {
174             return false;
175         }
176         NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
177         List<Neutron_IPs> neutronPortFixedIp = neutronPort.getFixedIPs();
178         if(null == neutronPortFixedIp || neutronPortFixedIp.isEmpty()) {
179             return false;
180         }
181         List<TerminationPoint> terminationPoints = node.getTerminationPoint();
182         if(terminationPoints != null && !terminationPoints.isEmpty()) {
183             for(TerminationPoint tp : terminationPoints) {
184                 OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation =
185                         tp.getAugmentation(OvsdbTerminationPointAugmentation.class);
186                 if (ovsdbTerminationPointAugmentation != null && !ovsdbTerminationPointAugmentation.
187                         getName().equals(Constants.INTEGRATION_BRIDGE)) {
188                     String portId = southbound.getInterfaceExternalIdsValue(ovsdbTerminationPointAugmentation,
189                                                                             Constants.EXTERNAL_ID_INTERFACE_ID);
190                     if(null!=portId) {
191                         NeutronPort port = neutronPortCache.getPort(portId);
192                         if(null!=port) {
193                             if(!(port.getID().equals(neutronPort.getID())) && port.getDeviceOwner().contains("compute")) {
194                                 List<Neutron_IPs> portFixedIp = port.getFixedIPs();
195                                 if(null == portFixedIp || portFixedIp.isEmpty()) {
196                                     return false;
197                                 }
198                                 if(portFixedIp.iterator().next().getSubnetUUID().equals
199                                         (neutronPort.getFixedIPs().iterator().next().getSubnetUUID())) {
200                                     return false;
201                                 }
202                             }
203                         }
204                     }
205                 }
206             }
207         }
208         return true;
209     }
210
211     @Override
212     public boolean isLastPortinBridge(Node node, OvsdbTerminationPointAugmentation terminationPointAugmentation) {
213         LOG.trace("isLastPortinBridge: for {}", terminationPointAugmentation.getName());
214         List<TerminationPoint> terminationPoints = node.getTerminationPoint();
215         if(terminationPoints != null && !terminationPoints.isEmpty()){
216             for(TerminationPoint tp : terminationPoints){
217                 OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation =
218                         tp.getAugmentation(OvsdbTerminationPointAugmentation.class);
219                 if(null!=ovsdbTerminationPointAugmentation)
220                 {
221                     if(!(ovsdbTerminationPointAugmentation.getName().equals(Constants.INTEGRATION_BRIDGE))
222                             && !(terminationPointAugmentation.getInterfaceUuid().equals
223                                     (ovsdbTerminationPointAugmentation.getInterfaceUuid()))) {
224                         return false;
225                     }
226                 }
227             }
228         }
229         return true;
230     }
231
232     @Override
233     public List<Neutron_IPs> getIpAddress(Node node,
234                                 OvsdbTerminationPointAugmentation terminationPointAugmentation) {
235         if (neutronPortCache == null) {
236             LOG.error("getIpAddress: neutron port is null");
237             return null;
238         }
239         LOG.trace("getIpAddress: for {}", terminationPointAugmentation.getName());
240         String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
241                 Constants.EXTERNAL_ID_INTERFACE_ID);
242         if (neutronPortId == null) {
243             return null;
244         }
245         NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
246         return neutronPort.getFixedIPs();
247     }
248
249     @Override
250     public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
251         southbound =
252                 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
253     }
254
255     @Override
256     public void setDependencies(Object impl) {
257         if (impl instanceof INeutronPortCRUD) {
258             neutronPortCache = (INeutronPortCRUD)impl;
259         }
260         else if (impl instanceof INeutronSubnetCRUD) {
261             neutronSubnetCache = (INeutronSubnetCRUD) impl;
262         }
263     }
264 }