blob: d88e132f20963e19ad24e934649d16d69174093d [file] [log] [blame]
Jian Li26ef1302018-07-04 14:37:06 +09001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
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.openstackvtap.util;
17
18import org.onlab.packet.IPv4;
Jimo Jung14e87bf2018-09-03 16:28:13 +090019import org.onlab.packet.IpAddress;
20import org.onlab.packet.IpPrefix;
21import org.onosproject.net.Host;
22import org.onosproject.net.behaviour.TunnelDescription;
Jian Li26ef1302018-07-04 14:37:06 +090023import org.onosproject.net.group.DefaultGroupKey;
24import org.onosproject.net.group.GroupKey;
25import org.onosproject.openstackvtap.api.OpenstackVtap;
Jimo Jung14e87bf2018-09-03 16:28:13 +090026import org.onosproject.openstackvtap.api.OpenstackVtapCriterion;
27import org.onosproject.openstackvtap.api.OpenstackVtapNetwork;
28import org.slf4j.Logger;
29
30import java.io.ByteArrayOutputStream;
Jimo Jung14e87bf2018-09-03 16:28:13 +090031import java.nio.charset.StandardCharsets;
32
33import static org.onosproject.openstacknetworking.api.Constants.ANNOTATION_NETWORK_ID;
34import static org.onosproject.openstacknetworking.api.Constants.ANNOTATION_PORT_ID;
Jian Li26ef1302018-07-04 14:37:06 +090035
36/**
Jimo Jung14e87bf2018-09-03 16:28:13 +090037 * An utilities that used in openstack vtap app.
Jian Li26ef1302018-07-04 14:37:06 +090038 */
39public final class OpenstackVtapUtil {
40
Jimo Jung14e87bf2018-09-03 16:28:13 +090041 private static final String VTAP_TUNNEL_GRE = "vtap_gre";
42 private static final String VTAP_TUNNEL_VXLAN = "vtap_vxlan";
Jian Li26ef1302018-07-04 14:37:06 +090043 private static final String VTAP_GROUP_KEY = "VTAP_GROUP_KEY";
44
45 /**
46 * Prevents object instantiation from external.
47 */
48 private OpenstackVtapUtil() {
49 }
50
51 /**
Jian Li26ef1302018-07-04 14:37:06 +090052 * Obtains IP protocol type from the given string.
53 *
Jimo Jung14e87bf2018-09-03 16:28:13 +090054 * @param str protocol string
55 * @return IP protocol number
Jian Li26ef1302018-07-04 14:37:06 +090056 */
57 public static byte getProtocolTypeFromString(String str) {
58 switch (str) {
59 case "tcp":
60 return IPv4.PROTOCOL_TCP;
61 case "udp":
62 return IPv4.PROTOCOL_UDP;
63 case "icmp":
64 return IPv4.PROTOCOL_ICMP;
Jimo Jung14e87bf2018-09-03 16:28:13 +090065 case "any":
Jian Li26ef1302018-07-04 14:37:06 +090066 return 0;
Jimo Jung14e87bf2018-09-03 16:28:13 +090067 default:
68 throw new IllegalArgumentException("Invalid vtap protocol string");
Jian Li26ef1302018-07-04 14:37:06 +090069 }
70 }
71
Jimo Jung14e87bf2018-09-03 16:28:13 +090072 /**
73 * Obtains openstack vtap type from the given string.
74 *
75 * @param str vtap type string
76 * @return vtap type
77 */
78 public static OpenstackVtap.Type getVtapTypeFromString(String str) {
79 switch (str) {
80 case "all":
81 return OpenstackVtap.Type.VTAP_ALL;
82 case "rx":
83 return OpenstackVtap.Type.VTAP_RX;
84 case "tx":
85 return OpenstackVtap.Type.VTAP_TX;
86 case "any":
87 return OpenstackVtap.Type.VTAP_ANY;
88 default:
89 throw new IllegalArgumentException("Invalid vtap type string");
90 }
91 }
92
93 /**
94 * Checks whether the given IP address is included in vtap criterion with
95 * TX and RX directions by given vtap type.
96 *
97 * @param type vtap type
98 * @param criterion vtap criterion
99 * @param ip IP address to check
100 * @return true on match address, false otherwise
101 */
102 public static boolean containsIp(OpenstackVtap.Type type, OpenstackVtapCriterion criterion, IpAddress ip) {
103 boolean isTxEdge = type.isValid(OpenstackVtap.Type.VTAP_TX) &&
104 criterion.srcIpPrefix().contains(ip);
105 boolean isRxEdge = type.isValid(OpenstackVtap.Type.VTAP_RX) &&
106 criterion.dstIpPrefix().contains(ip);
107 return isTxEdge || isRxEdge;
108 }
109
110 /**
111 * Checks the host validation from annotation information.
112 *
113 * @param host host to check
114 * @return true on validate, false otherwise
115 */
116 public static boolean isValidHost(Host host) {
117 return !host.ipAddresses().isEmpty() &&
118 host.annotations().value(ANNOTATION_NETWORK_ID) != null &&
119 host.annotations().value(ANNOTATION_PORT_ID) != null;
120 }
121
122 /**
123 * Checks whether the given IP prefix is contained in the first host rather
124 * than in the second host.
125 *
126 * @param host1 first host instance
127 * @param host2 second host instance
128 * @param ipPrefix IP prefix to be looked up
129 * @return a negative integer, zero, or a positive integer as the
130 * first argument is less than, equal to, or greater than the
131 * second.
132 */
133 public static int hostCompareIp(Host host1, Host host2, IpPrefix ipPrefix) {
134 if ((host1 == null || host1.ipAddresses().stream().noneMatch(ipPrefix::contains)) &&
135 (host2 != null || host2.ipAddresses().stream().anyMatch(ipPrefix::contains))) {
136 return -1;
137 } else if ((host1 != null && host1.ipAddresses().stream().anyMatch(ipPrefix::contains)) &&
138 (host2 == null || host2.ipAddresses().stream().noneMatch(ipPrefix::contains))) {
139 return 1;
140 }
141 return 0;
142 }
143
144 /**
145 * Obtains flow group key from the given id.
146 *
147 * @param groupId flow group identifier
148 * @return flow group key
149 */
Jian Li26ef1302018-07-04 14:37:06 +0900150 public static GroupKey getGroupKey(int groupId) {
151 return new DefaultGroupKey((VTAP_GROUP_KEY + Integer.toString(groupId)).getBytes());
152 }
Jimo Jung14e87bf2018-09-03 16:28:13 +0900153
154 /**
155 * Obtains tunnel interface name from the given openstack vtap network mode.
156 *
157 * @param mode vtap network mode
158 * @return tunnel interface name
159 */
160 public static String getTunnelName(OpenstackVtapNetwork.Mode mode) {
161 switch (mode) {
162 case GRE:
163 return VTAP_TUNNEL_GRE;
164 case VXLAN:
165 return VTAP_TUNNEL_VXLAN;
166 default:
167 return null;
168 }
169 }
170
171 /**
172 * Obtains tunnel description type from the given openstack vtap network mode.
173 *
174 * @param mode vtap network mode
175 * @return tunnel description type
176 */
177 public static TunnelDescription.Type getTunnelType(OpenstackVtapNetwork.Mode mode) {
178 return TunnelDescription.Type.valueOf(mode.toString());
179 }
180
181 /**
182 * Print stack trace of given exception.
183 *
184 * @param log logger for using print
185 * @param e exception to print
186 */
187 public static void dumpStackTrace(Logger log, Exception e) {
188 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Jimo Jung14e87bf2018-09-03 16:28:13 +0900189 log.error("\n{}", new String(outputStream.toByteArray(), StandardCharsets.UTF_8));
190 }
191
Jian Li26ef1302018-07-04 14:37:06 +0900192}