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.List;
13 import org.opendaylight.neutron.spi.INeutronPortCRUD;
14 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
15 import org.opendaylight.neutron.spi.NeutronPort;
16 import org.opendaylight.neutron.spi.NeutronSecurityGroup;
17 import org.opendaylight.neutron.spi.NeutronSubnet;
18 import org.opendaylight.neutron.spi.Neutron_IPs;
19 import org.opendaylight.ovsdb.openstack.netvirt.ConfigInterface;
20 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
21 import org.opendaylight.ovsdb.openstack.netvirt.api.SecurityServicesManager;
22 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
23 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.*;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
27 import org.osgi.framework.BundleContext;
28 import org.osgi.framework.ServiceReference;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class SecurityServicesImpl implements ConfigInterface, SecurityServicesManager {
33 private static final Logger LOG = LoggerFactory.getLogger(TenantNetworkManagerImpl.class);
34 private volatile INeutronPortCRUD neutronPortCache;
35 private volatile INeutronSubnetCRUD neutronSubnetCache;
36 private volatile Southbound southbound;
39 * Is security group ready.
41 * @param terminationPointAugmentation the intf
44 public boolean isPortSecurityReady(OvsdbTerminationPointAugmentation terminationPointAugmentation) {
45 if (neutronPortCache == null) {
46 LOG.error("neutron port is null");
49 LOG.trace("isPortSecurityReady for {}", terminationPointAugmentation.getName());
50 String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
51 Constants.EXTERNAL_ID_INTERFACE_ID);
52 if (neutronPortId == null) {
55 NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
56 if (neutronPort == null) {
59 String deviceOwner = neutronPort.getDeviceOwner();
60 if (!deviceOwner.contains("compute")) {
61 LOG.debug("Port {} is not a compute host, it is a: {}", neutronPortId, deviceOwner);
63 LOG.debug("isPortSecurityReady() is a {} ", deviceOwner);
64 List<NeutronSecurityGroup> securityGroups = neutronPort.getSecurityGroups();
65 if (securityGroups.isEmpty()) {
66 LOG.debug("Check for device: {} does not contain a Security Group for port: {}", deviceOwner,
70 LOG.debug("Security Group Check {} DOES contain a Neutron Security Group", neutronPortId);
75 * Gets security group in port.
77 * @param terminationPointAugmentation the intf
78 * @return the security group in port
80 public NeutronSecurityGroup getSecurityGroupInPort(OvsdbTerminationPointAugmentation terminationPointAugmentation) {
81 if (neutronPortCache == null) {
82 LOG.error("neutron port is null");
85 LOG.trace("isPortSecurityReady for {}", terminationPointAugmentation.getName());
86 String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
87 Constants.EXTERNAL_ID_INTERFACE_ID);
88 if (neutronPortId == null) {
91 NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
92 if (neutronPort == null) {
96 List<NeutronSecurityGroup> neutronSecurityGroups = neutronPort.getSecurityGroups();
97 if (neutronSecurityGroups != null) {
98 return (NeutronSecurityGroup) neutronSecurityGroups.toArray()[0];
105 public NeutronPort getDHCPServerPort(
106 OvsdbTerminationPointAugmentation terminationPointAugmentation) {
107 if (neutronPortCache == null) {
108 LOG.error("getDHCPServerPort: neutron port is null");
111 LOG.trace("getDHCPServerPort for {}",
112 terminationPointAugmentation.getName());
113 String neutronPortId = southbound.getInterfaceExternalIdsValue(
114 terminationPointAugmentation,
115 Constants.EXTERNAL_ID_INTERFACE_ID);
116 if (neutronPortId == null) {
119 NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
120 //Since all the fixed ip assigned to a port should be from the same network, first port is sufficient.
121 List<Neutron_IPs> fixedIps = neutronPort.getFixedIPs();
122 if(null==fixedIps || 0 == fixedIps.size() )
124 LOG.error("getDHCPServerPort: No fixed ip is assigned");
127 String subnetUUID = fixedIps.iterator().next().getSubnetUUID();
128 NeutronSubnet neutronSubnet = neutronSubnetCache.getSubnet(subnetUUID);
129 List<NeutronPort> ports = neutronSubnet.getPortsInSubnet();
130 for (NeutronPort port : ports) {
131 if (port.getDeviceOwner().contains("dhcp")) {
141 public boolean isComputePort(OvsdbTerminationPointAugmentation terminationPointAugmentation) {
142 if (neutronPortCache == null) {
143 LOG.error("neutron port is null");
146 LOG.trace("isComputePort for {}", terminationPointAugmentation.getName());
147 String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
148 Constants.EXTERNAL_ID_INTERFACE_ID);
149 if (neutronPortId == null) {
152 NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
153 if (neutronPort == null) {
156 String deviceOwner = neutronPort.getDeviceOwner();
157 if (!deviceOwner.contains("compute")) {
158 LOG.debug("isComputePort : Port {} is not a DHCP server port", neutronPortId, deviceOwner);
165 public boolean isLastPortinSubnet(Node node, OvsdbTerminationPointAugmentation terminationPointAugmentation) {
166 if (neutronPortCache == null) {
167 LOG.error("isLastPortinSubnet: neutron port is null");
170 LOG.trace("isLastPortinSubnet: for {}", terminationPointAugmentation.getName());
171 String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
172 Constants.EXTERNAL_ID_INTERFACE_ID);
173 if (neutronPortId == null) {
176 NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
177 List<Neutron_IPs> neutronPortFixedIp = neutronPort.getFixedIPs();
178 if(null == neutronPortFixedIp || neutronPortFixedIp.isEmpty()) {
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);
191 NeutronPort port = neutronPortCache.getPort(portId);
193 if(!(port.getID().equals(neutronPort.getID())) && port.getDeviceOwner().contains("compute")) {
194 List<Neutron_IPs> portFixedIp = port.getFixedIPs();
195 if(null == portFixedIp || portFixedIp.isEmpty()) {
198 if(portFixedIp.iterator().next().getSubnetUUID().equals
199 (neutronPort.getFixedIPs().iterator().next().getSubnetUUID())) {
212 public boolean isLastPortinBridge(Node node, OvsdbTerminationPointAugmentation terminationPointAugmentation) {
213 LOG.trace("isLastPortinBridge: for {}", terminationPointAugmentation.getName());
214 List<TerminationPoint> terminationPoints = node.getTerminationPoint();
215 if(terminationPoints != null && !terminationPoints.isEmpty()){
216 for(TerminationPoint tp : terminationPoints){
217 OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation =
218 tp.getAugmentation(OvsdbTerminationPointAugmentation.class);
219 if(null!=ovsdbTerminationPointAugmentation)
221 if(!(ovsdbTerminationPointAugmentation.getName().equals(Constants.INTEGRATION_BRIDGE))
222 && !(terminationPointAugmentation.getInterfaceUuid().equals
223 (ovsdbTerminationPointAugmentation.getInterfaceUuid()))) {
233 public List<Neutron_IPs> getIpAddress(Node node,
234 OvsdbTerminationPointAugmentation terminationPointAugmentation) {
235 if (neutronPortCache == null) {
236 LOG.error("getIpAddress: neutron port is null");
239 LOG.trace("getIpAddress: for {}", terminationPointAugmentation.getName());
240 String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
241 Constants.EXTERNAL_ID_INTERFACE_ID);
242 if (neutronPortId == null) {
245 NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
246 return neutronPort.getFixedIPs();
250 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
252 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
256 public void setDependencies(Object impl) {
257 if (impl instanceof INeutronPortCRUD) {
258 neutronPortCache = (INeutronPortCRUD)impl;
260 else if (impl instanceof INeutronSubnetCRUD) {
261 neutronSubnetCache = (INeutronSubnetCRUD) impl;