Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 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; |
| 20 | import org.onlab.util.Identifier; |
| 21 | |
| 22 | import static com.google.common.base.Preconditions.checkArgument; |
| 23 | import static com.google.common.base.Preconditions.checkNotNull; |
| 24 | |
| 25 | /** |
| 26 | * gRPCService identifier suitable as an external key. |
| 27 | * <p> |
| 28 | * This class is immutable.</p> |
| 29 | */ |
| 30 | @Beta |
| 31 | public final class GrpcServiceId extends Identifier<String> { |
| 32 | |
| 33 | private final GrpcChannelId channelId; |
| 34 | |
| 35 | private final String serviceName; |
| 36 | |
| 37 | /** |
| 38 | * Instantiates a new gRPC Service id. |
| 39 | * |
| 40 | * @param channelId the channel id |
| 41 | * @param serviceName the name of the service on that channel |
| 42 | */ |
| 43 | private GrpcServiceId(GrpcChannelId channelId, String serviceName) { |
| 44 | super(channelId.toString() + ":" + serviceName); |
| 45 | checkNotNull(channelId, "channel id must not be null"); |
| 46 | checkNotNull(serviceName, "service name must not be null"); |
| 47 | checkArgument(!serviceName.isEmpty(), "service name must not be empty"); |
| 48 | this.channelId = channelId; |
| 49 | this.serviceName = serviceName; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Returns the id of the channel that this service uses. |
| 54 | * |
| 55 | * @return the channel Id |
| 56 | */ |
| 57 | public GrpcChannelId channelId() { |
| 58 | return channelId; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Returns the name of this service. |
| 63 | * |
| 64 | * @return the service name |
| 65 | */ |
| 66 | public String serviceName() { |
| 67 | return serviceName; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Creates a gRPC Service identifier from the specified device id and |
| 72 | * service name provided. |
| 73 | * |
| 74 | * @param id channel id |
| 75 | * @param serviceName name of the service |
| 76 | * @return service name |
| 77 | */ |
| 78 | public static GrpcServiceId of(GrpcChannelId id, String serviceName) { |
| 79 | return new GrpcServiceId(id, serviceName); |
| 80 | } |
| 81 | } |