blob: b47d499cc6b36d68ede29024c3b2f9ab0bb7e496 [file] [log] [blame]
Yi Tseng2a340f72018-11-02 16:52:47 -07001/*
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
17package org.onosproject.grpc.api;
18
19import com.google.common.annotations.Beta;
20import org.onosproject.net.DeviceId;
Carmelo Cascone3977ea42019-02-28 13:43:42 -080021import org.onosproject.net.device.DeviceAgentListener;
22import org.onosproject.net.provider.ProviderId;
Yi Tseng2a340f72018-11-02 16:52:47 -070023
24/**
Carmelo Cascone3977ea42019-02-28 13:43:42 -080025 * Abstraction of controller that manages gRPC clients.
Yi Tseng2a340f72018-11-02 16:52:47 -070026 *
27 * @param <K> the gRPC client key
28 * @param <C> the gRPC client type
29 */
30@Beta
31public interface GrpcClientController<K extends GrpcClientKey, C extends GrpcClient> {
32
33 /**
Yi Tsengd7716482018-10-31 15:34:30 -070034 * 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 Tseng2a340f72018-11-02 16:52:47 -070036 * obtained by invoking {@link #getClient(DeviceId)}.
Yi Tsengd7716482018-10-31 15:34:30 -070037 * <p>
Carmelo Cascone3977ea42019-02-28 13:43:42 -080038 * 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 Tsengd7716482018-10-31 15:34:30 -070043 * 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 Cascone3977ea42019-02-28 13:43:42 -080045 * already exists. Otherwise, if a client for the same device ID but
46 * different client key already exists, throws an exception.
Yi Tseng2a340f72018-11-02 16:52:47 -070047 *
48 * @param clientKey the client key
Yi Tsengd7716482018-10-31 15:34:30 -070049 * @return true if the client was created and the channel to the server is
50 * open; false otherwise
Carmelo Cascone3977ea42019-02-28 13:43:42 -080051 * @throws IllegalArgumentException if a client for the same device ID but
52 * different client key already exists.
Yi Tseng2a340f72018-11-02 16:52:47 -070053 */
54 boolean createClient(K clientKey);
55
56 /**
Carmelo Cascone3977ea42019-02-28 13:43:42 -080057 * Returns the gRPC client previously created for the given device, or null
58 * if such client does not exist.
Yi Tseng2a340f72018-11-02 16:52:47 -070059 *
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 Cascone3977ea42019-02-28 13:43:42 -080066 * 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 Tseng2a340f72018-11-02 16:52:47 -070078 *
79 * @param deviceId the device identifier
80 */
81 void removeClient(DeviceId deviceId);
82
83 /**
Carmelo Cascone3977ea42019-02-28 13:43:42 -080084 * Similar to {@link #removeClient(DeviceId)} but uses the client key to
85 * identify the client to remove.
Yi Tseng2a340f72018-11-02 16:52:47 -070086 *
Carmelo Cascone3977ea42019-02-28 13:43:42 -080087 * @param clientKey the client key
Yi Tseng2a340f72018-11-02 16:52:47 -070088 */
Carmelo Cascone3977ea42019-02-28 13:43:42 -080089 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 Tseng2a340f72018-11-02 16:52:47 -0700109}