Use neutron from new repo
[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.controller.sal.utils.ServiceHelper;
15 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
16 import org.opendaylight.ovsdb.openstack.netvirt.api.SecurityServicesManager;
17
18 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import java.util.List;
23 import java.util.Map;
24
25 public class SecurityServicesImpl implements SecurityServicesManager {
26
27     static final Logger logger = LoggerFactory.getLogger(TenantNetworkManagerImpl.class);
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         INeutronPortCRUD neutronPortService = (INeutronPortCRUD) ServiceHelper.getGlobalInstance(INeutronPortCRUD.class,
47                 this);
48         NeutronPort neutronPort = neutronPortService.getPort(neutronPortId);
49         String deviceOwner = neutronPort.getDeviceOwner();
50         if (!deviceOwner.contains("compute")) {
51             logger.debug("Port {} is not a compute host, it is a: {}", neutronPortId, deviceOwner);
52         }
53         logger.debug("isPortSecurityReady() is a {} ", deviceOwner);
54         List<NeutronSecurityGroup> securityGroups = neutronPort.getSecurityGroups();
55         if (securityGroups.isEmpty()) {
56             logger.debug("Check for device: {} does not contain a Security Group for port: {}", deviceOwner,
57                     neutronPortId);
58             return false;
59         }
60         try {
61             String vmPort = externalIds.get("attached-mac");
62         } catch(Exception e) {
63             logger.debug("Error VMID did *NOT* work");
64         }
65         logger.debug("Security Group Check {} DOES contain a Neutron Security Group", neutronPortId);
66         return true;
67     }
68
69     /**
70      * Gets security group in port.
71      *
72      * @param intf the intf
73      * @return the security group in port
74      */
75     public NeutronSecurityGroup getSecurityGroupInPort(Interface intf) {
76         logger.trace("getTenantNetworkForInterface for {}", intf);
77         if (intf == null) return null;
78         Map<String, String> externalIds = intf.getExternalIdsColumn().getData();
79         logger.trace("externalIds {}", externalIds);
80         if (externalIds == null) return null;
81         String neutronPortId = externalIds.get(Constants.EXTERNAL_ID_INTERFACE_ID);
82         if (neutronPortId == null) return null;
83         INeutronPortCRUD neutronPortService = (INeutronPortCRUD)
84                 ServiceHelper.getGlobalInstance(INeutronPortCRUD.class, this);
85         NeutronPort neutronPort = neutronPortService.getPort(neutronPortId);
86         List<NeutronSecurityGroup> neutronSecurityGroups = neutronPort.getSecurityGroups();
87         NeutronSecurityGroup neutronSecurityGroup = (NeutronSecurityGroup) neutronSecurityGroups.toArray()[0];
88         return neutronSecurityGroup;
89     }
90 }