blob: 50d155e092da74e702eab4dfff09f049a41ce995 [file] [log] [blame]
Jian Li077b07e2020-09-01 16:55:25 +09001/*
2 * Copyright 2020-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.k8snode.api;
17
18import com.google.common.base.MoreObjects;
19import com.google.common.base.Strings;
20import org.onosproject.net.DeviceId;
21
22import java.util.Objects;
23
24import static org.onosproject.k8snode.api.Constants.GENEVE_TUNNEL;
25import static org.onosproject.k8snode.api.Constants.GRE_TUNNEL;
26import static org.onosproject.k8snode.api.Constants.TUNNEL_BRIDGE;
27import static org.onosproject.k8snode.api.Constants.VXLAN_TUNNEL;
28
29/**
30 * K8s tunnel bridge.
31 */
32public class K8sTunnelBridge {
33
34 private static final String OF_PREFIX = "of:";
35
36 private final int tunnelId;
37
38 /**
39 * Default constructor.
40 *
41 * @param tunnelId tunnel identifier
42 */
43 public K8sTunnelBridge(int tunnelId) {
44 this.tunnelId = tunnelId;
45 }
46
47 /**
48 * Returns device identifier.
49 *
50 * @return device identifier
51 */
52 public DeviceId deviceId() {
53 return DeviceId.deviceId(dpid());
54 }
55
56 /**
57 * Returns tunnel ID.
58 *
59 * @return tunnel ID
60 */
61 public int tunnelId() {
62 return tunnelId;
63 }
64
65 /**
66 * Return the datapath identifier.
67 *
68 * @return datapath identifier
69 */
70 public String dpid() {
71 return genDpidFromName(name());
72 }
73
74 /**
75 * Returns bridge name.
76 *
77 * @return bridge name
78 */
79 public String name() {
80 return TUNNEL_BRIDGE + "-" + tunnelId;
81 }
82
83 /**
84 * Returns GRE port name.
85 *
86 * @return GRE port name
87 */
88 public String grePortName() {
89 return GRE_TUNNEL + "-" + tunnelId;
90 }
91
92 /**
93 * Returns VXLAN port name.
94 *
95 * @return VXLAN port name
96 */
97 public String vxlanPortName() {
98 return VXLAN_TUNNEL + "-" + tunnelId;
99 }
100
101 /**
102 * Returns GENEVE port name.
103 *
104 * @return GENEVE port name
105 */
106 public String genevePortName() {
107 return GENEVE_TUNNEL + "-" + tunnelId;
108 }
109
110 @Override
111 public boolean equals(Object o) {
112 if (this == o) {
113 return true;
114 }
115 if (o == null || getClass() != o.getClass()) {
116 return false;
117 }
118 K8sTunnelBridge that = (K8sTunnelBridge) o;
119 return tunnelId == that.tunnelId;
120 }
121
122 @Override
123 public int hashCode() {
124 return Objects.hash(tunnelId);
125 }
126
127 @Override
128 public String toString() {
129 return MoreObjects.toStringHelper(this)
130 .add("tunnelId", tunnelId)
131 .toString();
132 }
133
134 private String genDpidFromName(String name) {
135 if (name != null) {
136 String hexString = Integer.toHexString(name.hashCode());
137 return OF_PREFIX + Strings.padStart(hexString, 16, '0');
138 }
139
140 return null;
141 }
142}