Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 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; |
Carmelo Cascone | a71b849 | 2018-12-17 17:47:50 -0800 | [diff] [blame] | 22 | import io.grpc.StatusRuntimeException; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 23 | |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 24 | import java.util.Map; |
| 25 | import java.util.Optional; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 26 | import java.util.concurrent.CompletableFuture; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 27 | |
| 28 | /** |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 29 | * Abstraction of a gRPC controller that stores and manages gRPC channels. |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 30 | */ |
| 31 | @Beta |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 32 | public interface GrpcChannelController { |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 33 | |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 34 | int CONNECTION_TIMEOUT_SECONDS = 20; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 35 | |
| 36 | /** |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 37 | * 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 Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 41 | * <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 Cascone | a71b849 | 2018-12-17 17:47:50 -0800 | [diff] [blame] | 44 | * 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 Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 47 | * |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 48 | * @param channelId ID of the channel |
| 49 | * @param channelBuilder builder of the managed channel |
| 50 | * @return the managed channel created |
Carmelo Cascone | a71b849 | 2018-12-17 17:47:50 -0800 | [diff] [blame] | 51 | * @throws StatusRuntimeException if the channel cannot be opened |
Carmelo Cascone | 6d57f32 | 2018-12-13 23:15:17 -0800 | [diff] [blame] | 52 | * @throws IllegalArgumentException if a channel with the same ID already |
| 53 | * exists |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 54 | */ |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 55 | ManagedChannel connectChannel(GrpcChannelId channelId, |
Carmelo Cascone | a71b849 | 2018-12-17 17:47:50 -0800 | [diff] [blame] | 56 | ManagedChannelBuilder<?> channelBuilder); |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 57 | |
| 58 | /** |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 59 | * Closes the gRPC managed channel (i.e., disconnects from the gRPC server) |
Carmelo Cascone | 6d57f32 | 2018-12-13 23:15:17 -0800 | [diff] [blame] | 60 | * and removes any internal state associated to it. |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 61 | * |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 62 | * @param channelId ID of the channel to remove |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 63 | */ |
| 64 | void disconnectChannel(GrpcChannelId channelId); |
| 65 | |
| 66 | /** |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 67 | * Returns all channels known by this controller, each one mapped to the ID |
| 68 | * passed at creation time. |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 69 | * |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 70 | * @return map of all the channels with their ID as stored in this |
| 71 | * controller |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 72 | */ |
| 73 | Map<GrpcChannelId, ManagedChannel> getChannels(); |
| 74 | |
| 75 | /** |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 76 | * If present, returns the channel associated with the given ID. |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 77 | * |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 78 | * @param channelId channel ID |
| 79 | * @return optional channel |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 80 | */ |
| 81 | Optional<ManagedChannel> getChannel(GrpcChannelId channelId); |
| 82 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 83 | /** |
| 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 Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 93 | } |