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.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.node.attributes.SupportingNode;
27 import org.opendaylight.yangtools.yang.binding.DataContainer;
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 static final Logger logger = LoggerFactory.getLogger(TenantNetworkManagerImpl.class);
35 private volatile INeutronPortCRUD neutronPortCache;
36 private volatile INeutronSubnetCRUD neutronSubnetCache;
37 private volatile Southbound southbound;
40 * Is security group ready.
42 * @param terminationPointAugmentation the intf
45 public boolean isPortSecurityReady(OvsdbTerminationPointAugmentation terminationPointAugmentation) {
46 if (neutronPortCache == null) {
47 logger.error("neutron port is null");
50 logger.trace("isPortSecurityReady for {}", terminationPointAugmentation.getName());
51 String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
52 Constants.EXTERNAL_ID_INTERFACE_ID);
53 if (neutronPortId == null) {
56 NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
57 if (neutronPort == null) {
60 String deviceOwner = neutronPort.getDeviceOwner();
61 if (!deviceOwner.contains("compute")) {
62 logger.debug("Port {} is not a compute host, it is a: {}", neutronPortId, deviceOwner);
64 logger.debug("isPortSecurityReady() is a {} ", deviceOwner);
65 List<NeutronSecurityGroup> securityGroups = neutronPort.getSecurityGroups();
66 if (securityGroups.isEmpty()) {
67 logger.debug("Check for device: {} does not contain a Security Group for port: {}", deviceOwner,
71 String vmPort = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
72 Constants.EXTERNAL_ID_VM_MAC);
73 logger.debug("Security Group Check {} DOES contain a Neutron Security Group", neutronPortId);
78 * Gets security group in port.
80 * @param terminationPointAugmentation the intf
81 * @return the security group in port
83 public NeutronSecurityGroup getSecurityGroupInPort(OvsdbTerminationPointAugmentation terminationPointAugmentation) {
84 if (neutronPortCache == null) {
85 logger.error("neutron port is null");
88 logger.trace("isPortSecurityReady for {}", terminationPointAugmentation.getName());
89 String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
90 Constants.EXTERNAL_ID_INTERFACE_ID);
91 if (neutronPortId == null) {
94 NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
95 if (neutronPort == null) {
99 List<NeutronSecurityGroup> neutronSecurityGroups = neutronPort.getSecurityGroups();
100 if (neutronSecurityGroups != null) {
101 NeutronSecurityGroup neutronSecurityGroup = (NeutronSecurityGroup) neutronSecurityGroups.toArray()[0];
102 return neutronSecurityGroup;
109 public NeutronPort getDHCPServerPort(
110 OvsdbTerminationPointAugmentation terminationPointAugmentation) {
111 if (neutronPortCache == null) {
112 logger.error("getDHCPServerPort: neutron port is null");
115 logger.trace("getDHCPServerPort for {}",
116 terminationPointAugmentation.getName());
117 String neutronPortId = southbound.getInterfaceExternalIdsValue(
118 terminationPointAugmentation,
119 Constants.EXTERNAL_ID_INTERFACE_ID);
120 if (neutronPortId == null) {
123 NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
124 //Since all the fixed ip assigned to a port should be from the same network, first port is sufficient.
125 List<Neutron_IPs> fixedIps = neutronPort.getFixedIPs();
126 if(null==fixedIps || 0 == fixedIps.size() )
128 logger.error("getDHCPServerPort: No fixed ip is assigned");
131 String subnetUUID = fixedIps.iterator().next().getSubnetUUID();
132 NeutronSubnet neutronSubnet = neutronSubnetCache.getSubnet(subnetUUID);
133 List<NeutronPort> ports = neutronSubnet.getPortsInSubnet();
134 for (NeutronPort port : ports) {
135 if (port.getDeviceOwner().contains("dhcp")) {
145 public boolean isComputePort(OvsdbTerminationPointAugmentation terminationPointAugmentation) {
146 if (neutronPortCache == null) {
147 logger.error("neutron port is null");
150 logger.trace("isComputePort for {}", terminationPointAugmentation.getName());
151 String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
152 Constants.EXTERNAL_ID_INTERFACE_ID);
153 if (neutronPortId == null) {
156 NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
157 if (neutronPort == null) {
160 String deviceOwner = neutronPort.getDeviceOwner();
161 if (!deviceOwner.contains("compute")) {
162 logger.debug("isComputePort : Port {} is not a DHCP server port", neutronPortId,deviceOwner);
169 public boolean isLastPortinSubnet(Node node, OvsdbTerminationPointAugmentation terminationPointAugmentation) {
170 if (neutronPortCache == null) {
171 logger.error("isLastPortinSubnet: neutron port is null");
174 logger.trace("isLastPortinSubnet: for {}", terminationPointAugmentation.getName());
175 String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
176 Constants.EXTERNAL_ID_INTERFACE_ID);
177 if (neutronPortId == null) {
180 NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
181 List<Neutron_IPs> neutronPortFixedIp = neutronPort.getFixedIPs();
182 if(null == neutronPortFixedIp || neutronPortFixedIp.isEmpty()) {
185 List<TerminationPoint> terminationPoints = node.getTerminationPoint();
186 if(terminationPoints != null && !terminationPoints.isEmpty()) {
187 for(TerminationPoint tp : terminationPoints) {
188 OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation =
189 tp.getAugmentation(OvsdbTerminationPointAugmentation.class);
190 if (ovsdbTerminationPointAugmentation != null && !ovsdbTerminationPointAugmentation.
191 getName().equals(Constants.INTEGRATION_BRIDGE)) {
192 String portId = southbound.getInterfaceExternalIdsValue(ovsdbTerminationPointAugmentation,
193 Constants.EXTERNAL_ID_INTERFACE_ID);
195 NeutronPort port = neutronPortCache.getPort(portId);
197 if(!(port.getID().equals(neutronPort.getID())) && port.getDeviceOwner().contains("compute")) {
198 List<Neutron_IPs> portFixedIp = port.getFixedIPs();
199 if(null == portFixedIp || portFixedIp.isEmpty()) {
202 if(portFixedIp.iterator().next().getSubnetUUID().equals
203 (neutronPort.getFixedIPs().iterator().next().getSubnetUUID())) {
216 public boolean isLastPortinBridge(Node node, OvsdbTerminationPointAugmentation terminationPointAugmentation) {
217 logger.trace("isLastPortinBridge: for {}", terminationPointAugmentation.getName());
218 List<TerminationPoint> terminationPoints = node.getTerminationPoint();
219 if(terminationPoints != null && !terminationPoints.isEmpty()){
220 for(TerminationPoint tp : terminationPoints){
221 OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation =
222 tp.getAugmentation(OvsdbTerminationPointAugmentation.class);
223 if(null!=ovsdbTerminationPointAugmentation)
225 if(!(ovsdbTerminationPointAugmentation.getName().equals(Constants.INTEGRATION_BRIDGE))
226 && !(terminationPointAugmentation.getInterfaceUuid().equals
227 (ovsdbTerminationPointAugmentation.getInterfaceUuid()))) {
237 public List<Neutron_IPs> getIpAddress(Node node,
238 OvsdbTerminationPointAugmentation terminationPointAugmentation) {
239 if (neutronPortCache == null) {
240 logger.error("getIpAddress: neutron port is null");
243 logger.trace("getIpAddress: for {}", terminationPointAugmentation.getName());
244 String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
245 Constants.EXTERNAL_ID_INTERFACE_ID);
246 if (neutronPortId == null) {
249 NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
250 List<Neutron_IPs> fixedIps = neutronPort.getFixedIPs();
255 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
257 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
261 public void setDependencies(Object impl) {
262 if (impl instanceof INeutronPortCRUD) {
263 neutronPortCache = (INeutronPortCRUD)impl;
265 else if (impl instanceof INeutronSubnetCRUD) {
266 neutronSubnetCache = (INeutronSubnetCRUD) impl;