blob: 510c9b76a54a9c30e199ceada87bc0113f16dcc9 [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 */
Jian Li019ce6a2020-09-09 10:23:21 +090032public class K8sTunnelBridge implements K8sBridge {
Jian Li077b07e2020-09-01 16:55:25 +090033
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
Jian Li019ce6a2020-09-09 10:23:21 +090047 @Override
Jian Li077b07e2020-09-01 16:55:25 +090048 public DeviceId deviceId() {
49 return DeviceId.deviceId(dpid());
50 }
51
52 /**
53 * Returns tunnel ID.
54 *
55 * @return tunnel ID
56 */
57 public int tunnelId() {
58 return tunnelId;
59 }
60
Jian Li019ce6a2020-09-09 10:23:21 +090061 @Override
Jian Li077b07e2020-09-01 16:55:25 +090062 public String dpid() {
63 return genDpidFromName(name());
64 }
65
Jian Li019ce6a2020-09-09 10:23:21 +090066 @Override
Jian Li077b07e2020-09-01 16:55:25 +090067 public String name() {
68 return TUNNEL_BRIDGE + "-" + tunnelId;
69 }
70
71 /**
72 * Returns GRE port name.
73 *
74 * @return GRE port name
75 */
76 public String grePortName() {
77 return GRE_TUNNEL + "-" + tunnelId;
78 }
79
80 /**
81 * Returns VXLAN port name.
82 *
83 * @return VXLAN port name
84 */
85 public String vxlanPortName() {
86 return VXLAN_TUNNEL + "-" + tunnelId;
87 }
88
89 /**
90 * Returns GENEVE port name.
91 *
92 * @return GENEVE port name
93 */
94 public String genevePortName() {
95 return GENEVE_TUNNEL + "-" + tunnelId;
96 }
97
98 @Override
99 public boolean equals(Object o) {
100 if (this == o) {
101 return true;
102 }
103 if (o == null || getClass() != o.getClass()) {
104 return false;
105 }
106 K8sTunnelBridge that = (K8sTunnelBridge) o;
107 return tunnelId == that.tunnelId;
108 }
109
110 @Override
111 public int hashCode() {
112 return Objects.hash(tunnelId);
113 }
114
115 @Override
116 public String toString() {
117 return MoreObjects.toStringHelper(this)
118 .add("tunnelId", tunnelId)
119 .toString();
120 }
121
122 private String genDpidFromName(String name) {
123 if (name != null) {
124 String hexString = Integer.toHexString(name.hashCode());
125 return OF_PREFIX + Strings.padStart(hexString, 16, '0');
126 }
127
128 return null;
129 }
130}