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; |
| 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 | /** |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 30 | * Abstraction of a gRPC controller that stores and manages gRPC channels. |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 31 | */ |
| 32 | @Beta |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 33 | public interface GrpcChannelController { |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 34 | |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 35 | int CONNECTION_TIMEOUT_SECONDS = 20; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 36 | |
| 37 | /** |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 38 | * 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 Cascone | 6d57f32 | 2018-12-13 23:15:17 -0800 | [diff] [blame] | 45 | * 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 Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 48 | * |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 49 | * @param channelId ID of the channel |
| 50 | * @param channelBuilder builder of the managed channel |
| 51 | * @return the managed channel created |
Carmelo Cascone | 6d57f32 | 2018-12-13 23:15:17 -0800 | [diff] [blame] | 52 | * @throws IOException if the channel cannot be opened |
| 53 | * @throws IllegalArgumentException if a channel with the same ID already |
| 54 | * exists |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 55 | */ |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 56 | ManagedChannel connectChannel(GrpcChannelId channelId, |
| 57 | ManagedChannelBuilder<?> channelBuilder) |
| 58 | throws IOException; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 59 | |
| 60 | /** |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 61 | * Closes the gRPC managed channel (i.e., disconnects from the gRPC server) |
Carmelo Cascone | 6d57f32 | 2018-12-13 23:15:17 -0800 | [diff] [blame] | 62 | * and removes any internal state associated to it. |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 63 | * |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 64 | * @param channelId ID of the channel to remove |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 65 | */ |
| 66 | void disconnectChannel(GrpcChannelId channelId); |
| 67 | |
| 68 | /** |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 69 | * Returns all channels known by this controller, each one mapped to the ID |
| 70 | * passed at creation time. |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 71 | * |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 72 | * @return map of all the channels with their ID as stored in this |
| 73 | * controller |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 74 | */ |
| 75 | Map<GrpcChannelId, ManagedChannel> getChannels(); |
| 76 | |
| 77 | /** |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 78 | * Returns true if the channel associated with the given identifier is open, |
Carmelo Cascone | 6d57f32 | 2018-12-13 23:15:17 -0800 | [diff] [blame] | 79 | * i.e. the server is able to successfully reply to RPCs, false otherwise. |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 80 | * |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 81 | * @param channelId channel ID |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 82 | * @return true if channel is open, false otherwise. |
| 83 | */ |
| 84 | boolean isChannelOpen(GrpcChannelId channelId); |
| 85 | |
| 86 | /** |
Carmelo Cascone | 6d57f32 | 2018-12-13 23:15:17 -0800 | [diff] [blame] | 87 | * Returns all channels associated to the given device ID. |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 88 | * |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 89 | * @param deviceId device ID |
| 90 | * @return collection of channels |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 91 | */ |
| 92 | Collection<ManagedChannel> getChannels(DeviceId deviceId); |
| 93 | |
| 94 | /** |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 95 | * If present, returns the channel associated with the given ID. |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 96 | * |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 97 | * @param channelId channel ID |
| 98 | * @return optional channel |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 99 | */ |
| 100 | Optional<ManagedChannel> getChannel(GrpcChannelId channelId); |
| 101 | |
| 102 | } |