blob: f401bde0aa1840c36a054afa90bc6c2f51d38dc8 [file] [log] [blame]
lishuai6c56f5e2015-11-17 16:38:19 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
lishuai6c56f5e2015-11-17 16:38:19 +08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.vtn.util;
17
18import java.util.ArrayList;
19import java.util.Collection;
lishuai858efd32015-12-04 14:30:36 +080020import java.util.Iterator;
21import java.util.List;
lishuai6c56f5e2015-11-17 16:38:19 +080022
lishuai858efd32015-12-04 14:30:36 +080023import org.onlab.packet.IpAddress;
lishuai6c56f5e2015-11-17 16:38:19 +080024import org.onosproject.net.AnnotationKeys;
25import org.onosproject.net.Device;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.Port;
28import org.onosproject.net.PortNumber;
lishuai858efd32015-12-04 14:30:36 +080029import org.onosproject.store.service.EventuallyConsistentMap;
30import org.onosproject.vtnrsc.FixedIp;
31import org.onosproject.vtnrsc.TenantNetworkId;
32import org.onosproject.vtnrsc.VirtualPort;
33import org.onosproject.vtnrsc.VirtualPortId;
lishuai6c56f5e2015-11-17 16:38:19 +080034import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
36
37import com.google.common.collect.Sets;
38
39/**
40 * VtnData utility class.
41 */
42public final class VtnData {
43
44 private static final Logger log = LoggerFactory.getLogger(VtnData.class);
45 private static final String SWITCH_CHANNEL_ID = "channelId";
46 private static final String PORT_HEAD = "vxlan";
47
48 /**
49 * Constructs a VtnData object. Utility classes should not have a public or
50 * default constructor, otherwise IDE will compile unsuccessfully. This
51 * class should not be instantiated.
52 */
53 private VtnData() {
54 }
55
56 /**
57 * Get the ControllerIp from the device .
58 *
59 * @param device Device
60 * @return Controller Ip
61 */
62 public static String getControllerIpOfSwitch(Device device) {
63 String url = device.annotations().value(SWITCH_CHANNEL_ID);
64 return url.substring(0, url.lastIndexOf(":"));
65 }
66
67 /**
68 * Get the ControllerId from the device .
69 *
70 * @param device Device
71 * @param devices Devices
72 * @return Controller Id
73 */
74 public static DeviceId getControllerId(Device device,
75 Iterable<Device> devices) {
76 for (Device d : devices) {
77 if (d.type() == Device.Type.CONTROLLER && d.id().toString()
78 .contains(getControllerIpOfSwitch(device))) {
79 return d.id();
80 }
81 }
82 log.info("Can not find controller for device : {}", device.id());
83 return null;
84 }
85
86 /**
87 * Get local tunnel ports.
88 *
89 * @param ports Iterable of Port
90 * @return Collection of PortNumber
91 */
92 public static Collection<PortNumber> getLocalTunnelPorts(Iterable<Port> ports) {
93 Collection<PortNumber> localTunnelPorts = new ArrayList<>();
94 Sets.newHashSet(ports).stream()
95 .filter(p -> !p.number().equals(PortNumber.LOCAL))
96 .forEach(p -> {
97 if (p.annotations().value(AnnotationKeys.PORT_NAME)
98 .startsWith(PORT_HEAD)) {
99 localTunnelPorts.add(p.number());
100 }
101 });
102 return localTunnelPorts;
103 }
104
lishuai858efd32015-12-04 14:30:36 +0800105 /**
106 * Get VirtualPort.
107 *
108 * @param vPortStore EventuallyConsistentMap of VirtualPort
109 * @param vPortId VirtualPortId of the VirtualPort
110 * @return VirtualPort
111 */
112 public static VirtualPort getPort(EventuallyConsistentMap<VirtualPortId, VirtualPort> vPortStore,
113 VirtualPortId vPortId) {
114 if (vPortStore != null) {
115 return vPortStore.get(vPortId);
116 }
117 return null;
118 }
119
120 /**
121 * Get VirtualPort.
122 *
123 * @param vPortStore EventuallyConsistentMap of VirtualPort
124 * @param fixedIP FixedIp of the VirtualPort
125 * @return VirtualPort
126 */
127 public static VirtualPort getPort(EventuallyConsistentMap<VirtualPortId, VirtualPort> vPortStore,
128 FixedIp fixedIP) {
129 if (vPortStore != null) {
130 List<VirtualPort> vPorts = new ArrayList<>();
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700131 vPortStore.values().forEach(p -> {
lishuai858efd32015-12-04 14:30:36 +0800132 Iterator<FixedIp> fixedIps = p.fixedIps().iterator();
133 while (fixedIps.hasNext()) {
134 if (fixedIps.next().equals(fixedIP)) {
135 vPorts.add(p);
136 break;
137 }
138 }
139 });
Jon Hallcbd1b392017-01-18 20:15:44 -0800140 if (vPorts.isEmpty()) {
lishuai858efd32015-12-04 14:30:36 +0800141 return null;
142 }
143 return vPorts.get(0);
144 }
145 return null;
146 }
147
148 /**
149 * Get VirtualPort.
150 *
151 * @param vPortStore EventuallyConsistentMap of VirtualPort
152 * @param networkId TenantNetworkId of the VirtualPort
153 * @param ip IpAddress of the VirtualPort
154 * @return VirtualPort
155 */
156 public static VirtualPort getPort(EventuallyConsistentMap<VirtualPortId, VirtualPort> vPortStore,
157 TenantNetworkId networkId, IpAddress ip) {
158 if (vPortStore != null) {
159 List<VirtualPort> vPorts = new ArrayList<>();
160 vPortStore.values().stream()
161 .filter(p -> p.networkId().equals(networkId))
162 .forEach(p -> {
163 Iterator<FixedIp> fixedIps = p.fixedIps().iterator();
164 while (fixedIps.hasNext()) {
165 if (fixedIps.next().ip().equals(ip)) {
166 vPorts.add(p);
167 break;
168 }
169 }
170 });
Jon Hallcbd1b392017-01-18 20:15:44 -0800171 if (vPorts.isEmpty()) {
lishuai858efd32015-12-04 14:30:36 +0800172 return null;
173 }
174 return vPorts.get(0);
175 }
176 return null;
177 }
178
lishuai6c56f5e2015-11-17 16:38:19 +0800179}