Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [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.p4runtime.ctl; |
| 18 | |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 19 | import com.google.common.cache.CacheBuilder; |
| 20 | import com.google.common.cache.CacheLoader; |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 21 | import com.google.common.cache.LoadingCache; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 22 | import com.google.common.collect.Maps; |
| 23 | import io.grpc.ManagedChannel; |
| 24 | import io.grpc.ManagedChannelBuilder; |
| 25 | import io.grpc.NameResolverProvider; |
| 26 | import io.grpc.internal.DnsNameResolverProvider; |
Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 27 | import io.grpc.netty.NettyChannelBuilder; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 28 | import org.apache.felix.scr.annotations.Activate; |
| 29 | import org.apache.felix.scr.annotations.Component; |
| 30 | import org.apache.felix.scr.annotations.Deactivate; |
| 31 | import org.apache.felix.scr.annotations.Reference; |
| 32 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 33 | import org.apache.felix.scr.annotations.Service; |
| 34 | import org.onosproject.event.AbstractListenerManager; |
| 35 | import org.onosproject.grpc.api.GrpcChannelId; |
| 36 | import org.onosproject.grpc.api.GrpcController; |
| 37 | import org.onosproject.net.DeviceId; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 38 | import org.onosproject.net.device.DeviceAgentEvent; |
| 39 | import org.onosproject.net.device.DeviceAgentListener; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 40 | import org.onosproject.p4runtime.api.P4RuntimeClient; |
| 41 | import org.onosproject.p4runtime.api.P4RuntimeController; |
| 42 | import org.onosproject.p4runtime.api.P4RuntimeEvent; |
| 43 | import org.onosproject.p4runtime.api.P4RuntimeEventListener; |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 44 | import org.onosproject.store.service.StorageService; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 45 | import org.slf4j.Logger; |
| 46 | |
| 47 | import java.io.IOException; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 48 | import java.math.BigInteger; |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 49 | import java.util.List; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 50 | import java.util.Map; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 51 | import java.util.concurrent.ConcurrentMap; |
| 52 | import java.util.concurrent.CopyOnWriteArrayList; |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 53 | import java.util.concurrent.TimeUnit; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 54 | import java.util.concurrent.locks.ReadWriteLock; |
| 55 | import java.util.concurrent.locks.ReentrantReadWriteLock; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 56 | |
| 57 | import static com.google.common.base.Preconditions.checkNotNull; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 58 | import static org.slf4j.LoggerFactory.getLogger; |
| 59 | |
| 60 | /** |
| 61 | * P4Runtime controller implementation. |
| 62 | */ |
| 63 | @Component(immediate = true) |
| 64 | @Service |
| 65 | public class P4RuntimeControllerImpl |
| 66 | extends AbstractListenerManager<P4RuntimeEvent, P4RuntimeEventListener> |
| 67 | implements P4RuntimeController { |
| 68 | |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 69 | private static final int DEVICE_LOCK_EXPIRE_TIME_IN_MIN = 10; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 70 | private final Logger log = getLogger(getClass()); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 71 | private final NameResolverProvider nameResolverProvider = new DnsNameResolverProvider(); |
Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 72 | private final Map<DeviceId, ClientKey> deviceIdToClientKey = Maps.newHashMap(); |
| 73 | private final Map<ClientKey, P4RuntimeClient> clientKeyToClient = Maps.newHashMap(); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 74 | private final Map<DeviceId, GrpcChannelId> channelIds = Maps.newHashMap(); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 75 | private final ConcurrentMap<DeviceId, List<DeviceAgentListener>> deviceAgentListeners = Maps.newConcurrentMap(); |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 76 | private final LoadingCache<DeviceId, ReadWriteLock> deviceLocks = CacheBuilder.newBuilder() |
| 77 | .expireAfterAccess(DEVICE_LOCK_EXPIRE_TIME_IN_MIN, TimeUnit.MINUTES) |
| 78 | .build(new CacheLoader<DeviceId, ReadWriteLock>() { |
| 79 | @Override |
| 80 | public ReadWriteLock load(DeviceId deviceId) { |
| 81 | return new ReentrantReadWriteLock(); |
| 82 | } |
| 83 | }); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 84 | private DistributedElectionIdGenerator electionIdGenerator; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 85 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 87 | private GrpcController grpcController; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 88 | |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 90 | private StorageService storageService; |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 91 | |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 92 | @Activate |
| 93 | public void activate() { |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 94 | eventDispatcher.addSink(P4RuntimeEvent.class, listenerRegistry); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 95 | electionIdGenerator = new DistributedElectionIdGenerator(storageService); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 96 | log.info("Started"); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | @Deactivate |
| 101 | public void deactivate() { |
| 102 | grpcController = null; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 103 | electionIdGenerator.destroy(); |
| 104 | electionIdGenerator = null; |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 105 | eventDispatcher.removeSink(P4RuntimeEvent.class); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 106 | log.info("Stopped"); |
| 107 | } |
| 108 | |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 109 | @Override |
Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 110 | public boolean createClient(DeviceId deviceId, String serverAddr, |
| 111 | int serverPort, long p4DeviceId) { |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 112 | checkNotNull(deviceId); |
Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 113 | checkNotNull(serverAddr); |
| 114 | |
| 115 | ClientKey newKey = new ClientKey(deviceId, serverAddr, serverPort, p4DeviceId); |
| 116 | |
| 117 | ManagedChannelBuilder channelBuilder = NettyChannelBuilder |
| 118 | .forAddress(serverAddr, serverPort) |
| 119 | .usePlaintext(true); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 120 | |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 121 | deviceLocks.getUnchecked(deviceId).writeLock().lock(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 122 | |
| 123 | try { |
Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 124 | if (deviceIdToClientKey.containsKey(deviceId)) { |
| 125 | final ClientKey existingKey = deviceIdToClientKey.get(deviceId); |
| 126 | if (newKey.equals(existingKey)) { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 127 | log.info("Not creating client for {} as it already exists (server={}:{}, p4DeviceId={})...", |
| 128 | deviceId, serverAddr, serverPort, p4DeviceId); |
Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 129 | return true; |
| 130 | } else { |
| 131 | throw new IllegalStateException( |
| 132 | "A client for the same device ID but different " + |
| 133 | "server endpoints already exists"); |
| 134 | } |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 135 | } else { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 136 | log.info("Creating client for {} (server={}:{}, p4DeviceId={})...", |
| 137 | deviceId, serverAddr, serverPort, p4DeviceId); |
Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 138 | return doCreateClient(newKey, channelBuilder); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 139 | } |
| 140 | } finally { |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 141 | deviceLocks.getUnchecked(deviceId).writeLock().unlock(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 145 | private boolean doCreateClient(ClientKey clientKey, ManagedChannelBuilder channelBuilder) { |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 146 | |
Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 147 | GrpcChannelId channelId = GrpcChannelId.of(clientKey.deviceId(), |
| 148 | "p4runtime-" + clientKey.p4DeviceId()); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 149 | |
| 150 | // Channel defaults. |
| 151 | channelBuilder.nameResolverFactory(nameResolverProvider); |
| 152 | |
| 153 | ManagedChannel channel; |
| 154 | try { |
| 155 | channel = grpcController.connectChannel(channelId, channelBuilder); |
| 156 | } catch (IOException e) { |
Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 157 | log.warn("Unable to connect to gRPC server of {}: {}", |
| 158 | clientKey.deviceId(), e.getMessage()); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 159 | return false; |
| 160 | } |
| 161 | |
Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 162 | P4RuntimeClient client = new P4RuntimeClientImpl( |
| 163 | clientKey.deviceId(), clientKey.p4DeviceId(), channel, this); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 164 | |
Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 165 | channelIds.put(clientKey.deviceId(), channelId); |
| 166 | deviceIdToClientKey.put(clientKey.deviceId(), clientKey); |
| 167 | clientKeyToClient.put(clientKey, client); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 168 | |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public P4RuntimeClient getClient(DeviceId deviceId) { |
| 174 | |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 175 | deviceLocks.getUnchecked(deviceId).readLock().lock(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 176 | |
| 177 | try { |
Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 178 | if (!deviceIdToClientKey.containsKey(deviceId)) { |
| 179 | return null; |
| 180 | } else { |
| 181 | return clientKeyToClient.get(deviceIdToClientKey.get(deviceId)); |
| 182 | } |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 183 | } finally { |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 184 | deviceLocks.getUnchecked(deviceId).readLock().unlock(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public void removeClient(DeviceId deviceId) { |
| 190 | |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 191 | deviceLocks.getUnchecked(deviceId).writeLock().lock(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 192 | try { |
Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 193 | if (deviceIdToClientKey.containsKey(deviceId)) { |
| 194 | final ClientKey clientKey = deviceIdToClientKey.get(deviceId); |
Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 195 | clientKeyToClient.remove(clientKey).shutdown(); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 196 | grpcController.disconnectChannel(channelIds.get(deviceId)); |
Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 197 | deviceIdToClientKey.remove(deviceId); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 198 | channelIds.remove(deviceId); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 199 | } |
| 200 | } finally { |
Andrea Campanella | a74bdba | 2018-05-15 16:45:00 +0200 | [diff] [blame] | 201 | deviceLocks.getUnchecked(deviceId).writeLock().unlock(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public boolean hasClient(DeviceId deviceId) { |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 207 | deviceLocks.getUnchecked(deviceId).readLock().lock(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 208 | |
| 209 | try { |
Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 210 | return deviceIdToClientKey.containsKey(deviceId); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 211 | } finally { |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 212 | deviceLocks.getUnchecked(deviceId).readLock().unlock(); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public boolean isReacheable(DeviceId deviceId) { |
| 218 | |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 219 | deviceLocks.getUnchecked(deviceId).readLock().lock(); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 220 | |
| 221 | try { |
Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 222 | if (!deviceIdToClientKey.containsKey(deviceId)) { |
| 223 | log.debug("No client for {}, can't check for reachability", deviceId); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 224 | return false; |
| 225 | } |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 226 | // FIXME: we're not checking for a P4Runtime server, it could be any gRPC service |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 227 | return grpcController.isChannelOpen(channelIds.get(deviceId)); |
| 228 | } finally { |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 229 | deviceLocks.getUnchecked(deviceId).readLock().unlock(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 233 | @Override |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 234 | public void addDeviceAgentListener(DeviceId deviceId, DeviceAgentListener listener) { |
| 235 | deviceAgentListeners.putIfAbsent(deviceId, new CopyOnWriteArrayList<>()); |
| 236 | deviceAgentListeners.get(deviceId).add(listener); |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 239 | @Override |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 240 | public void removeDeviceAgentListener(DeviceId deviceId, DeviceAgentListener listener) { |
| 241 | deviceAgentListeners.computeIfPresent(deviceId, (did, listeners) -> { |
| 242 | listeners.remove(listener); |
| 243 | return listeners; |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 244 | }); |
| 245 | } |
| 246 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 247 | BigInteger newMasterElectionId(DeviceId deviceId) { |
| 248 | return electionIdGenerator.generate(deviceId); |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 249 | } |
| 250 | |
Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 251 | void postEvent(P4RuntimeEvent event) { |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 252 | switch (event.type()) { |
| 253 | case CHANNEL_EVENT: |
| 254 | handleChannelEvent(event); |
| 255 | break; |
| 256 | case ARBITRATION_RESPONSE: |
| 257 | handleArbitrationReply(event); |
| 258 | break; |
| 259 | default: |
| 260 | post(event); |
| 261 | break; |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 262 | } |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 263 | } |
Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 264 | |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 265 | private void handleChannelEvent(P4RuntimeEvent event) { |
| 266 | final ChannelEvent channelEvent = (ChannelEvent) event.subject(); |
| 267 | final DeviceId deviceId = channelEvent.deviceId(); |
| 268 | final DeviceAgentEvent.Type agentEventType; |
| 269 | switch (channelEvent.type()) { |
| 270 | case OPEN: |
| 271 | agentEventType = DeviceAgentEvent.Type.CHANNEL_OPEN; |
| 272 | break; |
| 273 | case CLOSED: |
| 274 | agentEventType = DeviceAgentEvent.Type.CHANNEL_CLOSED; |
| 275 | break; |
| 276 | case ERROR: |
| 277 | agentEventType = !isReacheable(deviceId) |
| 278 | ? DeviceAgentEvent.Type.CHANNEL_CLOSED |
| 279 | : DeviceAgentEvent.Type.CHANNEL_ERROR; |
| 280 | break; |
| 281 | default: |
| 282 | log.warn("Unrecognized channel event type {}", channelEvent.type()); |
| 283 | return; |
| 284 | } |
| 285 | postDeviceAgentEvent(deviceId, new DeviceAgentEvent(agentEventType, deviceId)); |
| 286 | } |
| 287 | |
| 288 | private void handleArbitrationReply(P4RuntimeEvent event) { |
| 289 | final DeviceId deviceId = event.subject().deviceId(); |
| 290 | final ArbitrationResponse response = (ArbitrationResponse) event.subject(); |
| 291 | final DeviceAgentEvent.Type roleType = response.isMaster() |
| 292 | ? DeviceAgentEvent.Type.ROLE_MASTER |
| 293 | : DeviceAgentEvent.Type.ROLE_STANDBY; |
| 294 | postDeviceAgentEvent(deviceId, new DeviceAgentEvent( |
| 295 | roleType, response.deviceId())); |
| 296 | } |
| 297 | |
| 298 | private void postDeviceAgentEvent(DeviceId deviceId, DeviceAgentEvent event) { |
| 299 | if (deviceAgentListeners.containsKey(deviceId)) { |
| 300 | deviceAgentListeners.get(deviceId).forEach(l -> l.event(event)); |
| 301 | } |
| 302 | } |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 303 | } |