Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 org.onosproject.net.DeviceId; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 21 | import org.onosproject.net.device.DeviceAgentListener; |
| 22 | import org.onosproject.net.provider.ProviderId; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 23 | |
| 24 | /** |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 25 | * Abstraction of controller that manages gRPC clients. |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 26 | * |
| 27 | * @param <K> the gRPC client key |
| 28 | * @param <C> the gRPC client type |
| 29 | */ |
| 30 | @Beta |
| 31 | public interface GrpcClientController<K extends GrpcClientKey, C extends GrpcClient> { |
| 32 | |
| 33 | /** |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 34 | * Instantiates a new client to operate on a gRPC server identified by the |
| 35 | * given information. As a result of this method, a client can be later |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 36 | * obtained by invoking {@link #getClient(DeviceId)}. |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 37 | * <p> |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 38 | * Upon creation, a connection to the server is automatically started, which |
| 39 | * blocks execution. If the connection is successful, the client is created |
| 40 | * and this method returns true, otherwise (e.g., socket error) any state |
| 41 | * associated with this client is destroyed and returns false. |
| 42 | * <p> |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 43 | * Only one client can exist for the same device ID. Calls to this method |
| 44 | * are idempotent fot the same client key, i.e. returns true if such client |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 45 | * already exists. Otherwise, if a client for the same device ID but |
| 46 | * different client key already exists, throws an exception. |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 47 | * |
| 48 | * @param clientKey the client key |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 49 | * @return true if the client was created and the channel to the server is |
| 50 | * open; false otherwise |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 51 | * @throws IllegalArgumentException if a client for the same device ID but |
| 52 | * different client key already exists. |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 53 | */ |
| 54 | boolean createClient(K clientKey); |
| 55 | |
| 56 | /** |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 57 | * Returns the gRPC client previously created for the given device, or null |
| 58 | * if such client does not exist. |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 59 | * |
| 60 | * @param deviceId the device identifier |
| 61 | * @return the gRPC client of the device if exists; null otherwise |
| 62 | */ |
| 63 | C getClient(DeviceId deviceId); |
| 64 | |
| 65 | /** |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 66 | * Returns the gRPC client previously created for the given client key, or |
| 67 | * null if such client does not exist. |
| 68 | * |
| 69 | * @param clientKey client key |
| 70 | * @return the gRPC client of the device if exists; null otherwise |
| 71 | */ |
| 72 | C getClient(K clientKey); |
| 73 | |
| 74 | /** |
| 75 | * Removes the gRPC client for the given device and any gRPC channel state |
| 76 | * associated to it. If no client exists for the given device, the result is |
| 77 | * a no-op. |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 78 | * |
| 79 | * @param deviceId the device identifier |
| 80 | */ |
| 81 | void removeClient(DeviceId deviceId); |
| 82 | |
| 83 | /** |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 84 | * Similar to {@link #removeClient(DeviceId)} but uses the client key to |
| 85 | * identify the client to remove. |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 86 | * |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 87 | * @param clientKey the client key |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 88 | */ |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame^] | 89 | void removeClient(K clientKey); |
| 90 | |
| 91 | /** |
| 92 | * Adds a listener for device agent events for the given provider. |
| 93 | * |
| 94 | * @param deviceId device identifier |
| 95 | * @param providerId provider ID |
| 96 | * @param listener the device agent listener |
| 97 | */ |
| 98 | void addDeviceAgentListener(DeviceId deviceId, ProviderId providerId, |
| 99 | DeviceAgentListener listener); |
| 100 | |
| 101 | /** |
| 102 | * Removes the listener for device agent events that was previously |
| 103 | * registered for the given provider. |
| 104 | * |
| 105 | * @param deviceId device identifier |
| 106 | * @param providerId the provider ID |
| 107 | */ |
| 108 | void removeDeviceAgentListener(DeviceId deviceId, ProviderId providerId); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 109 | } |