2 * Copyright (c) 2016 Cisco Systems, 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.groupbasedpolicy.neutron.mapper.util;
11 import java.util.Collections;
12 import java.util.HashSet;
13 import java.util.List;
16 import javax.annotation.Nullable;
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;
23 import com.google.common.base.Optional;
25 public class PortUtils {
27 private static final String DEVICE_OWNER_DHCP = "network:dhcp";
28 private static final String DEVICE_OWNER_ROUTER_IFACE = "network:router_interface";
29 private static final String DEVICE_OWNER_ROUTER_GATEWAY = "network:router_gateway";
30 private static final String DEVICE_OWNER_FLOATING_IP = "network:floatingip";
32 public static Optional<Port> findPort(Uuid uuid, @Nullable Ports ports) {
33 if (ports == null || ports.getPort() == null) {
34 return Optional.absent();
36 for (Port port : ports.getPort()) {
37 if (port.getUuid().equals(uuid)) {
38 return Optional.of(port);
41 return Optional.absent();
44 public static Set<Port> findRouterInterfacePorts(@Nullable Ports ports) {
45 if (ports == null || ports.getPort() == null) {
46 return Collections.emptySet();
48 Set<Port> routerInterfacePorts = new HashSet<>();
49 for (Port port : ports.getPort()) {
50 if (isRouterInterfacePort(port)) {
51 routerInterfacePorts.add(port);
54 return routerInterfacePorts;
57 public static Set<Port> findPortsBySubnet(Uuid subnetUuid, @Nullable Ports ports) {
58 if (ports == null || ports.getPort() == null) {
59 return Collections.emptySet();
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);
72 return portsWithSubnet;
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));
80 return Optional.absent();
83 public static boolean isNormalPort(Port port) {
84 if (isDhcpPort(port) || isRouterInterfacePort(port) || isRouterGatewayPort(port) || isFloatingIpPort(port)) {
90 public static boolean isDhcpPort(Port port) {
91 return DEVICE_OWNER_DHCP.equals(port.getDeviceOwner());
94 public static boolean isRouterInterfacePort(Port port) {
95 return DEVICE_OWNER_ROUTER_IFACE.equals(port.getDeviceOwner());
98 public static boolean isRouterGatewayPort(Port port) {
99 return DEVICE_OWNER_ROUTER_GATEWAY.equals(port.getDeviceOwner());
102 public static boolean isFloatingIpPort(Port port) {
103 return DEVICE_OWNER_FLOATING_IP.equals(port.getDeviceOwner());