blob: d1d0b0ffcbca680ea9d53800ad18312d3ca495f8 [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
Carmelo Cascone3977ea42019-02-28 13:43:42 -080023import java.net.URI;
24
Yi Tseng2a340f72018-11-02 16:52:47 -070025import static com.google.common.base.Preconditions.checkArgument;
26import static com.google.common.base.Preconditions.checkNotNull;
Carmelo Cascone3977ea42019-02-28 13:43:42 -080027import static com.google.common.base.Strings.isNullOrEmpty;
Carmelo Casconea71b8492018-12-17 17:47:50 -080028import static java.lang.String.format;
Yi Tseng2a340f72018-11-02 16:52:47 -070029
30/**
31 * Key that uniquely identifies a gRPC client.
32 */
33@Beta
34public class GrpcClientKey {
Carmelo Cascone3977ea42019-02-28 13:43:42 -080035
36 private static final String GRPC = "grpc";
37 private static final String GRPCS = "grpcs";
38
Yi Tseng2a340f72018-11-02 16:52:47 -070039 private final String serviceName;
40 private final DeviceId deviceId;
Carmelo Cascone3977ea42019-02-28 13:43:42 -080041 private final URI serverUri;
Yi Tseng2a340f72018-11-02 16:52:47 -070042
43 /**
44 * Creates a new client key.
45 *
46 * @param serviceName gRPC service name of the client
Carmelo Cascone3977ea42019-02-28 13:43:42 -080047 * @param deviceId ONOS device ID
48 * @param serverUri gRPC server URI
Yi Tseng2a340f72018-11-02 16:52:47 -070049 */
Carmelo Cascone3977ea42019-02-28 13:43:42 -080050 public GrpcClientKey(String serviceName, DeviceId deviceId, URI serverUri) {
Yi Tseng2a340f72018-11-02 16:52:47 -070051 checkNotNull(serviceName);
52 checkNotNull(deviceId);
Carmelo Cascone3977ea42019-02-28 13:43:42 -080053 checkNotNull(serverUri);
Yi Tseng2a340f72018-11-02 16:52:47 -070054 checkArgument(!serviceName.isEmpty(),
Carmelo Cascone3977ea42019-02-28 13:43:42 -080055 "Service name can not be null");
56 checkArgument(serverUri.getScheme().equals(GRPC)
57 || serverUri.getScheme().equals(GRPCS),
58 format("Server URI scheme must be %s or %s", GRPC, GRPCS));
59 checkArgument(!isNullOrEmpty(serverUri.getHost()),
60 "Server host address should not be empty");
61 checkArgument(serverUri.getPort() > 0 && serverUri.getPort() <= 65535, "Invalid server port");
Yi Tseng2a340f72018-11-02 16:52:47 -070062 this.serviceName = serviceName;
63 this.deviceId = deviceId;
Carmelo Cascone3977ea42019-02-28 13:43:42 -080064 this.serverUri = serverUri;
Yi Tseng2a340f72018-11-02 16:52:47 -070065 }
66
67 /**
68 * Gets the gRPC service name of the client.
69 *
70 * @return the service name
71 */
72 public String serviceName() {
73 return serviceName;
74 }
75
76 /**
77 * Gets the device ID.
78 *
79 * @return the device ID
80 */
81 public DeviceId deviceId() {
82 return deviceId;
83 }
84
85 /**
Carmelo Cascone3977ea42019-02-28 13:43:42 -080086 * Returns the gRPC server URI.
Yi Tseng2a340f72018-11-02 16:52:47 -070087 *
Carmelo Cascone3977ea42019-02-28 13:43:42 -080088 * @return the gRPC server URI.
Yi Tseng2a340f72018-11-02 16:52:47 -070089 */
Carmelo Cascone3977ea42019-02-28 13:43:42 -080090 public URI serveUri() {
91 return serverUri;
Yi Tseng2a340f72018-11-02 16:52:47 -070092 }
93
94 /**
Carmelo Cascone3977ea42019-02-28 13:43:42 -080095 * Returns true if the client requires TLS/SSL, false otherwise.
Yi Tseng2a340f72018-11-02 16:52:47 -070096 *
Carmelo Cascone3977ea42019-02-28 13:43:42 -080097 * @return boolean
Yi Tseng2a340f72018-11-02 16:52:47 -070098 */
Carmelo Cascone3977ea42019-02-28 13:43:42 -080099 public boolean requiresSecureChannel() {
100 return serverUri.getScheme().equals(GRPCS);
Yi Tseng2a340f72018-11-02 16:52:47 -0700101 }
102
103 @Override
104 public boolean equals(Object o) {
105 if (this == o) {
106 return true;
107 }
108 if (o == null || getClass() != o.getClass()) {
109 return false;
110 }
111 GrpcClientKey that = (GrpcClientKey) o;
Carmelo Cascone3977ea42019-02-28 13:43:42 -0800112 return Objects.equal(serviceName, that.serviceName) &&
Yi Tseng2a340f72018-11-02 16:52:47 -0700113 Objects.equal(deviceId, that.deviceId) &&
Carmelo Cascone3977ea42019-02-28 13:43:42 -0800114 Objects.equal(serverUri, that.serverUri);
Yi Tseng2a340f72018-11-02 16:52:47 -0700115 }
116
117 @Override
118 public int hashCode() {
Carmelo Cascone3977ea42019-02-28 13:43:42 -0800119 return Objects.hashCode(serviceName, deviceId, serverUri);
Yi Tseng2a340f72018-11-02 16:52:47 -0700120 }
121
Yi Tseng2a340f72018-11-02 16:52:47 -0700122 @Override
123 public String toString() {
Carmelo Cascone3977ea42019-02-28 13:43:42 -0800124 return format("%s/%s@%s", deviceId, serviceName, serverUri);
Yi Tseng2a340f72018-11-02 16:52:47 -0700125 }
Carmelo Casconea71b8492018-12-17 17:47:50 -0800126}