2 * Copyright (c) 2014, 2015 Red Hat, Inc. and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.openstack.netvirt.impl;
11 import java.util.ArrayList;
12 import java.util.List;
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;
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;
40 public boolean isPortSecurityReady(OvsdbTerminationPointAugmentation terminationPointAugmentation) {
41 if (neutronPortCache == null) {
42 LOG.error("neutron port is null");
45 LOG.trace("isPortSecurityReady for {}", terminationPointAugmentation.getName());
46 String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
47 Constants.EXTERNAL_ID_INTERFACE_ID);
48 if (neutronPortId == null) {
51 NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
52 if (neutronPort == null) {
55 String deviceOwner = neutronPort.getDeviceOwner();
56 if (!deviceOwner.contains("compute")) {
57 LOG.debug("Port {} is not a compute host, it is a: {}", neutronPortId, deviceOwner);
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,
66 LOG.debug("Security Group Check {} does contain a Neutron Security Group", neutronPortId);
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;
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;
84 NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
85 if (neutronPort == null) {
86 return neutronSecurityGroups;
88 neutronSecurityGroups = neutronPort.getSecurityGroups();
89 return neutronSecurityGroups;
94 public NeutronPort getDhcpServerPort(OvsdbTerminationPointAugmentation terminationPointAugmentation) {
95 if (neutronPortCache == null) {
96 LOG.error("getDHCPServerPort: neutron port is null");
99 LOG.trace("getDHCPServerPort for {}",
100 terminationPointAugmentation.getName());
102 String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
103 Constants.EXTERNAL_ID_INTERFACE_ID);
104 if (neutronPortId == null) {
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");
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")) {
124 } catch (Exception e) {
125 LOG.error("getDHCPServerPort:getDHCPServerPort failed due to ", e);
134 public boolean isComputePort(OvsdbTerminationPointAugmentation terminationPointAugmentation) {
135 if (neutronPortCache == null) {
136 LOG.error("neutron port is null");
139 LOG.trace("isComputePort for {}", terminationPointAugmentation.getName());
140 String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
141 Constants.EXTERNAL_ID_INTERFACE_ID);
142 if (neutronPortId == null) {
145 NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
146 if (neutronPort == null) {
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);
161 public boolean isLastPortinSubnet(Node node, OvsdbTerminationPointAugmentation terminationPointAugmentation) {
162 if (neutronPortCache == null) {
163 LOG.error("isLastPortinSubnet: neutron port is null");
167 LOG.trace("isLastPortinSubnet: for {}", terminationPointAugmentation.getName());
168 String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
169 Constants.EXTERNAL_ID_INTERFACE_ID);
170 if (neutronPortId == null) {
173 NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
174 List<Neutron_IPs> neutronPortFixedIp = neutronPort.getFixedIPs();
175 if (null == neutronPortFixedIp || neutronPortFixedIp.isEmpty()) {
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
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);
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()) {
199 if (portFixedIp.iterator().next().getSubnetUUID()
200 .equals(neutronPort.getFixedIPs().iterator().next().getSubnetUUID())) {
201 LOG.trace("isLastPortinSubnet: Port is not the only port.");
210 } catch (Exception e) {
211 LOG.error("isLastPortinSubnet: isLastPortinSubnet failed due to ", e);
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());
241 public List<Neutron_IPs> getIpAddressList(Node node,
242 OvsdbTerminationPointAugmentation terminationPointAugmentation) {
243 if (neutronPortCache == null) {
244 LOG.error("getIpAddress: neutron port is null");
247 LOG.trace("getIpAddress: for {}", terminationPointAugmentation.getName());
248 String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
249 Constants.EXTERNAL_ID_INTERFACE_ID);
250 if (neutronPortId == null) {
253 NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
254 return neutronPort.getFixedIPs();
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
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());
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());
282 } catch (Exception e) {
283 LOG.error("getVMListForSecurityGroup: getVMListForSecurityGroup"
284 + " failed due to ", e);
287 return vmListForSecurityGroup;
292 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
294 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
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;