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.ctl; |
| 18 | |
| 19 | import com.google.common.collect.Maps; |
| 20 | import com.google.common.util.concurrent.Striped; |
| 21 | import io.grpc.ManagedChannel; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 22 | import org.onosproject.event.AbstractListenerManager; |
| 23 | import org.onosproject.event.Event; |
| 24 | import org.onosproject.event.EventListener; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 25 | import org.onosproject.grpc.api.GrpcClient; |
| 26 | import org.onosproject.grpc.api.GrpcClientController; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 27 | import org.onosproject.net.DeviceId; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 28 | import org.onosproject.net.device.DeviceAgentEvent; |
| 29 | import org.onosproject.net.device.DeviceAgentListener; |
| 30 | import org.onosproject.net.provider.ProviderId; |
Ray Milkey | 5739b2c | 2018-11-06 14:04:51 -0800 | [diff] [blame] | 31 | import org.osgi.service.component.annotations.Activate; |
Ray Milkey | 5739b2c | 2018-11-06 14:04:51 -0800 | [diff] [blame] | 32 | import org.osgi.service.component.annotations.Deactivate; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 33 | import org.slf4j.Logger; |
| 34 | |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 35 | import java.util.Map; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 36 | import java.util.concurrent.ConcurrentMap; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 37 | import java.util.concurrent.locks.Lock; |
| 38 | import java.util.function.Supplier; |
| 39 | |
| 40 | import static com.google.common.base.Preconditions.checkNotNull; |
Carmelo Cascone | a71b849 | 2018-12-17 17:47:50 -0800 | [diff] [blame] | 41 | import static java.lang.String.format; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 42 | import static org.slf4j.LoggerFactory.getLogger; |
| 43 | |
| 44 | /** |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 45 | * Abstract class of a controller for gRPC clients which provides means to |
| 46 | * create clients, associate device agent listeners to them and register other |
| 47 | * event listeners. |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 48 | * |
| 49 | * @param <C> the gRPC client type |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 50 | * @param <E> the event type of the gRPC client |
| 51 | * @param <L> the event listener of event {@link E} |
| 52 | */ |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 53 | public abstract class AbstractGrpcClientController |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 54 | <C extends GrpcClient, E extends Event, L extends EventListener<E>> |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 55 | extends AbstractListenerManager<E, L> |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 56 | implements GrpcClientController<C> { |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * The default max inbound message size (MB). |
| 60 | */ |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 61 | private static final int DEFAULT_DEVICE_LOCK_SIZE = 30; |
| 62 | |
| 63 | private final Logger log = getLogger(getClass()); |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 64 | private final Map<DeviceId, C> clients = Maps.newHashMap(); |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 65 | private final ConcurrentMap<DeviceId, ConcurrentMap<ProviderId, DeviceAgentListener>> |
| 66 | deviceAgentListeners = Maps.newConcurrentMap(); |
| 67 | private final Class<E> eventClass; |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 68 | private final String serviceName; |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 69 | private final Striped<Lock> stripedLocks = Striped.lock(DEFAULT_DEVICE_LOCK_SIZE); |
| 70 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 71 | public AbstractGrpcClientController(Class<E> eventClass, String serviceName) { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 72 | this.eventClass = eventClass; |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 73 | this.serviceName = serviceName; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 76 | @Activate |
| 77 | public void activate() { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 78 | eventDispatcher.addSink(eventClass, listenerRegistry); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 79 | log.info("Started"); |
| 80 | } |
| 81 | |
| 82 | @Deactivate |
| 83 | public void deactivate() { |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 84 | eventDispatcher.removeSink(eventClass); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 85 | clients.clear(); |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 86 | deviceAgentListeners.clear(); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 87 | log.info("Stopped"); |
| 88 | } |
| 89 | |
| 90 | @Override |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 91 | public boolean create(DeviceId deviceId, ManagedChannel channel) { |
| 92 | checkNotNull(deviceId); |
| 93 | checkNotNull(channel); |
| 94 | return withDeviceLock(() -> doCreateClient(deviceId, channel), deviceId); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 97 | private boolean doCreateClient(DeviceId deviceId, ManagedChannel channel) { |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 98 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 99 | if (clients.containsKey(deviceId)) { |
| 100 | throw new IllegalArgumentException(format( |
| 101 | "A %s client already exists for %s", serviceName, deviceId)); |
Brian O'Connor | c694383 | 2018-12-12 17:27:11 -0800 | [diff] [blame] | 102 | } |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 103 | |
Carmelo Cascone | 4b61631 | 2019-04-17 14:15:45 -0700 | [diff] [blame] | 104 | log.debug("Creating {}...", clientName(deviceId)); |
Carmelo Cascone | d51a555 | 2019-04-13 01:22:25 -0700 | [diff] [blame] | 105 | |
Carmelo Cascone | a71b849 | 2018-12-17 17:47:50 -0800 | [diff] [blame] | 106 | final C client; |
| 107 | try { |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 108 | client = createClientInstance(deviceId, channel); |
Carmelo Cascone | a71b849 | 2018-12-17 17:47:50 -0800 | [diff] [blame] | 109 | } catch (Throwable e) { |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 110 | log.error("Exception while creating {}", clientName(deviceId), e); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 111 | return false; |
| 112 | } |
Carmelo Cascone | a71b849 | 2018-12-17 17:47:50 -0800 | [diff] [blame] | 113 | |
| 114 | if (client == null) { |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 115 | log.error("Unable to create {}, implementation returned null...", |
| 116 | clientName(deviceId)); |
Carmelo Cascone | a71b849 | 2018-12-17 17:47:50 -0800 | [diff] [blame] | 117 | return false; |
| 118 | } |
| 119 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 120 | clients.put(deviceId, client); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 121 | return true; |
| 122 | } |
| 123 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 124 | protected abstract C createClientInstance(DeviceId deviceId, ManagedChannel channel); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 125 | |
| 126 | @Override |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 127 | public C get(DeviceId deviceId) { |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 128 | checkNotNull(deviceId); |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 129 | return withDeviceLock(() -> clients.get(deviceId), deviceId); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 132 | @Override |
| 133 | public void remove(DeviceId deviceId) { |
| 134 | checkNotNull(deviceId); |
| 135 | withDeviceLock(() -> { |
| 136 | final C client = clients.remove(deviceId); |
| 137 | if (client != null) { |
Carmelo Cascone | 4b61631 | 2019-04-17 14:15:45 -0700 | [diff] [blame] | 138 | log.debug("Removing {}...", clientName(deviceId)); |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 139 | client.shutdown(); |
| 140 | } |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 141 | return null; |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 142 | }, deviceId); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 145 | @Override |
| 146 | public void addDeviceAgentListener(DeviceId deviceId, ProviderId providerId, DeviceAgentListener listener) { |
| 147 | checkNotNull(deviceId, "deviceId cannot be null"); |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 148 | checkNotNull(providerId, "providerId cannot be null"); |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 149 | checkNotNull(listener, "listener cannot be null"); |
| 150 | deviceAgentListeners.putIfAbsent(deviceId, Maps.newConcurrentMap()); |
| 151 | deviceAgentListeners.get(deviceId).put(providerId, listener); |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public void removeDeviceAgentListener(DeviceId deviceId, ProviderId providerId) { |
| 156 | checkNotNull(deviceId, "deviceId cannot be null"); |
| 157 | checkNotNull(providerId, "listener cannot be null"); |
| 158 | deviceAgentListeners.computeIfPresent(deviceId, (did, listeners) -> { |
| 159 | listeners.remove(providerId); |
| 160 | return listeners.isEmpty() ? null : listeners; |
| 161 | }); |
| 162 | } |
| 163 | |
| 164 | public void postEvent(E event) { |
| 165 | checkNotNull(event); |
| 166 | post(event); |
| 167 | } |
| 168 | |
| 169 | public void postEvent(DeviceAgentEvent event) { |
| 170 | // We should have only one event delivery mechanism. We have two now |
| 171 | // because we have two different types of events, DeviceAgentEvent and |
| 172 | // controller/protocol specific ones (e.g. P4Runtime or gNMI). |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 173 | // TODO: extend device agent event to allow delivery of protocol-specific |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 174 | // events, e.g. packet-in |
| 175 | checkNotNull(event); |
| 176 | if (deviceAgentListeners.containsKey(event.subject())) { |
| 177 | deviceAgentListeners.get(event.subject()).values() |
| 178 | .forEach(l -> l.event(event)); |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 179 | } |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | private <U> U withDeviceLock(Supplier<U> task, DeviceId deviceId) { |
| 183 | final Lock lock = stripedLocks.get(deviceId); |
| 184 | lock.lock(); |
| 185 | try { |
| 186 | return task.get(); |
| 187 | } finally { |
| 188 | lock.unlock(); |
| 189 | } |
| 190 | } |
Carmelo Cascone | a71b849 | 2018-12-17 17:47:50 -0800 | [diff] [blame] | 191 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 192 | private String clientName(DeviceId deviceId) { |
| 193 | return format("%s client for %s", serviceName, deviceId); |
Carmelo Cascone | a71b849 | 2018-12-17 17:47:50 -0800 | [diff] [blame] | 194 | } |
Yi Tseng | 2a340f7 | 2018-11-02 16:52:47 -0700 | [diff] [blame] | 195 | } |