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.netvirt.openstack.netvirt.impl;
11 import org.opendaylight.netvirt.openstack.netvirt.ConfigInterface;
12 import org.opendaylight.netvirt.openstack.netvirt.api.ConfigurationService;
13 import org.opendaylight.netvirt.openstack.netvirt.api.Constants;
14 import org.opendaylight.netvirt.openstack.netvirt.api.EgressAclProvider;
15 import org.opendaylight.netvirt.openstack.netvirt.api.IngressAclProvider;
16 import org.opendaylight.netvirt.openstack.netvirt.api.SecurityServicesManager;
17 import org.opendaylight.netvirt.openstack.netvirt.api.Southbound;
18 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronNetwork;
19 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronPort;
20 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronSecurityGroup;
21 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronSecurityRule;
22 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronSubnet;
23 import org.opendaylight.netvirt.openstack.netvirt.translator.Neutron_IPs;
24 import org.opendaylight.netvirt.openstack.netvirt.translator.crud.INeutronNetworkCRUD;
25 import org.opendaylight.netvirt.openstack.netvirt.translator.crud.INeutronPortCRUD;
26 import org.opendaylight.netvirt.openstack.netvirt.translator.crud.INeutronSubnetCRUD;
27 import org.opendaylight.netvirt.utils.servicehelper.ServiceHelper;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
31 import org.osgi.framework.ServiceReference;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import java.util.ArrayList;
36 import java.util.List;
38 public class SecurityServicesImpl implements ConfigInterface, SecurityServicesManager {
40 private static final Logger LOG = LoggerFactory.getLogger(TenantNetworkManagerImpl.class);
41 private volatile INeutronPortCRUD neutronPortCache;
42 private volatile INeutronSubnetCRUD neutronSubnetCache;
43 private volatile Southbound southbound;
44 private volatile INeutronNetworkCRUD neutronNetworkCache;
45 private volatile ConfigurationService configurationService;
46 private volatile IngressAclProvider ingressAclProvider;
47 private volatile EgressAclProvider egressAclProvider;
48 private volatile NeutronL3Adapter neutronL3Adapter;
49 private boolean isConntrackEnabled = false;
51 public SecurityServicesImpl() {
55 public SecurityServicesImpl(boolean isConntrack) {
57 this.isConntrackEnabled = isConntrack;
61 public boolean isPortSecurityReady(OvsdbTerminationPointAugmentation terminationPointAugmentation) {
62 if (neutronPortCache == null) {
63 LOG.error("neutron port is null");
66 LOG.trace("isPortSecurityReady for {}", terminationPointAugmentation.getName());
67 String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
68 Constants.EXTERNAL_ID_INTERFACE_ID);
69 if (neutronPortId == null) {
72 NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
73 if (neutronPort == null) {
74 neutronPort = neutronL3Adapter.getPortFromCleanupCache(neutronPortId);
75 if (neutronPort == null) {
76 LOG.error("isPortSecurityReady for {}", terminationPointAugmentation.getName()
81 String deviceOwner = neutronPort.getDeviceOwner();
82 if (!deviceOwner.contains("compute")) {
83 LOG.debug("Port {} is not a compute host, it is a: {}", neutronPortId, deviceOwner);
85 LOG.debug("isPortSecurityReady() is a {} ", deviceOwner);
86 List<NeutronSecurityGroup> securityGroups = neutronPort.getSecurityGroups();
87 if (securityGroups.isEmpty()) {
88 LOG.debug("Check for device: {} does not contain a Security Group for port: {}", deviceOwner,
92 LOG.debug("Security Group Check {} does contain a Neutron Security Group", neutronPortId);
97 public List<NeutronSecurityGroup> getSecurityGroupInPortList(OvsdbTerminationPointAugmentation
98 terminationPointAugmentation) {
99 List<NeutronSecurityGroup> neutronSecurityGroups = new ArrayList<>();
100 if (neutronPortCache == null) {
101 LOG.error("neutron port is null");
102 return neutronSecurityGroups;
104 LOG.trace("getSecurityGroupInPortList for {}", terminationPointAugmentation.getName());
105 String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
106 Constants.EXTERNAL_ID_INTERFACE_ID);
107 if (neutronPortId == null) {
108 return neutronSecurityGroups;
110 NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
111 if (neutronPort == null) {
112 neutronPort = neutronL3Adapter.getPortFromCleanupCache(neutronPortId);
113 if (neutronPort == null) {
114 LOG.error("getSecurityGroupInPortList for {}", terminationPointAugmentation.getName()
116 return neutronSecurityGroups;
119 neutronSecurityGroups = neutronPort.getSecurityGroups();
120 return neutronSecurityGroups;
125 public NeutronPort getDhcpServerPort(OvsdbTerminationPointAugmentation terminationPointAugmentation) {
126 if (neutronPortCache == null) {
127 LOG.warn("getDHCPServerPort: neutron port cache is null");
129 LOG.trace("getDHCPServerPort for {}",
130 terminationPointAugmentation.getName());
131 NeutronPort neutronPort = null;
133 String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
134 Constants.EXTERNAL_ID_INTERFACE_ID);
135 if (neutronPortId == null) {
138 if (null != neutronPortCache) {
139 neutronPort = neutronPortCache.getPort(neutronPortId);
142 if (neutronPort == null) {
143 neutronPort = neutronL3Adapter.getPortFromCleanupCache(neutronPortId);
144 if (neutronPort == null) {
145 LOG.error("getDHCPServerPort: neutron port of {} is not found", neutronPortId);
148 LOG.info("getDHCPServerPort: neutron port of {} got from cleanupcache", neutronPortId);
151 /* if the current port is a DHCP port, return the same*/
152 if (neutronPort.getDeviceOwner().contains("dhcp")) {
155 /*Since all the fixed ip assigned to a port should be
156 *from the same network, first port is sufficient.*/
157 List<Neutron_IPs> fixedIps = neutronPort.getFixedIPs();
158 if (null == fixedIps || 0 == fixedIps.size() ) {
159 LOG.error("getDHCPServerPort: No fixed ip is assigned");
162 /* Get all the ports in the subnet and identify the dhcp port*/
163 String subnetUuid = fixedIps.iterator().next().getSubnetUUID();
164 NeutronSubnet neutronSubnet = neutronSubnetCache.getSubnet(subnetUuid);
165 if (neutronSubnet == null) {
166 LOG.error("getDHCPServerPort: No subnet is found for " + subnetUuid);
169 List<NeutronPort> ports = neutronSubnet.getPortsInSubnet();
170 for (NeutronPort port : ports) {
171 if (port.getDeviceOwner().contains("dhcp")) {
175 } catch (Exception e) {
176 LOG.error("getDHCPServerPort:getDHCPServerPort failed due to ", e);
183 public NeutronPort getNeutronPortFromDhcpIntf(
184 OvsdbTerminationPointAugmentation terminationPointAugmentation) {
185 if (neutronPortCache == null) {
186 LOG.error("getNeutronPortFromDhcpIntf: neutron port is null");
189 String neutronPortId = southbound.getInterfaceExternalIdsValue(
190 terminationPointAugmentation,
191 Constants.EXTERNAL_ID_INTERFACE_ID);
192 if (neutronPortId == null) {
195 NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
196 if (neutronPort == null) {
197 neutronPort = neutronL3Adapter.getPortFromCleanupCache(neutronPortId);
198 if (neutronPort == null) {
199 LOG.error("getNeutronPortFromDhcpIntf: neutron port of {} is not found", neutronPortId);
203 /* if the current port is a DHCP port, return true*/
204 if (neutronPort.getDeviceOwner().contains("dhcp")) {
205 LOG.trace("getNeutronPortFromDhcpIntf: neutronPort is a dhcp port", neutronPort );
213 public NeutronPort getNeutronPortFromCache(OvsdbTerminationPointAugmentation terminationPointAugmentation) {
214 NeutronPort neutronPort = null;
215 LOG.trace("getNeutronPortFromCache for {}",
216 terminationPointAugmentation.getName());
218 String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
219 Constants.EXTERNAL_ID_INTERFACE_ID);
220 if (neutronPortId == null) {
223 if (null != neutronPortCache) {
224 neutronPort = neutronPortCache.getPort(neutronPortId);
227 if (neutronPort == null) {
228 LOG.trace("getNeutronPortFromCache: neutron port of {} search in cleanupcache", neutronPortId);
230 neutronPort = neutronL3Adapter.getPortFromCleanupCache(neutronPortId);
231 if (neutronPort == null) {
232 LOG.error("getNeutronPortFromCache: neutron port of {} is not found", neutronPortId);
235 LOG.trace("getNeutronPortFromCache: neutron port of {} got from cleanupcache", neutronPortId);
238 } catch (Exception e) {
239 LOG.warn("getNeutronPortFromCache:getNeutronPortFromCache failed due to ", e);
248 public boolean isComputePort(OvsdbTerminationPointAugmentation terminationPointAugmentation) {
249 if (neutronPortCache == null) {
250 LOG.warn("isComputePort : neutronPortCache is null");
252 NeutronPort neutronPort = null;
253 LOG.trace("isComputePort for {}", terminationPointAugmentation.getName());
254 String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
255 Constants.EXTERNAL_ID_INTERFACE_ID);
256 if (neutronPortId == null) {
259 if (neutronPortCache != null) {
260 neutronPort = neutronPortCache.getPort(neutronPortId);
262 if (neutronPort == null) {
263 neutronPort = getNeutronPortFromCache(terminationPointAugmentation);
264 if (neutronPort == null) {
268 /*Check the device owner and if it contains compute to identify
269 * whether it is a compute port.*/
270 String deviceOwner = neutronPort.getDeviceOwner();
271 if (!deviceOwner.contains("compute")) {
272 LOG.debug("isComputePort : Port {} is not a DHCP server port for device owner {}",
273 neutronPortId,deviceOwner);
280 public boolean isLastPortinSubnet(Node node, OvsdbTerminationPointAugmentation terminationPointAugmentation) {
281 if (neutronPortCache == null) {
282 LOG.error("isLastPortinSubnet: neutronPortCache is null");
284 NeutronPort neutronPort = null;
286 LOG.trace("isLastPortinSubnet: for {}", terminationPointAugmentation.getName());
287 String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
288 Constants.EXTERNAL_ID_INTERFACE_ID);
289 if (neutronPortId == null) {
292 if (neutronPortCache != null) {
293 neutronPort = neutronPortCache.getPort(neutronPortId);
295 if (neutronPort == null) {
296 neutronPort = getNeutronPortFromCache(terminationPointAugmentation);
297 if (neutronPort == null) {
298 LOG.error("isLastPortinSubnet: neutron port of {} is not found", neutronPortId);
302 List<Neutron_IPs> neutronPortFixedIp = neutronPort.getFixedIPs();
303 if (null == neutronPortFixedIp || neutronPortFixedIp.isEmpty()) {
306 /*Get all the ports in the current node and check whether there
307 * is any port belonging to the same subnet of the input
309 List<TerminationPoint> terminationPoints = node.getTerminationPoint();
310 if (terminationPoints != null && !terminationPoints.isEmpty()) {
311 for (TerminationPoint tp : terminationPoints) {
312 OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation =
313 tp.getAugmentation(OvsdbTerminationPointAugmentation.class);
314 if (ovsdbTerminationPointAugmentation != null && !ovsdbTerminationPointAugmentation
315 .getName().equals(Constants.INTEGRATION_BRIDGE)) {
316 String portId = southbound.getInterfaceExternalIdsValue(ovsdbTerminationPointAugmentation,
317 Constants.EXTERNAL_ID_INTERFACE_ID);
318 if (null != portId) {
319 NeutronPort port = neutronPortCache.getPort(portId);
320 if (null != port && !(port.getID().equals(neutronPort.getID()))
321 && port.getDeviceOwner().contains("compute")) {
322 List<Neutron_IPs> portFixedIp = port.getFixedIPs();
323 if (null == portFixedIp || portFixedIp.isEmpty()) {
326 if (portFixedIp.iterator().next().getSubnetUUID()
327 .equals(neutronPort.getFixedIPs().iterator().next().getSubnetUUID())) {
328 LOG.trace("isLastPortinSubnet: Port is not the only port.");
336 } catch (Exception e) {
337 LOG.error("isLastPortinSubnet: isLastPortinSubnet failed due to ", e);
344 public boolean isLastPortinBridge(Node node, OvsdbTerminationPointAugmentation terminationPointAugmentation) {
345 LOG.trace("isLastPortinBridge: for {}", terminationPointAugmentation.getName());
346 List<TerminationPoint> terminationPoints = node.getTerminationPoint();
347 /*Check whether the node has any port other than br-int*/
348 if (terminationPoints != null && !terminationPoints.isEmpty()) {
349 for (TerminationPoint tp : terminationPoints) {
350 OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation =
351 tp.getAugmentation(OvsdbTerminationPointAugmentation.class);
352 if (null != ovsdbTerminationPointAugmentation
353 && !(ovsdbTerminationPointAugmentation.getName().equals(Constants.INTEGRATION_BRIDGE))
354 && !(terminationPointAugmentation.getInterfaceUuid()
355 .equals(ovsdbTerminationPointAugmentation.getInterfaceUuid()))) {
356 LOG.debug("isLastPortinBridge: it the last port in bridge {}",
357 terminationPointAugmentation.getName());
366 public List<Neutron_IPs> getIpAddressList(OvsdbTerminationPointAugmentation terminationPointAugmentation) {
367 if (neutronPortCache == null) {
368 LOG.warn("getIpAddress: neutronPortCache is null");
370 NeutronPort neutronPort = null;
371 LOG.trace("getIpAddress: for {}", terminationPointAugmentation.getName());
372 String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
373 Constants.EXTERNAL_ID_INTERFACE_ID);
374 if (neutronPortId == null) {
377 if (neutronPortCache != null) {
378 neutronPort = neutronPortCache.getPort(neutronPortId);
380 if (neutronPort == null) {
381 neutronPort = getNeutronPortFromCache(terminationPointAugmentation);
383 if (neutronPort == null) {
384 LOG.error("getIpAddress: neutron port of {} is not found", neutronPortId);
387 return neutronPort.getFixedIPs();
391 public List<Neutron_IPs> getVmListForSecurityGroup(String portUuid, String securityGroupUuid) {
392 List<Neutron_IPs> vmListForSecurityGroup = new ArrayList<>();
393 /*For every port check whether security grouplist contains the current
396 for (String neutronPortUuid:neutronL3Adapter.getPortCleanupCache().keySet()) {
397 NeutronPort neutronPort = neutronL3Adapter.getPortCleanupCache().get(neutronPortUuid);
398 if (!neutronPort.getDeviceOwner().contains("compute")) {
399 LOG.debug("getVMListForSecurityGroup : the port {} is not "
400 + "compute port belongs to {}", neutronPort.getID(), neutronPort.getDeviceOwner());
403 if (portUuid.equals(neutronPort.getID())) {
406 List<NeutronSecurityGroup> securityGroups = neutronPort.getSecurityGroups();
407 if (null != securityGroups) {
408 for (NeutronSecurityGroup securityGroup:securityGroups) {
409 if (securityGroup.getSecurityGroupUUID().equals(securityGroupUuid)) {
410 LOG.debug("getVMListForSecurityGroup : adding ports with ips {} "
411 + "compute port", neutronPort.getFixedIPs());
412 if (neutronPort.getFixedIPs() != null) {
413 vmListForSecurityGroup.addAll(neutronPort.getFixedIPs());
420 } catch (Exception e) {
421 LOG.error("getVMListForSecurityGroup: getVMListForSecurityGroup"
422 + " failed due to ", e);
425 return vmListForSecurityGroup;
430 public void syncSecurityGroup(NeutronPort port, List<NeutronSecurityGroup> securityGroupList, boolean write) {
431 LOG.trace("syncSecurityGroup:" + securityGroupList + " Write:" + write);
432 if (null != port && null != port.getSecurityGroups()) {
433 Node node = getNode(port);
437 NeutronNetwork neutronNetwork = neutronNetworkCache.getNetwork(port.getNetworkUUID());
438 if (null == neutronNetwork) {
439 neutronNetwork = neutronL3Adapter.getNetworkFromCleanupCache(port.getNetworkUUID());
441 if (neutronNetwork == null) {
444 String segmentationId = neutronNetwork.getProviderSegmentationID();
445 OvsdbTerminationPointAugmentation intf = getInterface(node, port);
449 long localPort = southbound.getOFPort(intf);
450 String attachedMac = southbound.getInterfaceExternalIdsValue(intf, Constants.EXTERNAL_ID_VM_MAC);
451 if (attachedMac == null) {
452 LOG.debug("programVlanRules: No AttachedMac seen in {}", intf);
455 long dpid = getDpidOfIntegrationBridge(node);
459 String neutronPortId = southbound.getInterfaceExternalIdsValue(intf,
460 Constants.EXTERNAL_ID_INTERFACE_ID);
461 if (neutronPortId == null) {
462 LOG.debug("syncSecurityGroup: No neutronPortId seen in {}", intf);
465 for (NeutronSecurityGroup securityGroupInPort:securityGroupList) {
466 ingressAclProvider.programPortSecurityGroup(dpid, segmentationId, attachedMac, localPort,
467 securityGroupInPort, neutronPortId, write);
468 egressAclProvider.programPortSecurityGroup(dpid, segmentationId, attachedMac, localPort,
469 securityGroupInPort, neutronPortId, write);
475 public void syncSecurityRule(NeutronPort port, NeutronSecurityRule securityRule,Neutron_IPs vmIp, boolean write) {
476 LOG.trace("syncSecurityGroup:" + securityRule + " Write:" + write);
477 if (null != port && null != port.getSecurityGroups()) {
478 Node node = getNode(port);
482 NeutronNetwork neutronNetwork = neutronNetworkCache.getNetwork(port.getNetworkUUID());
483 if (null == neutronNetwork) {
484 neutronNetwork = neutronL3Adapter.getNetworkFromCleanupCache(port.getNetworkUUID());
486 if (neutronNetwork == null) {
489 String segmentationId = neutronNetwork.getProviderSegmentationID();
490 OvsdbTerminationPointAugmentation intf = getInterface(node, port);
494 long localPort = southbound.getOFPort(intf);
495 String attachedMac = southbound.getInterfaceExternalIdsValue(intf, Constants.EXTERNAL_ID_VM_MAC);
496 if (attachedMac == null) {
497 LOG.debug("programVlanRules: No AttachedMac seen in {}", intf);
500 long dpid = getDpidOfIntegrationBridge(node);
504 if (NeutronSecurityRule.ETHERTYPE_IPV4.equals(securityRule.getSecurityRuleEthertype())) {
505 if (NeutronSecurityRule.DIRECTION_INGRESS.equals(securityRule.getSecurityRuleDirection())) {
506 ingressAclProvider.programPortSecurityRule(dpid, segmentationId, attachedMac, localPort,
507 securityRule, vmIp, write);
508 } else if (NeutronSecurityRule.DIRECTION_EGRESS.equals(securityRule.getSecurityRuleDirection())) {
509 egressAclProvider.programPortSecurityRule(dpid, segmentationId, attachedMac, localPort,
510 securityRule, vmIp, write);
516 private long getDpidOfIntegrationBridge(Node node) {
517 LOG.trace("getDpidOfIntegrationBridge:" + node);
519 if (southbound.getBridgeName(node).equals(configurationService.getIntegrationBridgeName())) {
520 dpid = getDpid(node);
523 LOG.warn("getDpidOfIntegerationBridge: dpid not found: {}", node);
528 private long getDpid(Node node) {
529 LOG.trace("getDpid" + node);
530 long dpid = southbound.getDataPathId(node);
532 LOG.warn("getDpid: dpid not found: {}", node);
537 private Node getNode(NeutronPort port) {
538 LOG.trace("getNode:Port" + port);
539 List<Node> toplogyNodes = southbound.readOvsdbTopologyNodes();
541 for (Node topologyNode : toplogyNodes) {
543 Node node = southbound.getBridgeNode(topologyNode,Constants.INTEGRATION_BRIDGE);
544 List<OvsdbTerminationPointAugmentation> ovsdbPorts = southbound.getTerminationPointsOfBridge(node);
545 for (OvsdbTerminationPointAugmentation ovsdbPort : ovsdbPorts) {
546 String uuid = southbound.getInterfaceExternalIdsValue(ovsdbPort,
547 Constants.EXTERNAL_ID_INTERFACE_ID);
548 if (null != uuid && uuid.equals(port.getID())) {
552 } catch (Exception e) {
553 LOG.error("Exception during handlingNeutron network delete", e);
556 LOG.info("no node found for port:" + port);
560 private OvsdbTerminationPointAugmentation getInterface(Node node, NeutronPort port) {
561 LOG.trace("getInterface:Node:" + node + " Port:" + port);
563 List<OvsdbTerminationPointAugmentation> ovsdbPorts = southbound.getTerminationPointsOfBridge(node);
564 for (OvsdbTerminationPointAugmentation ovsdbPort : ovsdbPorts) {
565 String uuid = southbound.getInterfaceExternalIdsValue(ovsdbPort,
566 Constants.EXTERNAL_ID_INTERFACE_ID);
567 if (null != uuid && uuid.equals(port.getID())) {
571 } catch (Exception e) {
572 LOG.error("Exception during handlingNeutron network delete", e);
574 LOG.info("no interface found for node: " + node + " port:" + port);
579 public void setDependencies(ServiceReference serviceReference) {
581 (NeutronL3Adapter) ServiceHelper.getGlobalInstance(NeutronL3Adapter.class, this);
583 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
584 neutronNetworkCache =
585 (INeutronNetworkCRUD) ServiceHelper.getGlobalInstance(INeutronNetworkCRUD.class, this);
586 configurationService =
587 (ConfigurationService) ServiceHelper.getGlobalInstance(ConfigurationService.class, this);
591 public void setDependencies(Object impl) {
592 if (impl instanceof INeutronPortCRUD) {
593 neutronPortCache = (INeutronPortCRUD)impl;
594 } else if (impl instanceof INeutronSubnetCRUD) {
595 neutronSubnetCache = (INeutronSubnetCRUD) impl;
596 } else if (impl instanceof IngressAclProvider) {
597 ingressAclProvider = (IngressAclProvider) impl;
598 } else if (impl instanceof EgressAclProvider) {
599 egressAclProvider = (EgressAclProvider) impl;
604 public boolean isConntrackEnabled() {
605 return isConntrackEnabled;