blob: ad4324bc3746ead41c60d7c5c205d2dcd52d5ade [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;
Carmelo Casconea71b8492018-12-17 17:47:50 -080022import io.grpc.StatusRuntimeException;
Andrea Campanella378e21a2017-06-07 12:09:59 +020023
Andrea Campanella378e21a2017-06-07 12:09:59 +020024import java.util.Map;
25import java.util.Optional;
Carmelo Cascone3977ea42019-02-28 13:43:42 -080026import java.util.concurrent.CompletableFuture;
Andrea Campanella378e21a2017-06-07 12:09:59 +020027
28/**
Carmelo Cascone47a853b2018-01-05 02:40:58 +010029 * Abstraction of a gRPC controller that stores and manages gRPC channels.
Andrea Campanella378e21a2017-06-07 12:09:59 +020030 */
31@Beta
Yi Tseng2a340f72018-11-02 16:52:47 -070032public interface GrpcChannelController {
Andrea Campanella378e21a2017-06-07 12:09:59 +020033
Carmelo Cascone47a853b2018-01-05 02:40:58 +010034 int CONNECTION_TIMEOUT_SECONDS = 20;
Andrea Campanella378e21a2017-06-07 12:09:59 +020035
36 /**
Carmelo Cascone3977ea42019-02-28 13:43:42 -080037 * Creates a gRPC managed channel from the given builder and opens the
38 * connection. If the connection is successful, returns the managed channel
39 * object and stores the channel internally, associated with the given
40 * channel ID.
Carmelo Cascone47a853b2018-01-05 02:40:58 +010041 * <p>
42 * This method blocks until the channel is open or a timeout expires. By
43 * default the timeout is {@link #CONNECTION_TIMEOUT_SECONDS} seconds. If
Carmelo Casconea71b8492018-12-17 17:47:50 -080044 * the timeout expires, a {@link StatusRuntimeException} is thrown. If
45 * another channel with the same ID already exists, an {@link
46 * IllegalArgumentException} is thrown.
Andrea Campanella378e21a2017-06-07 12:09:59 +020047 *
Carmelo Cascone47a853b2018-01-05 02:40:58 +010048 * @param channelId ID of the channel
49 * @param channelBuilder builder of the managed channel
50 * @return the managed channel created
Carmelo Casconea71b8492018-12-17 17:47:50 -080051 * @throws StatusRuntimeException if the channel cannot be opened
Carmelo Cascone6d57f322018-12-13 23:15:17 -080052 * @throws IllegalArgumentException if a channel with the same ID already
53 * exists
Andrea Campanella378e21a2017-06-07 12:09:59 +020054 */
Carmelo Cascone47a853b2018-01-05 02:40:58 +010055 ManagedChannel connectChannel(GrpcChannelId channelId,
Carmelo Casconea71b8492018-12-17 17:47:50 -080056 ManagedChannelBuilder<?> channelBuilder);
Andrea Campanella378e21a2017-06-07 12:09:59 +020057
58 /**
Carmelo Cascone47a853b2018-01-05 02:40:58 +010059 * Closes the gRPC managed channel (i.e., disconnects from the gRPC server)
Carmelo Cascone6d57f322018-12-13 23:15:17 -080060 * and removes any internal state associated to it.
Andrea Campanella378e21a2017-06-07 12:09:59 +020061 *
Carmelo Cascone47a853b2018-01-05 02:40:58 +010062 * @param channelId ID of the channel to remove
Andrea Campanella378e21a2017-06-07 12:09:59 +020063 */
64 void disconnectChannel(GrpcChannelId channelId);
65
66 /**
Carmelo Cascone47a853b2018-01-05 02:40:58 +010067 * Returns all channels known by this controller, each one mapped to the ID
68 * passed at creation time.
Andrea Campanella378e21a2017-06-07 12:09:59 +020069 *
Carmelo Cascone47a853b2018-01-05 02:40:58 +010070 * @return map of all the channels with their ID as stored in this
71 * controller
Andrea Campanella378e21a2017-06-07 12:09:59 +020072 */
73 Map<GrpcChannelId, ManagedChannel> getChannels();
74
75 /**
Carmelo Cascone47a853b2018-01-05 02:40:58 +010076 * If present, returns the channel associated with the given ID.
Andrea Campanella378e21a2017-06-07 12:09:59 +020077 *
Carmelo Cascone47a853b2018-01-05 02:40:58 +010078 * @param channelId channel ID
79 * @return optional channel
Andrea Campanella378e21a2017-06-07 12:09:59 +020080 */
81 Optional<ManagedChannel> getChannel(GrpcChannelId channelId);
82
Carmelo Cascone3977ea42019-02-28 13:43:42 -080083 /**
84 * Probes the server at the endpoint of the given channel. Returns true if
85 * the server responded to the probe, false otherwise or if the channel does
86 * not exist.
87 *
88 * @param channelId channel ID
89 * @return completable future eventually true if the gRPC server responded
90 * to the probe; false otherwise
91 */
92 CompletableFuture<Boolean> probeChannel(GrpcChannelId channelId);
Andrea Campanella378e21a2017-06-07 12:09:59 +020093}