blob: 99ea23f597262d673356fd9140bc57317380748e [file] [log] [blame]
Yi Tseng2a340f72018-11-02 16:52:47 -07001/*
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 */
16
17package org.onosproject.grpc.api;
18
19import com.google.common.annotations.Beta;
Yi Tseng2a340f72018-11-02 16:52:47 -070020import com.google.common.base.Objects;
21import org.onosproject.net.DeviceId;
22
23import static com.google.common.base.Preconditions.checkArgument;
24import static com.google.common.base.Preconditions.checkNotNull;
Carmelo Casconea71b8492018-12-17 17:47:50 -080025import static java.lang.String.format;
Yi Tseng2a340f72018-11-02 16:52:47 -070026
27/**
28 * Key that uniquely identifies a gRPC client.
29 */
30@Beta
31public class GrpcClientKey {
32 private final String serviceName;
33 private final DeviceId deviceId;
34 private final String serverAddr;
35 private final int serverPort;
36
37 /**
38 * Creates a new client key.
39 *
40 * @param serviceName gRPC service name of the client
41 * @param deviceId ONOS device ID
42 * @param serverAddr gRPC server address
43 * @param serverPort gRPC server port
44 */
45 public GrpcClientKey(String serviceName, DeviceId deviceId, String serverAddr, int serverPort) {
46 checkNotNull(serviceName);
47 checkNotNull(deviceId);
48 checkNotNull(serverAddr);
49 checkArgument(!serviceName.isEmpty(),
50 "Service name can not be null");
51 checkArgument(!serverAddr.isEmpty(),
52 "Server address should not be empty");
53 checkArgument(serverPort > 0 && serverPort <= 65535, "Invalid server port");
54 this.serviceName = serviceName;
55 this.deviceId = deviceId;
56 this.serverAddr = serverAddr;
57 this.serverPort = serverPort;
58 }
59
60 /**
61 * Gets the gRPC service name of the client.
62 *
63 * @return the service name
64 */
65 public String serviceName() {
66 return serviceName;
67 }
68
69 /**
70 * Gets the device ID.
71 *
72 * @return the device ID
73 */
74 public DeviceId deviceId() {
75 return deviceId;
76 }
77
78 /**
79 * Gets the gRPC server address.
80 *
81 * @return the gRPC server address.
82 */
83 public String serverAddr() {
84 return serverAddr;
85 }
86
87 /**
88 * Gets the gRPC server port.
89 *
90 * @return the gRPC server port.
91 */
92 public int serverPort() {
93 return serverPort;
94 }
95
96 @Override
97 public boolean equals(Object o) {
98 if (this == o) {
99 return true;
100 }
101 if (o == null || getClass() != o.getClass()) {
102 return false;
103 }
104 GrpcClientKey that = (GrpcClientKey) o;
105 return serverPort == that.serverPort &&
106 Objects.equal(serviceName, that.serviceName) &&
107 Objects.equal(deviceId, that.deviceId) &&
108 Objects.equal(serverAddr, that.serverAddr);
109 }
110
111 @Override
112 public int hashCode() {
113 return Objects.hashCode(serviceName, deviceId, serverAddr, serverPort);
114 }
115
Yi Tseng2a340f72018-11-02 16:52:47 -0700116 @Override
117 public String toString() {
Carmelo Casconea71b8492018-12-17 17:47:50 -0800118 return format("%s/%s@%s:%s", deviceId, serviceName, serverAddr, serverPort);
Yi Tseng2a340f72018-11-02 16:52:47 -0700119 }
Carmelo Casconea71b8492018-12-17 17:47:50 -0800120}