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 | * GrpcStreamObserver identifier suitable as an external key. |
| 27 | * <p> |
| 28 | * This class is immutable.</p> |
| 29 | */ |
| 30 | @Beta |
| 31 | public final class GrpcStreamObserverId extends Identifier<String> { |
| 32 | |
| 33 | private GrpcServiceId serviceId; |
| 34 | private String streamName; |
| 35 | |
| 36 | /** |
| 37 | * Instantiates a new GrpcStreamObserver id. |
| 38 | * |
| 39 | * @param serviceId the service id |
| 40 | * @param streamName the name of the stream on that device |
| 41 | */ |
| 42 | private GrpcStreamObserverId(GrpcServiceId serviceId, String streamName) { |
| 43 | super(serviceId.toString() + ":" + streamName); |
| 44 | checkNotNull(serviceId, "service id must not be null"); |
| 45 | checkNotNull(streamName, "stream name must not be null"); |
| 46 | checkArgument(!streamName.isEmpty(), "stream name must not be empty"); |
| 47 | this.serviceId = serviceId; |
| 48 | this.streamName = streamName; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Returns the id of the service that this stream observer uses. |
| 53 | * |
| 54 | * @return the service Id |
| 55 | */ |
| 56 | public GrpcServiceId serviceId() { |
| 57 | return serviceId; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Returns the name of this stream. |
| 62 | * |
| 63 | * @return the stream name |
| 64 | */ |
| 65 | public String streamName() { |
| 66 | return streamName; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Creates a gRPC Stream Observer identifier from the specified service id and |
| 71 | * stream name provided. |
| 72 | * |
| 73 | * @param id service id |
| 74 | * @param streamName stream name |
| 75 | * @return stream name |
| 76 | */ |
| 77 | public static GrpcStreamObserverId of(GrpcServiceId id, String streamName) { |
| 78 | return new GrpcStreamObserverId(id, streamName); |
| 79 | } |
| 80 | } |