blob: 4172292f337da2a2c64eb82a066a79acff41799b [file] [log] [blame]
Charles Chan445659f2019-01-02 13:46:16 -08001/*
2 * Copyright 2019-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.segmentrouting.xconnect.api;
17
18import java.util.Objects;
19
20import static com.google.common.base.Preconditions.checkArgument;
21
22/**
23 * Represents a cross connect endpoint specified by load balancer.
24 */
25public final class XconnectLoadBalancerEndpoint extends XconnectEndpoint {
26 private final int key;
27
28 private XconnectLoadBalancerEndpoint(int key) {
29 this.key = key;
30 }
31
32 /**
33 * Returns load balancer key.
34 *
35 * @return load balancer key.
36 */
37 public int key() {
38 return key;
39 }
40
41 /**
42 * Returns an instance of XconnectLoadBalancerEndpoint with given load balancer key.
43 *
44 * @param key load balancer key
45 * @return an instance of XconnectLoadBalancerEndpoint
46 */
47 public static XconnectLoadBalancerEndpoint of(int key) {
48 return new XconnectLoadBalancerEndpoint(key);
49 }
50
51 /**
52 * Gets XconnectLoadBalancerEndpoint from string.
53 *
54 * @param s string
55 * @return XconnectLoadBalancerEndpoint
56 */
57 public static XconnectLoadBalancerEndpoint fromString(String s) {
58 checkArgument(s.matches(LOAD_BALANCER_PATTERN), "String {} does not match {} format", s, LOAD_BALANCER_PATTERN);
59 return new XconnectLoadBalancerEndpoint(Integer.valueOf(s.replaceFirst(LB_KEYWORD, "")));
60 }
61
62 @Override
63 public Type type() {
64 return Type.LOAD_BALANCER;
65 }
66
67 @Override
68 public int hashCode() {
69 return Objects.hash(key);
70 }
71
72 @Override
73 public boolean equals(Object obj) {
74 if (this == obj) {
75 return true;
76 }
77 if (obj instanceof XconnectLoadBalancerEndpoint) {
78 final XconnectLoadBalancerEndpoint other = (XconnectLoadBalancerEndpoint) obj;
79 return Objects.equals(this.key, other.key);
80 }
81 return false;
82 }
83
84 @Override
85 public String toString() {
86 return LB_KEYWORD + String.valueOf(key);
87 }
88}