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; |
| 27 | import org.apache.felix.scr.annotations.Activate; |
| 28 | import org.apache.felix.scr.annotations.Component; |
| 29 | import org.apache.felix.scr.annotations.Deactivate; |
| 30 | import org.apache.felix.scr.annotations.Reference; |
| 31 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 32 | import org.apache.felix.scr.annotations.Service; |
| 33 | import org.onosproject.event.AbstractListenerManager; |
| 34 | import org.onosproject.grpc.api.GrpcChannelId; |
| 35 | import org.onosproject.grpc.api.GrpcController; |
| 36 | import org.onosproject.net.DeviceId; |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 37 | import org.onosproject.net.device.ChannelEvent; |
| 38 | import org.onosproject.net.device.ChannelListener; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 39 | import org.onosproject.p4runtime.api.P4RuntimeClient; |
| 40 | import org.onosproject.p4runtime.api.P4RuntimeController; |
| 41 | import org.onosproject.p4runtime.api.P4RuntimeEvent; |
| 42 | import org.onosproject.p4runtime.api.P4RuntimeEventListener; |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 43 | import org.onosproject.store.service.AtomicCounter; |
| 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; |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 48 | import java.util.ArrayList; |
| 49 | import java.util.List; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 50 | import java.util.Map; |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 51 | import java.util.concurrent.TimeUnit; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 52 | import java.util.concurrent.locks.ReadWriteLock; |
| 53 | import java.util.concurrent.locks.ReentrantReadWriteLock; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 54 | |
| 55 | import static com.google.common.base.Preconditions.checkNotNull; |
| 56 | import static java.lang.String.format; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 57 | import static org.slf4j.LoggerFactory.getLogger; |
| 58 | |
| 59 | /** |
| 60 | * P4Runtime controller implementation. |
| 61 | */ |
| 62 | @Component(immediate = true) |
| 63 | @Service |
| 64 | public class P4RuntimeControllerImpl |
| 65 | extends AbstractListenerManager<P4RuntimeEvent, P4RuntimeEventListener> |
| 66 | implements P4RuntimeController { |
| 67 | |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 68 | private static final String P4R_ELECTION = "p4runtime-election"; |
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 | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 72 | private final Map<DeviceId, P4RuntimeClient> clients = Maps.newHashMap(); |
| 73 | private final Map<DeviceId, GrpcChannelId> channelIds = Maps.newHashMap(); |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 74 | private final Map<DeviceId, List<ChannelListener>> channelListeners = Maps.newConcurrentMap(); |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 75 | private final LoadingCache<DeviceId, ReadWriteLock> deviceLocks = CacheBuilder.newBuilder() |
| 76 | .expireAfterAccess(DEVICE_LOCK_EXPIRE_TIME_IN_MIN, TimeUnit.MINUTES) |
| 77 | .build(new CacheLoader<DeviceId, ReadWriteLock>() { |
| 78 | @Override |
| 79 | public ReadWriteLock load(DeviceId deviceId) { |
| 80 | return new ReentrantReadWriteLock(); |
| 81 | } |
| 82 | }); |
| 83 | |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 84 | private AtomicCounter 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 | c8e8498 | 2017-07-26 15:34:42 -0400 | [diff] [blame] | 87 | public 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) |
| 90 | public StorageService storageService; |
| 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); |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 95 | electionIdGenerator = storageService.getAtomicCounter(P4R_ELECTION); |
| 96 | |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 97 | log.info("Started"); |
| 98 | } |
| 99 | |
| 100 | |
| 101 | @Deactivate |
| 102 | public void deactivate() { |
| 103 | grpcController = null; |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 104 | eventDispatcher.removeSink(P4RuntimeEvent.class); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 105 | log.info("Stopped"); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | @Override |
Carmelo Cascone | f423bec | 2017-08-30 01:56:25 +0200 | [diff] [blame] | 110 | public boolean createClient(DeviceId deviceId, long p4DeviceId, ManagedChannelBuilder channelBuilder) { |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 111 | checkNotNull(deviceId); |
| 112 | checkNotNull(channelBuilder); |
| 113 | |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 114 | deviceLocks.getUnchecked(deviceId).writeLock().lock(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 115 | log.info("Creating client for {} (with internal device id {})...", deviceId, p4DeviceId); |
| 116 | |
| 117 | try { |
| 118 | if (clients.containsKey(deviceId)) { |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 119 | // TODO might want to consider a more fine-grained check such as same port/p4DeviceId |
| 120 | log.warn("A client already exists for {}", deviceId); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 121 | throw new IllegalStateException(format("A client already exists for %s", deviceId)); |
| 122 | } else { |
| 123 | return doCreateClient(deviceId, p4DeviceId, channelBuilder); |
| 124 | } |
| 125 | } finally { |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 126 | deviceLocks.getUnchecked(deviceId).writeLock().unlock(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
Carmelo Cascone | f423bec | 2017-08-30 01:56:25 +0200 | [diff] [blame] | 130 | private boolean doCreateClient(DeviceId deviceId, long p4DeviceId, ManagedChannelBuilder channelBuilder) { |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 131 | |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 132 | GrpcChannelId channelId = GrpcChannelId.of(deviceId, "p4runtime"); |
| 133 | |
| 134 | // Channel defaults. |
| 135 | channelBuilder.nameResolverFactory(nameResolverProvider); |
| 136 | |
| 137 | ManagedChannel channel; |
| 138 | try { |
| 139 | channel = grpcController.connectChannel(channelId, channelBuilder); |
| 140 | } catch (IOException e) { |
| 141 | log.warn("Unable to connect to gRPC server of {}: {}", deviceId, e.getMessage()); |
| 142 | return false; |
| 143 | } |
| 144 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 145 | P4RuntimeClient client = new P4RuntimeClientImpl(deviceId, p4DeviceId, channel, this); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 146 | |
| 147 | channelIds.put(deviceId, channelId); |
| 148 | clients.put(deviceId, client); |
| 149 | |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public P4RuntimeClient getClient(DeviceId deviceId) { |
| 155 | |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 156 | deviceLocks.getUnchecked(deviceId).readLock().lock(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 157 | |
| 158 | try { |
| 159 | return clients.get(deviceId); |
| 160 | } finally { |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 161 | deviceLocks.getUnchecked(deviceId).readLock().unlock(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public void removeClient(DeviceId deviceId) { |
| 167 | |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 168 | deviceLocks.getUnchecked(deviceId).writeLock().lock(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 169 | |
| 170 | try { |
| 171 | if (clients.containsKey(deviceId)) { |
| 172 | clients.get(deviceId).shutdown(); |
| 173 | grpcController.disconnectChannel(channelIds.get(deviceId)); |
| 174 | clients.remove(deviceId); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 175 | channelIds.remove(deviceId); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 176 | } |
| 177 | } finally { |
Andrea Campanella | a74bdba | 2018-05-15 16:45:00 +0200 | [diff] [blame] | 178 | deviceLocks.getUnchecked(deviceId).writeLock().unlock(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public boolean hasClient(DeviceId deviceId) { |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 184 | deviceLocks.getUnchecked(deviceId).readLock().lock(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 185 | |
| 186 | try { |
| 187 | return clients.containsKey(deviceId); |
| 188 | } finally { |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 189 | deviceLocks.getUnchecked(deviceId).readLock().unlock(); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public boolean isReacheable(DeviceId deviceId) { |
| 195 | |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 196 | deviceLocks.getUnchecked(deviceId).readLock().lock(); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 197 | |
| 198 | try { |
| 199 | if (!clients.containsKey(deviceId)) { |
| 200 | log.warn("No client for {}, can't check for reachability", deviceId); |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | return grpcController.isChannelOpen(channelIds.get(deviceId)); |
| 205 | } finally { |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 206 | deviceLocks.getUnchecked(deviceId).readLock().unlock(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 210 | @Override |
| 211 | public long getNewMasterElectionId() { |
| 212 | return electionIdGenerator.incrementAndGet(); |
| 213 | } |
| 214 | |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 215 | @Override |
| 216 | public void addChannelListener(DeviceId deviceId, ChannelListener listener) { |
| 217 | channelListeners.compute(deviceId, (devId, listeners) -> { |
| 218 | List<ChannelListener> newListeners; |
| 219 | if (listeners != null) { |
| 220 | newListeners = listeners; |
| 221 | } else { |
| 222 | newListeners = new ArrayList<>(); |
| 223 | } |
| 224 | newListeners.add(listener); |
| 225 | return newListeners; |
| 226 | }); |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public void removeChannelListener(DeviceId deviceId, ChannelListener listener) { |
| 231 | channelListeners.compute(deviceId, (devId, listeners) -> { |
| 232 | if (listeners != null) { |
| 233 | listeners.remove(listener); |
| 234 | return listeners; |
| 235 | } else { |
| 236 | log.debug("Device {} has no listener registered", deviceId); |
| 237 | return null; |
| 238 | } |
| 239 | }); |
| 240 | } |
| 241 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 242 | public void postEvent(P4RuntimeEvent event) { |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame^] | 243 | if (event.type().equals(P4RuntimeEvent.Type.CHANNEL_EVENT)) { |
| 244 | DefaultChannelEvent channelError = (DefaultChannelEvent) event.subject(); |
| 245 | DeviceId deviceId = event.subject().deviceId(); |
| 246 | ChannelEvent channelEvent = null; |
| 247 | //If disconnection is already known we propagate it. |
| 248 | if (channelError.type().equals(ChannelEvent.Type.CHANNEL_DISCONNECTED)) { |
| 249 | channelEvent = new ChannelEvent(ChannelEvent.Type.CHANNEL_DISCONNECTED, channelError.deviceId(), |
| 250 | channelError.throwable()); |
| 251 | } else if (channelError.type().equals(ChannelEvent.Type.CHANNEL_ERROR)) { |
| 252 | //If we don't know what the error is we check for reachability |
| 253 | if (!isReacheable(deviceId)) { |
| 254 | //if false the channel has disconnected |
| 255 | channelEvent = new ChannelEvent(ChannelEvent.Type.CHANNEL_DISCONNECTED, channelError.deviceId(), |
| 256 | channelError.throwable()); |
| 257 | } else { |
| 258 | // else we propagate the event. |
| 259 | channelEvent = new ChannelEvent(ChannelEvent.Type.CHANNEL_ERROR, channelError.deviceId(), |
| 260 | channelError.throwable()); |
| 261 | } |
| 262 | } |
| 263 | //Ignoring CHANNEL_CONNECTED |
| 264 | if (channelEvent != null && channelListeners.get(deviceId) != null) { |
| 265 | for (ChannelListener listener : channelListeners.get(deviceId)) { |
| 266 | listener.event(channelEvent); |
| 267 | } |
| 268 | } |
| 269 | } else { |
| 270 | post(event); |
| 271 | } |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 272 | } |
| 273 | } |