Qrouter not assigned to BD fix
[groupbasedpolicy.git] / neutron-mapper / src / main / java / org / opendaylight / groupbasedpolicy / neutron / mapper / util / PortUtils.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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.groupbasedpolicy.neutron.mapper.util;
10
11 import java.util.Collections;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Set;
15
16 import javax.annotation.Nullable;
17
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.binding.rev150712.PortBindingExtension;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIps;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
23
24 import com.google.common.base.Optional;
25
26 public class PortUtils {
27
28     public static final String DEVICE_OWNER_DHCP = "network:dhcp";
29     public static final String DEVICE_OWNER_ROUTER_IFACE = "network:router_interface";
30     public static final String DEVICE_OWNER_ROUTER_GATEWAY = "network:router_gateway";
31     public static final String DEVICE_OWNER_FLOATING_IP = "network:floatingip";
32     public static final String DEVICE_VIF_TYPE = "vhostuser";
33
34     public static Optional<Port> findPort(Uuid uuid, @Nullable Ports ports) {
35         if (ports == null || ports.getPort() == null) {
36             return Optional.absent();
37         }
38         for (Port port : ports.getPort()) {
39             if (port.getUuid().equals(uuid)) {
40                 return Optional.of(port);
41             }
42         }
43         return Optional.absent();
44     }
45
46     public static Set<Port> findRouterInterfacePorts(@Nullable Ports ports) {
47         if (ports == null || ports.getPort() == null) {
48             return Collections.emptySet();
49         }
50         Set<Port> routerInterfacePorts = new HashSet<>();
51         for (Port port : ports.getPort()) {
52             if (isRouterInterfacePort(port)) {
53                 routerInterfacePorts.add(port);
54             }
55         }
56         return routerInterfacePorts;
57     }
58
59     public static Set<Port> findPortsBySubnet(Uuid subnetUuid, @Nullable Ports ports) {
60         if (ports == null || ports.getPort() == null) {
61             return Collections.emptySet();
62         }
63         Set<Port> portsWithSubnet = new HashSet<>();
64         for (Port port : ports.getPort()) {
65             List<FixedIps> fixedIps = port.getFixedIps();
66             if (fixedIps != null && !fixedIps.isEmpty()) {
67                 for (FixedIps ipWithSubnet : fixedIps) {
68                     if (ipWithSubnet.getSubnetId().equals(subnetUuid)) {
69                         portsWithSubnet.add(port);
70                     }
71                 }
72             }
73         }
74         return portsWithSubnet;
75     }
76
77     public static Optional<FixedIps> resolveFirstFixedIps(Port port) {
78         List<FixedIps> fixedIps = port.getFixedIps();
79         if (fixedIps != null && !fixedIps.isEmpty()) {
80             return Optional.of(fixedIps.get(0));
81         }
82         return Optional.absent();
83     }
84
85     public static boolean isNormalPort(Port port) {
86         if (isDhcpPort(port) || isRouterInterfacePort(port) || isRouterGatewayPort(port) || isFloatingIpPort(port)) {
87             return false;
88         }
89         return true;
90     }
91
92     public static boolean isDhcpPort(Port port) {
93         return DEVICE_OWNER_DHCP.equals(port.getDeviceOwner());
94     }
95
96     public static boolean isQrouterPort(Port port) {
97         return DEVICE_OWNER_ROUTER_IFACE.equals(port.getDeviceOwner())
98             && port.getAugmentation(PortBindingExtension.class) != null
99             && DEVICE_VIF_TYPE.equals(port.getAugmentation(PortBindingExtension.class).getVifType());
100     }
101
102     public static boolean isRouterInterfacePort(Port port) {
103         return DEVICE_OWNER_ROUTER_IFACE.equals(port.getDeviceOwner());
104     }
105
106     public static boolean isRouterGatewayPort(Port port) {
107         return DEVICE_OWNER_ROUTER_GATEWAY.equals(port.getDeviceOwner());
108     }
109
110     public static boolean isFloatingIpPort(Port port) {
111         return DEVICE_OWNER_FLOATING_IP.equals(port.getDeviceOwner());
112     }
113 }