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.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;
24 import com.google.common.base.Optional;
26 public class PortUtils {
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";
34 public static Optional<Port> findPort(Uuid uuid, @Nullable Ports ports) {
35 if (ports == null || ports.getPort() == null) {
36 return Optional.absent();
38 for (Port port : ports.getPort()) {
39 if (port.getUuid().equals(uuid)) {
40 return Optional.of(port);
43 return Optional.absent();
46 public static Set<Port> findRouterInterfacePorts(@Nullable Ports ports) {
47 if (ports == null || ports.getPort() == null) {
48 return Collections.emptySet();
50 Set<Port> routerInterfacePorts = new HashSet<>();
51 for (Port port : ports.getPort()) {
52 if (isRouterInterfacePort(port)) {
53 routerInterfacePorts.add(port);
56 return routerInterfacePorts;
59 public static Set<Port> findPortsBySubnet(Uuid subnetUuid, @Nullable Ports ports) {
60 if (ports == null || ports.getPort() == null) {
61 return Collections.emptySet();
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);
74 return portsWithSubnet;
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));
82 return Optional.absent();
85 public static boolean isNormalPort(Port port) {
86 if (isDhcpPort(port) || isRouterInterfacePort(port) || isRouterGatewayPort(port) || isFloatingIpPort(port)) {
92 public static boolean isDhcpPort(Port port) {
93 return DEVICE_OWNER_DHCP.equals(port.getDeviceOwner());
96 public static boolean isQrouterOrVppRouterPort(Port port) {
97 return DEVICE_OWNER_ROUTER_IFACE.equals(port.getDeviceOwner())
98 && port.getAugmentation(PortBindingExtension.class) != null;
101 public static boolean isRouterInterfacePort(Port port) {
102 return DEVICE_OWNER_ROUTER_IFACE.equals(port.getDeviceOwner());
105 public static boolean isRouterGatewayPort(Port port) {
106 return DEVICE_OWNER_ROUTER_GATEWAY.equals(port.getDeviceOwner());
109 public static boolean isFloatingIpPort(Port port) {
110 return DEVICE_OWNER_FLOATING_IP.equals(port.getDeviceOwner());