Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.grpc.api; |
| 18 | |
| 19 | import com.google.common.annotations.Beta; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 20 | import com.google.common.base.Objects; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 23 | import java.net.URI; |
| 24 | |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 25 | import static com.google.common.base.Preconditions.checkArgument; |
| 26 | import static com.google.common.base.Preconditions.checkNotNull; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 27 | import static com.google.common.base.Strings.isNullOrEmpty; |
Carmelo Cascone | a71b849 | 2018-12-17 17:47:50 -0800 | [diff] [blame] | 28 | import static java.lang.String.format; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Key that uniquely identifies a gRPC client. |
| 32 | */ |
| 33 | @Beta |
| 34 | public class GrpcClientKey { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 35 | |
| 36 | private static final String GRPC = "grpc"; |
| 37 | private static final String GRPCS = "grpcs"; |
| 38 | |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 39 | private final String serviceName; |
| 40 | private final DeviceId deviceId; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 41 | private final URI serverUri; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Creates a new client key. |
| 45 | * |
| 46 | * @param serviceName gRPC service name of the client |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 47 | * @param deviceId ONOS device ID |
| 48 | * @param serverUri gRPC server URI |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 49 | */ |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 50 | public GrpcClientKey(String serviceName, DeviceId deviceId, URI serverUri) { |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 51 | checkNotNull(serviceName); |
| 52 | checkNotNull(deviceId); |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 53 | checkNotNull(serverUri); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 54 | checkArgument(!serviceName.isEmpty(), |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 55 | "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 Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 62 | this.serviceName = serviceName; |
| 63 | this.deviceId = deviceId; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 64 | this.serverUri = serverUri; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 65 | } |
| 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 Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 86 | * Returns the gRPC server URI. |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 87 | * |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 88 | * @return the gRPC server URI. |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 89 | */ |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 90 | public URI serveUri() { |
| 91 | return serverUri; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 95 | * Returns true if the client requires TLS/SSL, false otherwise. |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 96 | * |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 97 | * @return boolean |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 98 | */ |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 99 | public boolean requiresSecureChannel() { |
| 100 | return serverUri.getScheme().equals(GRPCS); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 101 | } |
| 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 Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 112 | return Objects.equal(serviceName, that.serviceName) && |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 113 | Objects.equal(deviceId, that.deviceId) && |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 114 | Objects.equal(serverUri, that.serverUri); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public int hashCode() { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 119 | return Objects.hashCode(serviceName, deviceId, serverUri); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 122 | @Override |
| 123 | public String toString() { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 124 | return format("%s/%s@%s", deviceId, serviceName, serverUri); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 125 | } |
Carmelo Cascone | a71b849 | 2018-12-17 17:47:50 -0800 | [diff] [blame] | 126 | } |