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 io.grpc.ManagedChannel; |
| 21 | import io.grpc.ManagedChannelBuilder; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | |
| 24 | import java.io.IOException; |
| 25 | import java.util.Collection; |
| 26 | import java.util.Map; |
| 27 | import java.util.Optional; |
| 28 | |
| 29 | /** |
| 30 | * Abstraction of a gRPC controller. Serves as a one stop shop for obtaining |
| 31 | * gRCP ManagedChannels to interact with devices and (un)register observers on event streams from the device. |
| 32 | */ |
| 33 | @Beta |
| 34 | public interface GrpcController { |
| 35 | |
| 36 | /** |
| 37 | * Adds a StreamObserver on a channel specific for a device. |
| 38 | * |
| 39 | * @param observerId the id of the observer |
| 40 | * @param grpcObserverHandler the manager for the stream. |
| 41 | */ |
| 42 | void addObserver(GrpcStreamObserverId observerId, GrpcObserverHandler grpcObserverHandler); |
| 43 | |
| 44 | /** |
| 45 | * Removes the StreamObserver on a channel specific for a device. |
| 46 | * |
| 47 | * @param observerId the id of the observer |
| 48 | */ |
| 49 | void removeObserver(GrpcStreamObserverId observerId); |
| 50 | |
| 51 | /** |
| 52 | * If present returns the stream observer manager previously added for the given device. |
| 53 | * |
| 54 | * @param observerId the id of the observer. |
| 55 | * @return the ObserverManager |
| 56 | */ |
| 57 | Optional<GrpcObserverHandler> getObserverManager(GrpcStreamObserverId observerId); |
| 58 | |
| 59 | /** |
| 60 | * Tries to connect to a specific gRPC device, if the connection is successful |
| 61 | * returns the ManagedChannel. This method blocks until the channel is setup or a timeout expires. |
| 62 | * By default the timeout is 20 seconds. If the timeout expires and thus the channel can't be set up |
| 63 | * a IOException is thrown. |
| 64 | * |
| 65 | * @param channelId the id of the channel |
| 66 | * @param channelBuilder the builder for the managed channel. |
| 67 | * @return the ManagedChannel created. |
| 68 | * @throws IOException if channel can't be setup. |
| 69 | */ |
| 70 | ManagedChannel connectChannel(GrpcChannelId channelId, ManagedChannelBuilder<?> channelBuilder) throws IOException; |
| 71 | |
| 72 | /** |
| 73 | * Disconnects a gRPC device by removing it's ManagedChannel from this controller. |
| 74 | * |
| 75 | * @param channelId id of the service to remove |
| 76 | */ |
| 77 | void disconnectChannel(GrpcChannelId channelId); |
| 78 | |
| 79 | /** |
| 80 | * Gets all ManagedChannels for the gRPC devices. |
| 81 | * |
| 82 | * @return Map of all the ManagedChannels with their identifier saved in this controller |
| 83 | */ |
| 84 | Map<GrpcChannelId, ManagedChannel> getChannels(); |
| 85 | |
| 86 | /** |
| 87 | * Returns all ManagedChannels associated to the given device identifier. |
| 88 | * |
| 89 | * @param deviceId the device for which we are interested. |
| 90 | * @return collection of all the ManagedChannels saved in this controller |
| 91 | */ |
| 92 | Collection<ManagedChannel> getChannels(DeviceId deviceId); |
| 93 | |
| 94 | /** |
| 95 | * If present, returns the managed channel associated with the given identifier. |
| 96 | * |
| 97 | * @param channelId the id of the channel |
| 98 | * @return the ManagedChannel of the device. |
| 99 | */ |
| 100 | Optional<ManagedChannel> getChannel(GrpcChannelId channelId); |
| 101 | |
| 102 | } |