blob: 6a7c1e3dcc60be2d470e7c21228edda74d68d511 [file] [log] [blame]
Andrea Campanella378e21a2017-06-07 12:09:59 +02001/*
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
17package org.onosproject.grpc.api;
18
19import com.google.common.annotations.Beta;
20import io.grpc.ManagedChannel;
21import io.grpc.ManagedChannelBuilder;
22import org.onosproject.net.DeviceId;
23
24import java.io.IOException;
25import java.util.Collection;
26import java.util.Map;
27import java.util.Optional;
28
29/**
30 * Abstraction of a gRPC controller. Serves as a one stop shop for obtaining
wu0ef5b6f2017-07-27 10:31:05 +080031 * gRPC ManagedChannels to interact with devices and (un)register observers on event streams from the device.
Andrea Campanella378e21a2017-06-07 12:09:59 +020032 */
33@Beta
34public 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 /**
Carmelo Cascone59f57de2017-07-11 19:55:09 -040060 * Tries to connect to a specific gRPC server, if the connection is successful
Andrea Campanella378e21a2017-06-07 12:09:59 +020061 * 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 /**
Carmelo Cascone59f57de2017-07-11 19:55:09 -040087 * Returns true if the channel associated with the given identifier is open, i.e. the server is able to successfully
88 * responds to RPCs.
89 *
90 * @param channelId channel identifier
91 * @return true if channel is open, false otherwise.
92 */
93 boolean isChannelOpen(GrpcChannelId channelId);
94
95 /**
Andrea Campanella378e21a2017-06-07 12:09:59 +020096 * Returns all ManagedChannels associated to the given device identifier.
97 *
98 * @param deviceId the device for which we are interested.
99 * @return collection of all the ManagedChannels saved in this controller
100 */
101 Collection<ManagedChannel> getChannels(DeviceId deviceId);
102
103 /**
104 * If present, returns the managed channel associated with the given identifier.
105 *
106 * @param channelId the id of the channel
107 * @return the ManagedChannel of the device.
108 */
109 Optional<ManagedChannel> getChannel(GrpcChannelId channelId);
110
111}