Added IPV4, Custom TCP and UDP SG rules.
[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             /*Since all the fixed ip assigned to a port should be
109              *from the same network, first port is sufficient.*/
110             List<Neutron_IPs> fixedIps = neutronPort.getFixedIPs();
111             if (null == fixedIps || 0 == fixedIps.size() ) {
112                 LOG.error("getDHCPServerPort: No fixed ip is assigned");
113                 return null;
114             }
115             /* Get all the ports in the subnet and identify the dhcp port*/
116             String subnetUuid = fixedIps.iterator().next().getSubnetUUID();
117             NeutronSubnet neutronSubnet = neutronSubnetCache.getSubnet(subnetUuid);
118             List<NeutronPort> ports = neutronSubnet.getPortsInSubnet();
119             for (NeutronPort port : ports) {
120                 if (port.getDeviceOwner().contains("dhcp")) {
121                     return port;
122                 }
123             }
124         } catch (Exception e) {
125             LOG.error("getDHCPServerPort:getDHCPServerPort failed due to ", e);
126             return null;
127         }
128
129         return null;
130
131     }
132
133     @Override
134     public boolean isComputePort(OvsdbTerminationPointAugmentation terminationPointAugmentation) {
135         if (neutronPortCache == null) {
136             LOG.error("neutron port is null");
137             return false;
138         }
139         LOG.trace("isComputePort for {}", terminationPointAugmentation.getName());
140         String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
141                                                                        Constants.EXTERNAL_ID_INTERFACE_ID);
142         if (neutronPortId == null) {
143             return false;
144         }
145         NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
146         if (neutronPort == null) {
147             return false;
148         }
149         /*Check the device owner and if it contains compute to identify
150          * whether it is a compute port.*/
151         String deviceOwner = neutronPort.getDeviceOwner();
152         if (!deviceOwner.contains("compute")) {
153             LOG.debug("isComputePort : Port {} is not a DHCP server port for device owner {}",
154                       neutronPortId,deviceOwner);
155             return false;
156         }
157         return true;
158     }
159
160     @Override
161     public boolean isLastPortinSubnet(Node node, OvsdbTerminationPointAugmentation terminationPointAugmentation) {
162         if (neutronPortCache == null) {
163             LOG.error("isLastPortinSubnet: neutron port is null");
164             return false;
165         }
166         try {
167             LOG.trace("isLastPortinSubnet: for {}", terminationPointAugmentation.getName());
168             String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
169                                                                            Constants.EXTERNAL_ID_INTERFACE_ID);
170             if (neutronPortId == null) {
171                 return false;
172             }
173             NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
174             List<Neutron_IPs> neutronPortFixedIp = neutronPort.getFixedIPs();
175             if (null == neutronPortFixedIp || neutronPortFixedIp.isEmpty()) {
176                 return false;
177             }
178             /*Get all the ports in the current node and check whether there
179              * is any port belonging to the same subnet of the input
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()))
194                                         && port.getDeviceOwner().contains("compute")) {
195                                     List<Neutron_IPs> portFixedIp = port.getFixedIPs();
196                                     if (null == portFixedIp || portFixedIp.isEmpty()) {
197                                         return false;
198                                     }
199                                     if (portFixedIp.iterator().next().getSubnetUUID()
200                                             .equals(neutronPort.getFixedIPs().iterator().next().getSubnetUUID())) {
201                                         LOG.trace("isLastPortinSubnet: Port is not the only port.");
202                                         return false;
203                                     }
204                                 }
205                             }
206                         }
207                     }
208                 }
209             }
210         } catch (Exception e) {
211             LOG.error("isLastPortinSubnet: isLastPortinSubnet failed due to ", e);
212             return false;
213         }
214         return true;
215     }
216
217     @Override
218     public boolean isLastPortinBridge(Node node, OvsdbTerminationPointAugmentation terminationPointAugmentation) {
219         LOG.trace("isLastPortinBridge: for {}", terminationPointAugmentation.getName());
220         List<TerminationPoint> terminationPoints = node.getTerminationPoint();
221         /*Check whether the node has any port other than br-int*/
222         if (terminationPoints != null && !terminationPoints.isEmpty()) {
223             for (TerminationPoint tp : terminationPoints) {
224                 OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation =
225                         tp.getAugmentation(OvsdbTerminationPointAugmentation.class);
226                 if (null != ovsdbTerminationPointAugmentation) {
227                     if (!(ovsdbTerminationPointAugmentation.getName().equals(Constants.INTEGRATION_BRIDGE))
228                             && !(terminationPointAugmentation.getInterfaceUuid()
229                                     .equals(ovsdbTerminationPointAugmentation.getInterfaceUuid()))) {
230                         LOG.debug("isLastPortinBridge: it the last port in bridge {}",
231                                   terminationPointAugmentation.getName());
232                         return false;
233                     }
234                 }
235             }
236         }
237         return true;
238     }
239
240     @Override
241     public List<Neutron_IPs> getIpAddressList(Node node,
242                                           OvsdbTerminationPointAugmentation terminationPointAugmentation) {
243         if (neutronPortCache == null) {
244             LOG.error("getIpAddress: neutron port is null");
245             return null;
246         }
247         LOG.trace("getIpAddress: for {}", terminationPointAugmentation.getName());
248         String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
249                                                                        Constants.EXTERNAL_ID_INTERFACE_ID);
250         if (neutronPortId == null) {
251             return null;
252         }
253         NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
254         return neutronPort.getFixedIPs();
255     }
256
257     @Override
258     public List<Neutron_IPs> getVmListForSecurityGroup(List<Neutron_IPs> srcAddressList, String securityGroupUuid) {
259         List<Neutron_IPs> vmListForSecurityGroup = new ArrayList<Neutron_IPs>();
260         /*For every port check whether security grouplist contains the current
261          * security group.*/
262         try {
263             for (NeutronPort neutronPort:neutronPortCache.getAllPorts()) {
264                 if (!neutronPort.getDeviceOwner().contains("compute")) {
265                     LOG.debug("getVMListForSecurityGroup : the port {} is not "
266                             + "compute port belongs to {}", neutronPort.getID(), neutronPort.getDeviceOwner());
267                     continue;
268                 }
269                 List<NeutronSecurityGroup> securityGroups = neutronPort.getSecurityGroups();
270                 if (null != securityGroups) {
271                     for (NeutronSecurityGroup securityGroup:securityGroups) {
272                         if (securityGroup.getSecurityGroupUUID().equals(securityGroupUuid)
273                                 && !neutronPort.getFixedIPs().containsAll(srcAddressList)) {
274                             LOG.debug("getVMListForSecurityGroup : adding ports with ips {} "
275                                     + "compute port", neutronPort.getFixedIPs());
276                             vmListForSecurityGroup.addAll(neutronPort.getFixedIPs());
277                         }
278                     }
279                 }
280
281             }
282         } catch (Exception e) {
283             LOG.error("getVMListForSecurityGroup: getVMListForSecurityGroup"
284                     + " failed due to ", e);
285             return null;
286         }
287         return vmListForSecurityGroup;
288
289     }
290
291     @Override
292     public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
293         southbound =
294                 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
295     }
296
297     @Override
298     public void setDependencies(Object impl) {
299         if (impl instanceof INeutronPortCRUD) {
300             neutronPortCache = (INeutronPortCRUD)impl;
301         } else if (impl instanceof INeutronSubnetCRUD) {
302             neutronSubnetCache = (INeutronSubnetCRUD) impl;
303         }
304     }
305 }