Unit tests for neutron mapper (parent commit)
[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.ports.rev150712.port.attributes.FixedIps;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
22
23 import com.google.common.base.Optional;
24
25 public class PortUtils {
26
27     public static final String DEVICE_OWNER_DHCP = "network:dhcp";
28     public static final String DEVICE_OWNER_ROUTER_IFACE = "network:router_interface";
29     public static final String DEVICE_OWNER_ROUTER_GATEWAY = "network:router_gateway";
30     public static final String DEVICE_OWNER_FLOATING_IP = "network:floatingip";
31
32     public static Optional<Port> findPort(Uuid uuid, @Nullable Ports ports) {
33         if (ports == null || ports.getPort() == null) {
34             return Optional.absent();
35         }
36         for (Port port : ports.getPort()) {
37             if (port.getUuid().equals(uuid)) {
38                 return Optional.of(port);
39             }
40         }
41         return Optional.absent();
42     }
43
44     public static Set<Port> findRouterInterfacePorts(@Nullable Ports ports) {
45         if (ports == null || ports.getPort() == null) {
46             return Collections.emptySet();
47         }
48         Set<Port> routerInterfacePorts = new HashSet<>();
49         for (Port port : ports.getPort()) {
50             if (isRouterInterfacePort(port)) {
51                 routerInterfacePorts.add(port);
52             }
53         }
54         return routerInterfacePorts;
55     }
56
57     public static Set<Port> findPortsBySubnet(Uuid subnetUuid, @Nullable Ports ports) {
58         if (ports == null || ports.getPort() == null) {
59             return Collections.emptySet();
60         }
61         Set<Port> portsWithSubnet = new HashSet<>();
62         for (Port port : ports.getPort()) {
63             List<FixedIps> fixedIps = port.getFixedIps();
64             if (fixedIps != null && !fixedIps.isEmpty()) {
65                 for (FixedIps ipWithSubnet : fixedIps) {
66                     if (ipWithSubnet.getSubnetId().equals(subnetUuid)) {
67                         portsWithSubnet.add(port);
68                     }
69                 }
70             }
71         }
72         return portsWithSubnet;
73     }
74
75     public static Optional<FixedIps> resolveFirstFixedIps(Port port) {
76         List<FixedIps> fixedIps = port.getFixedIps();
77         if (fixedIps != null && !fixedIps.isEmpty()) {
78             return Optional.of(fixedIps.get(0));
79         }
80         return Optional.absent();
81     }
82
83     public static boolean isNormalPort(Port port) {
84         if (isDhcpPort(port) || isRouterInterfacePort(port) || isRouterGatewayPort(port) || isFloatingIpPort(port)) {
85             return false;
86         }
87         return true;
88     }
89
90     public static boolean isDhcpPort(Port port) {
91         return DEVICE_OWNER_DHCP.equals(port.getDeviceOwner());
92     }
93
94     public static boolean isRouterInterfacePort(Port port) {
95         return DEVICE_OWNER_ROUTER_IFACE.equals(port.getDeviceOwner());
96     }
97
98     public static boolean isRouterGatewayPort(Port port) {
99         return DEVICE_OWNER_ROUTER_GATEWAY.equals(port.getDeviceOwner());
100     }
101
102     public static boolean isFloatingIpPort(Port port) {
103         return DEVICE_OWNER_FLOATING_IP.equals(port.getDeviceOwner());
104     }
105 }