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