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