blob: 1e676930e95f9330930876c7109d23b916b3d722 [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;
19import org.onosproject.net.group.DefaultGroupKey;
20import org.onosproject.net.group.GroupKey;
21import org.onosproject.openstackvtap.api.OpenstackVtap;
22
23/**
24 * An utility that used in openstack vTap app.
25 */
26public final class OpenstackVtapUtil {
27
28 private static final String VTAP_GROUP_KEY = "VTAP_GROUP_KEY";
29
30 /**
31 * Prevents object instantiation from external.
32 */
33 private OpenstackVtapUtil() {
34 }
35
36 /**
37 * Obtains vTap type from the given string.
38 *
39 * @param str string
40 * @return vTap type
41 */
42 public static OpenstackVtap.Type getVtapTypeFromString(String str) {
43 switch (str) {
44 case "all":
45 return OpenstackVtap.Type.VTAP_ALL;
46 case "tx":
47 return OpenstackVtap.Type.VTAP_TX;
48 case "rx":
49 return OpenstackVtap.Type.VTAP_RX;
50 case "none":
51 return OpenstackVtap.Type.VTAP_NONE;
52 default:
53 throw new IllegalArgumentException("Invalid vTap type string");
54 }
55 }
56
57 /**
58 * Obtains IP protocol type from the given string.
59 *
60 * @param str string
61 * @return vTap type
62 */
63 public static byte getProtocolTypeFromString(String str) {
64 switch (str) {
65 case "tcp":
66 return IPv4.PROTOCOL_TCP;
67 case "udp":
68 return IPv4.PROTOCOL_UDP;
69 case "icmp":
70 return IPv4.PROTOCOL_ICMP;
71 default:
72 return 0;
73 }
74 }
75
76 public static GroupKey getGroupKey(int groupId) {
77 return new DefaultGroupKey((VTAP_GROUP_KEY + Integer.toString(groupId)).getBytes());
78 }
79}