2 * Copyright (C) 2014 Red Hat, Inc.
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
8 package org.opendaylight.ovsdb.openstack.netvirt.impl;
10 import java.util.List;
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;
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;
38 * Is security group ready.
40 * @param terminationPointAugmentation the intf
43 public boolean isPortSecurityReady(OvsdbTerminationPointAugmentation terminationPointAugmentation) {
44 if (neutronPortCache == null) {
45 LOG.error("neutron port is null");
48 LOG.trace("isPortSecurityReady for {}", terminationPointAugmentation.getName());
49 String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
50 Constants.EXTERNAL_ID_INTERFACE_ID);
51 if (neutronPortId == null) {
54 NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
55 if (neutronPort == null) {
58 String deviceOwner = neutronPort.getDeviceOwner();
59 if (!deviceOwner.contains("compute")) {
60 LOG.debug("Port {} is not a compute host, it is a: {}", neutronPortId, deviceOwner);
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,
69 LOG.debug("Security Group Check {} DOES contain a Neutron Security Group", neutronPortId);
74 * Gets security group in port.
76 * @param terminationPointAugmentation the intf
77 * @return the security group in port
79 public NeutronSecurityGroup getSecurityGroupInPort(OvsdbTerminationPointAugmentation terminationPointAugmentation) {
80 if (neutronPortCache == null) {
81 LOG.error("neutron port is null");
84 LOG.trace("isPortSecurityReady for {}", terminationPointAugmentation.getName());
85 String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
86 Constants.EXTERNAL_ID_INTERFACE_ID);
87 if (neutronPortId == null) {
90 NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
91 if (neutronPort == null) {
95 List<NeutronSecurityGroup> neutronSecurityGroups = neutronPort.getSecurityGroups();
96 if (neutronSecurityGroups != null) {
97 return (NeutronSecurityGroup) neutronSecurityGroups.toArray()[0];
104 public NeutronPort getDHCPServerPort(
105 OvsdbTerminationPointAugmentation terminationPointAugmentation) {
106 if (neutronPortCache == null) {
107 LOG.error("getDHCPServerPort: neutron port is null");
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) {
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() )
123 LOG.error("getDHCPServerPort: No fixed ip is assigned");
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")) {
140 public boolean isComputePort(OvsdbTerminationPointAugmentation terminationPointAugmentation) {
141 if (neutronPortCache == null) {
142 LOG.error("neutron port is null");
145 LOG.trace("isComputePort for {}", terminationPointAugmentation.getName());
146 String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
147 Constants.EXTERNAL_ID_INTERFACE_ID);
148 if (neutronPortId == null) {
151 NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
152 if (neutronPort == null) {
155 String deviceOwner = neutronPort.getDeviceOwner();
156 if (!deviceOwner.contains("compute")) {
157 LOG.debug("isComputePort : Port {} is not a DHCP server port", neutronPortId, deviceOwner);
164 public boolean isLastPortinSubnet(Node node, OvsdbTerminationPointAugmentation terminationPointAugmentation) {
165 if (neutronPortCache == null) {
166 LOG.error("isLastPortinSubnet: neutron port is null");
169 LOG.trace("isLastPortinSubnet: for {}", terminationPointAugmentation.getName());
170 String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
171 Constants.EXTERNAL_ID_INTERFACE_ID);
172 if (neutronPortId == null) {
175 NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
176 List<Neutron_IPs> neutronPortFixedIp = neutronPort.getFixedIPs();
177 if(null == neutronPortFixedIp || neutronPortFixedIp.isEmpty()) {
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);
190 NeutronPort port = neutronPortCache.getPort(portId);
192 if(!(port.getID().equals(neutronPort.getID())) && port.getDeviceOwner().contains("compute")) {
193 List<Neutron_IPs> portFixedIp = port.getFixedIPs();
194 if(null == portFixedIp || portFixedIp.isEmpty()) {
197 if(portFixedIp.iterator().next().getSubnetUUID().equals
198 (neutronPort.getFixedIPs().iterator().next().getSubnetUUID())) {
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)
220 if(!(ovsdbTerminationPointAugmentation.getName().equals(Constants.INTEGRATION_BRIDGE))
221 && !(terminationPointAugmentation.getInterfaceUuid().equals
222 (ovsdbTerminationPointAugmentation.getInterfaceUuid()))) {
232 public List<Neutron_IPs> getIpAddress(Node node,
233 OvsdbTerminationPointAugmentation terminationPointAugmentation) {
234 if (neutronPortCache == null) {
235 LOG.error("getIpAddress: neutron port is null");
238 LOG.trace("getIpAddress: for {}", terminationPointAugmentation.getName());
239 String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
240 Constants.EXTERNAL_ID_INTERFACE_ID);
241 if (neutronPortId == null) {
244 NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
245 return neutronPort.getFixedIPs();
249 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
251 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
255 public void setDependencies(Object impl) {
256 if (impl instanceof INeutronPortCRUD) {
257 neutronPortCache = (INeutronPortCRUD)impl;
259 else if (impl instanceof INeutronSubnetCRUD) {
260 neutronSubnetCache = (INeutronSubnetCRUD) impl;