411dc550d85eb83ddffa25776dcba9e25a51ac4c
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / impl / SecurityServicesImpl.java
1 /*
2  * Copyright (C) 2014 Red Hat, Inc.
3  *
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
7  */
8
9 package org.opendaylight.ovsdb.openstack.netvirt.impl;
10
11 import org.opendaylight.neutron.spi.INeutronPortCRUD;
12 import org.opendaylight.neutron.spi.NeutronPort;
13 import org.opendaylight.neutron.spi.NeutronSecurityGroup;
14 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
15 import org.opendaylight.ovsdb.openstack.netvirt.api.SecurityServicesManager;
16
17 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import java.util.List;
22 import java.util.Map;
23
24 public class SecurityServicesImpl implements SecurityServicesManager {
25
26     static final Logger logger = LoggerFactory.getLogger(TenantNetworkManagerImpl.class);
27     private volatile INeutronPortCRUD neutronPortService;
28
29     public SecurityServicesImpl() {
30     }
31
32     /**
33      * Is security group ready.
34      *
35      * @param intf the intf
36      * @return the boolean
37      */
38     public boolean isPortSecurityReady(Interface intf) {
39         logger.trace("getTenantNetworkForInterface for {}", intf);
40         if (intf == null) return false;
41         Map<String, String> externalIds = intf.getExternalIdsColumn().getData();
42         logger.trace("externalIds {}", externalIds);
43         if (externalIds == null) return false;
44         String neutronPortId = externalIds.get(Constants.EXTERNAL_ID_INTERFACE_ID);
45         if (neutronPortId == null) return false;
46         NeutronPort neutronPort = neutronPortService.getPort(neutronPortId);
47         String deviceOwner = neutronPort.getDeviceOwner();
48         if (!deviceOwner.contains("compute")) {
49             logger.debug("Port {} is not a compute host, it is a: {}", neutronPortId, deviceOwner);
50         }
51         logger.debug("isPortSecurityReady() is a {} ", deviceOwner);
52         List<NeutronSecurityGroup> securityGroups = neutronPort.getSecurityGroups();
53         if (securityGroups.isEmpty()) {
54             logger.debug("Check for device: {} does not contain a Security Group for port: {}", deviceOwner,
55                     neutronPortId);
56             return false;
57         }
58         try {
59             String vmPort = externalIds.get("attached-mac");
60         } catch(Exception e) {
61             logger.debug("Error VMID did *NOT* work");
62         }
63         logger.debug("Security Group Check {} DOES contain a Neutron Security Group", neutronPortId);
64         return true;
65     }
66
67     /**
68      * Gets security group in port.
69      *
70      * @param intf the intf
71      * @return the security group in port
72      */
73     public NeutronSecurityGroup getSecurityGroupInPort(Interface intf) {
74         logger.trace("getTenantNetworkForInterface for {}", intf);
75         if (intf == null) return null;
76         Map<String, String> externalIds = intf.getExternalIdsColumn().getData();
77         logger.trace("externalIds {}", externalIds);
78         if (externalIds == null) return null;
79         String neutronPortId = externalIds.get(Constants.EXTERNAL_ID_INTERFACE_ID);
80         if (neutronPortId == null) return null;
81         NeutronPort neutronPort = neutronPortService.getPort(neutronPortId);
82         List<NeutronSecurityGroup> neutronSecurityGroups = neutronPort.getSecurityGroups();
83         NeutronSecurityGroup neutronSecurityGroup = (NeutronSecurityGroup) neutronSecurityGroups.toArray()[0];
84         return neutronSecurityGroup;
85     }
86 }