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; |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 20 | import io.grpc.ManagedChannel; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 21 | import org.onosproject.net.DeviceId; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 22 | import org.onosproject.net.device.DeviceAgentListener; |
| 23 | import org.onosproject.net.provider.ProviderId; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 24 | |
| 25 | /** |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 26 | * Abstraction of controller that manages gRPC clients. |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 27 | * |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 28 | * @param <C> the gRPC client type |
| 29 | */ |
| 30 | @Beta |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 31 | public interface GrpcClientController<C extends GrpcClient> { |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 34 | * Instantiates a new client to operate on the given gRPC channel. Returns |
| 35 | * true if the client was created successfully, false otherwise. Clients |
| 36 | * are identified by device IDs and once created they can be obtained by |
| 37 | * invoking {@link #get(DeviceId)}. |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 38 | * <p> |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 39 | * Only one client can exist for the same device ID. If a client for the |
| 40 | * given device ID already exists, throws an exception. |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 41 | * |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 42 | * @param deviceId device ID |
| 43 | * @param channel gRPC managed channel |
| 44 | * @return true if the client was created, false otherwise |
| 45 | * @throws IllegalArgumentException if a client for the same device ID |
| 46 | * already exists. |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 47 | */ |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 48 | boolean create(DeviceId deviceId, ManagedChannel channel); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 51 | * Returns the gRPC client previously created for the given device ID, or |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 52 | * null if such client does not exist. |
| 53 | * |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 54 | * @param deviceId the device ID |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 55 | * @return the gRPC client of the device if exists; null otherwise |
| 56 | */ |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 57 | C get(DeviceId deviceId); |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Removes the gRPC client for the given device and any gRPC channel state |
| 61 | * associated to it. If no client exists for the given device, the result is |
| 62 | * a no-op. |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 63 | * |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 64 | * @param deviceId the device ID |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 65 | */ |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 66 | void remove(DeviceId deviceId); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 69 | * Adds a listener for device agent events for the given provider. If a |
| 70 | * listener already exists for the given device ID and provider ID, then it |
| 71 | * will be replaced by the new one. |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 72 | * |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 73 | * @param deviceId device ID |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 74 | * @param providerId provider ID |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 75 | * @param listener the device agent listener |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 76 | */ |
| 77 | void addDeviceAgentListener(DeviceId deviceId, ProviderId providerId, |
| 78 | DeviceAgentListener listener); |
| 79 | |
| 80 | /** |
| 81 | * Removes the listener for device agent events that was previously |
| 82 | * registered for the given provider. |
| 83 | * |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 84 | * @param deviceId device ID |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 85 | * @param providerId the provider ID |
| 86 | */ |
| 87 | void removeDeviceAgentListener(DeviceId deviceId, ProviderId providerId); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 88 | } |