blob: 4775db5a27e5e5fb6d10ae59509247339cd3ad06 [file] [log] [blame]
Andrea Campanella378e21a2017-06-07 12:09:59 +02001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Andrea Campanella378e21a2017-06-07 12:09:59 +02003 *
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/**
Carmelo Cascone47a853b2018-01-05 02:40:58 +010030 * Abstraction of a gRPC controller that stores and manages gRPC channels.
Andrea Campanella378e21a2017-06-07 12:09:59 +020031 */
32@Beta
Yi Tseng2a340f72018-11-02 16:52:47 -070033public interface GrpcChannelController {
Andrea Campanella378e21a2017-06-07 12:09:59 +020034
Carmelo Cascone47a853b2018-01-05 02:40:58 +010035 int CONNECTION_TIMEOUT_SECONDS = 20;
Andrea Campanella378e21a2017-06-07 12:09:59 +020036
37 /**
Carmelo Cascone47a853b2018-01-05 02:40:58 +010038 * Creates a gRPC managed channel from the given builder and opens a
39 * connection to it. If the connection is successful returns the managed
40 * channel object and stores the channel internally, associated with the
41 * given channel ID.
42 * <p>
43 * This method blocks until the channel is open or a timeout expires. By
44 * default the timeout is {@link #CONNECTION_TIMEOUT_SECONDS} seconds. If
Carmelo Cascone6d57f322018-12-13 23:15:17 -080045 * the timeout expires, an {@link IOException} is thrown. If another channel
46 * with the same ID already exists, an {@link IllegalArgumentException} is
47 * thrown.
Andrea Campanella378e21a2017-06-07 12:09:59 +020048 *
Carmelo Cascone47a853b2018-01-05 02:40:58 +010049 * @param channelId ID of the channel
50 * @param channelBuilder builder of the managed channel
51 * @return the managed channel created
Carmelo Cascone6d57f322018-12-13 23:15:17 -080052 * @throws IOException if the channel cannot be opened
53 * @throws IllegalArgumentException if a channel with the same ID already
54 * exists
Andrea Campanella378e21a2017-06-07 12:09:59 +020055 */
Carmelo Cascone47a853b2018-01-05 02:40:58 +010056 ManagedChannel connectChannel(GrpcChannelId channelId,
57 ManagedChannelBuilder<?> channelBuilder)
58 throws IOException;
Andrea Campanella378e21a2017-06-07 12:09:59 +020059
60 /**
Carmelo Cascone47a853b2018-01-05 02:40:58 +010061 * Closes the gRPC managed channel (i.e., disconnects from the gRPC server)
Carmelo Cascone6d57f322018-12-13 23:15:17 -080062 * and removes any internal state associated to it.
Andrea Campanella378e21a2017-06-07 12:09:59 +020063 *
Carmelo Cascone47a853b2018-01-05 02:40:58 +010064 * @param channelId ID of the channel to remove
Andrea Campanella378e21a2017-06-07 12:09:59 +020065 */
66 void disconnectChannel(GrpcChannelId channelId);
67
68 /**
Carmelo Cascone47a853b2018-01-05 02:40:58 +010069 * Returns all channels known by this controller, each one mapped to the ID
70 * passed at creation time.
Andrea Campanella378e21a2017-06-07 12:09:59 +020071 *
Carmelo Cascone47a853b2018-01-05 02:40:58 +010072 * @return map of all the channels with their ID as stored in this
73 * controller
Andrea Campanella378e21a2017-06-07 12:09:59 +020074 */
75 Map<GrpcChannelId, ManagedChannel> getChannels();
76
77 /**
Carmelo Cascone47a853b2018-01-05 02:40:58 +010078 * Returns true if the channel associated with the given identifier is open,
Carmelo Cascone6d57f322018-12-13 23:15:17 -080079 * i.e. the server is able to successfully reply to RPCs, false otherwise.
Carmelo Cascone59f57de2017-07-11 19:55:09 -040080 *
Carmelo Cascone47a853b2018-01-05 02:40:58 +010081 * @param channelId channel ID
Carmelo Cascone59f57de2017-07-11 19:55:09 -040082 * @return true if channel is open, false otherwise.
83 */
84 boolean isChannelOpen(GrpcChannelId channelId);
85
86 /**
Carmelo Cascone6d57f322018-12-13 23:15:17 -080087 * Returns all channels associated to the given device ID.
Andrea Campanella378e21a2017-06-07 12:09:59 +020088 *
Carmelo Cascone47a853b2018-01-05 02:40:58 +010089 * @param deviceId device ID
90 * @return collection of channels
Andrea Campanella378e21a2017-06-07 12:09:59 +020091 */
92 Collection<ManagedChannel> getChannels(DeviceId deviceId);
93
94 /**
Carmelo Cascone47a853b2018-01-05 02:40:58 +010095 * If present, returns the channel associated with the given ID.
Andrea Campanella378e21a2017-06-07 12:09:59 +020096 *
Carmelo Cascone47a853b2018-01-05 02:40:58 +010097 * @param channelId channel ID
98 * @return optional channel
Andrea Campanella378e21a2017-06-07 12:09:59 +020099 */
100 Optional<ManagedChannel> getChannel(GrpcChannelId channelId);
101
102}