blob: 7e5b472dc451c02ae0ef10ff1bcd019389958582 [file] [log] [blame]
Jian Li019ce6a2020-09-09 10:23:21 +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.ROUTER_BRIDGE;
25
26/**
27 * K8s router bridge.
28 */
29public class K8sRouterBridge implements K8sBridge {
30
31 private static final String OF_PREFIX = "of:";
32
33 private final int segmentId;
34
35 /**
36 * Default constructor.
37 *
38 * @param segmentId segment identifier
39 */
40 public K8sRouterBridge(int segmentId) {
41 this.segmentId = segmentId;
42 }
43
44 /**
45 * Returns segment ID.
46 *
47 * @return segment ID
48 */
49 public int segmentId() {
50 return segmentId;
51 }
52
53 @Override
54 public DeviceId deviceId() {
55 return DeviceId.deviceId(dpid());
56 }
57
58 @Override
59 public String dpid() {
60 return genDpidFromName(name());
61 }
62
63 @Override
64 public String name() {
65 return ROUTER_BRIDGE + "-" + segmentId;
66 }
67
68 @Override
69 public boolean equals(Object o) {
70 if (this == o) {
71 return true;
72 }
73 if (o == null || getClass() != o.getClass()) {
74 return false;
75 }
76 K8sRouterBridge that = (K8sRouterBridge) o;
77 return segmentId == that.segmentId;
78 }
79
80 @Override
81 public int hashCode() {
82 return Objects.hash(segmentId);
83 }
84
85 @Override
86 public String toString() {
87 return MoreObjects.toStringHelper(this)
88 .add("segmentId", segmentId)
89 .toString();
90 }
91
92 private String genDpidFromName(String name) {
93 if (name != null) {
94 String hexString = Integer.toHexString(name.hashCode());
95 return OF_PREFIX + Strings.padStart(hexString, 16, '0');
96 }
97
98 return null;
99 }
100}