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; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 22 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 23 | import java.net.URI; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 24 | import java.util.Optional; |
| 25 | |
| 26 | /** |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 27 | * Abstraction of a gRPC controller that creates, stores, and manages gRPC |
| 28 | * channels. |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 29 | */ |
| 30 | @Beta |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 31 | public interface GrpcChannelController { |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 32 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Creates a gRPC managed channel to the server identified by the given |
| 35 | * channel URI. The channel is created using the information contained in the |
| 36 | * URI, as such, the URI is expected to have absolute server-based form, |
| 37 | * where the scheme can be either {@code grpc:} or {@code grpcs:}, to |
| 38 | * indicated respectively a plaintext or secure channel. |
| 39 | * <p> |
| 40 | * Example of valid URIs are: <pre> {@code |
| 41 | * grpc://10.0.0.1:50001 |
| 42 | * grpcs://10.0.0.1:50001 |
| 43 | * grpcs://myserver.local:50001 |
| 44 | * }</pre> |
| 45 | * <p> |
| 46 | * This method creates and stores the channel instance associating it to the |
| 47 | * passed URI, but it does not make any attempt to connect the channel or |
| 48 | * verify server reachability. |
| 49 | * <p> |
| 50 | * If another channel with the same URI already exists, an {@link |
| 51 | * IllegalArgumentException} is thrown. To create multiple channels to the |
| 52 | * same server-port combination, URI file or query parameters can be used. |
| 53 | * For example: <pre> {@code |
| 54 | * grpc://10.0.0.1:50001/foo |
| 55 | * grpc://10.0.0.1:50001/bar |
| 56 | * grpc://10.0.0.1:50001/bar?param=1 |
| 57 | * grpc://10.0.0.1:50001/bar?param=2 |
| 58 | * }</pre> |
| 59 | * <p> |
| 60 | * When creating secure channels (i.e., {@code grpcs:)}, the current |
| 61 | * implementation provides encryption but not authentication, any server |
| 62 | * certificate, even if insecure, will be accepted. |
| 63 | * |
| 64 | * @param channelUri channel URI |
| 65 | * @return the managed channel created |
| 66 | * @throws IllegalArgumentException if a channel with the same channel URI |
| 67 | * already exists |
| 68 | */ |
| 69 | ManagedChannel create(URI channelUri); |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 70 | |
| 71 | /** |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 72 | * Similar to {@link #create(URI)} but does not create the chanel instance, |
| 73 | * instead, it uses the given channel builder to create it. As such, there |
| 74 | * is no requirement on the format of the URI, any URI can be used. The |
| 75 | * implementation might modify the passed builder for purposes specific to |
| 76 | * this controller, such as to enable gRPC message logging. |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 77 | * |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 78 | * @param channelUri URI identifying the channel |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 79 | * @param channelBuilder builder of the managed channel |
| 80 | * @return the managed channel created |
Carmelo Cascone | 6d57f32 | 2018-12-13 23:15:17 -0800 | [diff] [blame] | 81 | * @throws IllegalArgumentException if a channel with the same ID already |
| 82 | * exists |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 83 | */ |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 84 | ManagedChannel create(URI channelUri, |
| 85 | ManagedChannelBuilder<?> channelBuilder); |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 86 | |
| 87 | /** |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 88 | * Closes and destroys the gRPC channel associated to the given URI and |
| 89 | * removes any internal state associated to it. |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 90 | * |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 91 | * @param channelUri URI of the channel to remove |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 92 | */ |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 93 | void destroy(URI channelUri); |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 94 | |
| 95 | /** |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 96 | * If present, returns the channel associated with the given URI. |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 97 | * |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 98 | * @param channelUri channel URI |
Carmelo Cascone | 47a853b | 2018-01-05 02:40:58 +0100 | [diff] [blame] | 99 | * @return optional channel |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 100 | */ |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 101 | Optional<ManagedChannel> get(URI channelUri); |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 102 | } |