blob: be4b3296a891bef555aa44f49e8e3dc749647f25 [file] [log] [blame]
Carmelo Cascone9e4972c2018-08-30 00:29:16 -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.net.pi.impl;
18
19import com.google.common.collect.Maps;
Carmelo Cascone3977ea42019-02-28 13:43:42 -080020import com.google.common.util.concurrent.Futures;
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070021import com.google.common.util.concurrent.Striped;
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070022import org.onlab.util.KryoNamespace;
23import org.onlab.util.Tools;
24import org.onosproject.cfg.ComponentConfigService;
25import org.onosproject.event.AbstractListenerManager;
26import org.onosproject.mastership.MastershipInfo;
27import org.onosproject.mastership.MastershipService;
28import org.onosproject.net.Device;
29import org.onosproject.net.DeviceId;
30import org.onosproject.net.MastershipRole;
31import org.onosproject.net.behaviour.PiPipelineProgrammable;
32import org.onosproject.net.device.DeviceEvent;
33import org.onosproject.net.device.DeviceHandshaker;
34import org.onosproject.net.device.DeviceListener;
35import org.onosproject.net.device.DeviceService;
36import org.onosproject.net.pi.model.PiPipeconf;
37import org.onosproject.net.pi.model.PiPipeconfId;
Carmelo Cascone75a9a892019-04-22 12:12:23 -070038import org.onosproject.net.pi.service.PiPipeconfEvent;
39import org.onosproject.net.pi.service.PiPipeconfListener;
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070040import org.onosproject.net.pi.service.PiPipeconfMappingStore;
41import org.onosproject.net.pi.service.PiPipeconfService;
42import org.onosproject.net.pi.service.PiPipeconfWatchdogEvent;
43import org.onosproject.net.pi.service.PiPipeconfWatchdogListener;
44import org.onosproject.net.pi.service.PiPipeconfWatchdogService;
pierventrece191fe2021-03-22 22:17:21 +010045import org.onosproject.store.primitives.DefaultDistributedSet;
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070046import org.onosproject.store.serializers.KryoNamespaces;
pierventrece191fe2021-03-22 22:17:21 +010047import org.onosproject.store.service.DistributedPrimitive;
48import org.onosproject.store.service.DistributedSet;
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070049import org.onosproject.store.service.EventuallyConsistentMap;
50import org.onosproject.store.service.EventuallyConsistentMapEvent;
51import org.onosproject.store.service.EventuallyConsistentMapListener;
pierventrece191fe2021-03-22 22:17:21 +010052import org.onosproject.store.service.Serializer;
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070053import org.onosproject.store.service.StorageService;
54import org.onosproject.store.service.WallClockTimestamp;
55import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070056import org.osgi.service.component.annotations.Activate;
57import org.osgi.service.component.annotations.Component;
58import org.osgi.service.component.annotations.Deactivate;
59import org.osgi.service.component.annotations.Modified;
60import org.osgi.service.component.annotations.Reference;
61import org.osgi.service.component.annotations.ReferenceCardinality;
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070062import org.slf4j.Logger;
63
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070064import java.util.Dictionary;
65import java.util.Map;
Carmelo Cascone75a9a892019-04-22 12:12:23 -070066import java.util.Objects;
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070067import java.util.concurrent.ExecutorService;
68import java.util.concurrent.Executors;
pierventree1f80102021-10-01 22:01:22 +020069import java.util.concurrent.ScheduledExecutorService;
70import java.util.concurrent.ScheduledFuture;
71import java.util.concurrent.TimeUnit;
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070072import java.util.concurrent.locks.Lock;
73
Carmelo Cascone95308282019-03-18 17:18:04 -070074import static java.util.Collections.singleton;
pierventree1f80102021-10-01 22:01:22 +020075import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070076import static org.onlab.util.Tools.groupedThreads;
Ray Milkeyd04e2272018-10-16 18:20:18 -070077import static org.onosproject.net.OsgiPropertyConstants.PWM_PROBE_INTERVAL;
78import static org.onosproject.net.OsgiPropertyConstants.PWM_PROBE_INTERVAL_DEFAULT;
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070079import static org.slf4j.LoggerFactory.getLogger;
80
81/**
82 * Implementation of PiPipeconfWatchdogService that implements a periodic
83 * pipeline probe task and listens for device events to update the status of the
84 * pipeline.
85 */
Ray Milkeyd04e2272018-10-16 18:20:18 -070086@Component(
Carmelo Cascone75a9a892019-04-22 12:12:23 -070087 immediate = true,
88 service = PiPipeconfWatchdogService.class,
89 property = {
90 PWM_PROBE_INTERVAL + ":Integer=" + PWM_PROBE_INTERVAL_DEFAULT
91 }
Ray Milkeyd04e2272018-10-16 18:20:18 -070092)
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070093public class PiPipeconfWatchdogManager
94 extends AbstractListenerManager<PiPipeconfWatchdogEvent, PiPipeconfWatchdogListener>
95 implements PiPipeconfWatchdogService {
96
97 private final Logger log = getLogger(getClass());
98
Ray Milkeyd84f89b2018-08-17 14:54:17 -070099 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700100 private PiPipeconfMappingStore pipeconfMappingStore;
101
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700102 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700103 private DeviceService deviceService;
104
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700105 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700106 private MastershipService mastershipService;
107
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700108 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700109 protected PiPipeconfService pipeconfService;
110
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700111 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700112 protected StorageService storageService;
113
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700114 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700115 private ComponentConfigService componentConfigService;
116
Carmelo Cascone75a9a892019-04-22 12:12:23 -0700117 /**
118 * Configure interval in seconds for device pipeconf probing.
119 */
Ray Milkeyd04e2272018-10-16 18:20:18 -0700120 private int probeInterval = PWM_PROBE_INTERVAL_DEFAULT;
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700121
122 protected ExecutorService executor = Executors.newFixedThreadPool(
123 30, groupedThreads("onos/pipeconf-watchdog", "%d", log));
124
Carmelo Cascone75a9a892019-04-22 12:12:23 -0700125 private final DeviceListener deviceListener = new InternalDeviceListener();
126 private final PiPipeconfListener pipeconfListener = new InternalPipeconfListener();
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700127
pierventree1f80102021-10-01 22:01:22 +0200128 private ScheduledExecutorService eventExecutor = newSingleThreadScheduledExecutor(
129 groupedThreads("onos/pipeconf-event", "%d", log));
130 private ScheduledFuture<?> poller = null;
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700131
132 private final Striped<Lock> locks = Striped.lock(30);
133
134 private EventuallyConsistentMap<DeviceId, PipelineStatus> statusMap;
135 private Map<DeviceId, PipelineStatus> localStatusMap;
136
pierventrece191fe2021-03-22 22:17:21 +0100137 // Configured devices by this cluster. We use a set to keep track of all devices for which
138 // we have pushed the forwarding pipeline config at least once. This guarantees that device
139 // pipelines are wiped out/reset at least once when starting the cluster, minimizing the risk
140 // of any stale state from previous runs affecting control operations. Another effect of this
141 // approach is that the default entries mirror will get populated even though the pipeline results
142 // to be the same across different ONOS installations.
143 private static final String CONFIGURED_DEVICES = "onos-pipeconf-configured-set";
144 private DistributedSet<DeviceId> configuredDevices;
145
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700146 @Activate
147 public void activate() {
148 eventDispatcher.addSink(PiPipeconfWatchdogEvent.class, listenerRegistry);
149 localStatusMap = Maps.newConcurrentMap();
pierventrece191fe2021-03-22 22:17:21 +0100150 // Init distributed status map and configured devices set
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700151 KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
152 .register(KryoNamespaces.API)
153 .register(PipelineStatus.class);
154 statusMap = storageService.<DeviceId, PipelineStatus>eventuallyConsistentMapBuilder()
155 .withName("onos-pipeconf-status-table")
156 .withSerializer(serializer)
157 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
158 statusMap.addListener(new StatusMapListener());
pierventrece191fe2021-03-22 22:17:21 +0100159 // Init the set of the configured devices
160 configuredDevices = new DefaultDistributedSet<>(storageService.<DeviceId>setBuilder()
161 .withName(CONFIGURED_DEVICES)
162 .withSerializer(Serializer.using(KryoNamespaces.API))
163 .build(),
164 DistributedPrimitive.DEFAULT_OPERATION_TIMEOUT_MILLIS);
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700165 // Register component configurable properties.
166 componentConfigService.registerProperties(getClass());
167 // Start periodic watchdog task.
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700168 startProbeTask();
Carmelo Cascone75a9a892019-04-22 12:12:23 -0700169 // Add listeners.
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700170 deviceService.addListener(deviceListener);
Carmelo Cascone75a9a892019-04-22 12:12:23 -0700171 pipeconfService.addListener(pipeconfListener);
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700172 log.info("Started");
173 }
174
175 @Modified
176 public void modified(ComponentContext context) {
177 if (context == null) {
178 return;
179 }
180
181 Dictionary<?, ?> properties = context.getProperties();
182 final int oldProbeInterval = probeInterval;
183 probeInterval = Tools.getIntegerProperty(
Ray Milkeyd04e2272018-10-16 18:20:18 -0700184 properties, PWM_PROBE_INTERVAL, PWM_PROBE_INTERVAL_DEFAULT);
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700185 log.info("Configured. {} is configured to {} seconds",
Ray Milkeyd04e2272018-10-16 18:20:18 -0700186 PWM_PROBE_INTERVAL_DEFAULT, probeInterval);
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700187
188 if (oldProbeInterval != probeInterval) {
189 rescheduleProbeTask();
190 }
191 }
192
193 @Deactivate
194 public void deactivate() {
195 eventDispatcher.removeSink(PiPipeconfWatchdogEvent.class);
Carmelo Cascone75a9a892019-04-22 12:12:23 -0700196 pipeconfService.removeListener(pipeconfListener);
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700197 deviceService.removeListener(deviceListener);
198 stopProbeTask();
pierventree1f80102021-10-01 22:01:22 +0200199 eventExecutor.shutdown();
200 executor.shutdown();
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700201 statusMap = null;
202 localStatusMap = null;
203 log.info("Stopped");
204 }
205
206 @Override
207 public void triggerProbe(DeviceId deviceId) {
208 final Device device = deviceService.getDevice(deviceId);
209 if (device != null) {
Carmelo Cascone95308282019-03-18 17:18:04 -0700210 filterAndTriggerTasks(singleton(device));
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700211 }
212 }
213
214 @Override
215 public PipelineStatus getStatus(DeviceId deviceId) {
216 final PipelineStatus status = statusMap.get(deviceId);
217 return status == null ? PipelineStatus.UNKNOWN : status;
218 }
219
220 private void triggerCheckAllDevices() {
221 filterAndTriggerTasks(deviceService.getDevices());
222 }
223
224 private void filterAndTriggerTasks(Iterable<Device> devices) {
225 devices.forEach(device -> {
226 if (!isLocalMaster(device)) {
227 return;
228 }
229
230 final PiPipeconfId pipeconfId = pipeconfMappingStore.getPipeconfId(device.id());
Carmelo Cascone95308282019-03-18 17:18:04 -0700231 if (pipeconfId == null || !device.is(PiPipelineProgrammable.class)) {
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700232 return;
233 }
234
235 if (!pipeconfService.getPipeconf(pipeconfId).isPresent()) {
Carmelo Cascone75a9a892019-04-22 12:12:23 -0700236 log.warn("Pipeconf {} is not registered, skipping probe for {}",
237 pipeconfId, device.id());
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700238 return;
239 }
240
241 final PiPipeconf pipeconf = pipeconfService.getPipeconf(pipeconfId).get();
242
243 if (!device.is(DeviceHandshaker.class)) {
244 log.error("Missing DeviceHandshaker behavior for {}", device.id());
245 return;
246 }
247
248 // Trigger task with per-device lock.
249 executor.execute(withLock(() -> {
250 final boolean success = doSetPipeconfIfRequired(device, pipeconf);
251 if (success) {
252 signalStatusReady(device.id());
pierventrece191fe2021-03-22 22:17:21 +0100253 signalStatusConfigured(device.id());
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700254 } else {
255 signalStatusUnknown(device.id());
256 }
257 }, device.id()));
258 });
259 }
260
261 /**
262 * Returns true if the given device is known to be configured with the given
263 * pipeline, false otherwise. If necessary, this method enforces setting the
264 * given pipeconf using drivers.
265 *
266 * @param device device
267 * @param pipeconf pipeconf
268 * @return boolean
269 */
270 private boolean doSetPipeconfIfRequired(Device device, PiPipeconf pipeconf) {
271 log.debug("Starting watchdog task for {} ({})", device.id(), pipeconf.id());
272 final PiPipelineProgrammable pipelineProg = device.as(PiPipelineProgrammable.class);
273 final DeviceHandshaker handshaker = device.as(DeviceHandshaker.class);
Carmelo Casconec2be50a2019-04-10 00:15:39 -0700274 if (!handshaker.hasConnection()) {
pierventre03450cb2021-05-18 18:06:40 +0200275 log.warn("There is no connectivity with {}", device.id());
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700276 return false;
277 }
pierventrece191fe2021-03-22 22:17:21 +0100278 if (Futures.getUnchecked(pipelineProg.isPipeconfSet(pipeconf)) &&
279 configuredDevices.contains(device.id())) {
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700280 log.debug("Pipeconf {} already configured on {}",
281 pipeconf.id(), device.id());
282 return true;
283 }
Carmelo Cascone95308282019-03-18 17:18:04 -0700284 return Futures.getUnchecked(pipelineProg.setPipeconf(pipeconf));
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700285 }
286
287 private Runnable withLock(Runnable task, Object object) {
288 return () -> {
289 final Lock lock = locks.get(object);
290 lock.lock();
291 try {
292 task.run();
293 } finally {
294 lock.unlock();
295 }
296 };
297 }
298
299 private void signalStatusUnknown(DeviceId deviceId) {
300 statusMap.remove(deviceId);
301 }
302
303 private void signalStatusReady(DeviceId deviceId) {
304 statusMap.put(deviceId, PipelineStatus.READY);
305 }
306
pierventrece191fe2021-03-22 22:17:21 +0100307 private void signalStatusUnconfigured(DeviceId deviceId) {
308 configuredDevices.remove(deviceId);
309 }
310
311 private void signalStatusConfigured(DeviceId deviceId) {
312 configuredDevices.add(deviceId);
313 }
314
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700315 private boolean isLocalMaster(Device device) {
316 if (mastershipService.isLocalMaster(device.id())) {
317 return true;
318 }
319 // The device might have no master (e.g. after it has been disconnected
320 // from core), hence we use device mastership state.
321 final MastershipInfo info = mastershipService.getMastershipFor(device.id());
322 return !info.master().isPresent() &&
323 device.is(DeviceHandshaker.class) &&
324 device.as(DeviceHandshaker.class).getRole()
325 .equals(MastershipRole.MASTER);
326 }
327
328 private void startProbeTask() {
Carmelo Cascone95308282019-03-18 17:18:04 -0700329 synchronized (this) {
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700330 log.info("Starting pipeline probe thread with {} seconds interval...", probeInterval);
pierventree1f80102021-10-01 22:01:22 +0200331 poller = eventExecutor.scheduleAtFixedRate(this::triggerCheckAllDevices, probeInterval,
332 probeInterval, TimeUnit.SECONDS);
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700333 }
334 }
335
336
337 private void stopProbeTask() {
Carmelo Cascone95308282019-03-18 17:18:04 -0700338 synchronized (this) {
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700339 log.info("Stopping pipeline probe thread...");
pierventree1f80102021-10-01 22:01:22 +0200340 poller.cancel(false);
341 poller = null;
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700342 }
343 }
344
345
346 private synchronized void rescheduleProbeTask() {
Carmelo Cascone95308282019-03-18 17:18:04 -0700347 synchronized (this) {
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700348 stopProbeTask();
349 startProbeTask();
350 }
351 }
352
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700353 /**
354 * Listener of device events used to update the pipeline status.
355 */
356 private class InternalDeviceListener implements DeviceListener {
357
358 @Override
359 public void event(DeviceEvent event) {
pierventree1f80102021-10-01 22:01:22 +0200360 eventExecutor.execute(() -> {
361 final Device device = event.subject();
362 switch (event.type()) {
363 case DEVICE_ADDED:
364 case DEVICE_UPDATED:
365 case DEVICE_AVAILABILITY_CHANGED:
366 /*
367 * The GeneralDeviceProvider marks online/offline devices that
368 * have/have not ANY pipeline config set. Here we make sure the
369 * one configured in the pipeconf service is the expected one.
370 * Clearly, it would be better to let the GDP do this check and
371 * avoid sending twice the same message to the switch.
372 */
373 if (!deviceService.isAvailable(device.id())) {
374 signalStatusUnknown(device.id());
375 } else {
376 filterAndTriggerTasks(singleton(device));
377 }
378 break;
379 case DEVICE_REMOVED:
380 case DEVICE_SUSPENDED:
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700381 signalStatusUnknown(device.id());
pierventree1f80102021-10-01 22:01:22 +0200382 signalStatusUnconfigured(device.id());
383 break;
384 case PORT_ADDED:
385 case PORT_UPDATED:
386 case PORT_REMOVED:
387 case PORT_STATS_UPDATED:
388 default:
389 break;
390 }
391 });
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700392 }
393 }
394
Carmelo Cascone75a9a892019-04-22 12:12:23 -0700395 private class InternalPipeconfListener implements PiPipeconfListener {
396 @Override
397 public void event(PiPipeconfEvent event) {
pierventree1f80102021-10-01 22:01:22 +0200398 eventExecutor.execute(() -> {
399 if (Objects.equals(event.type(), PiPipeconfEvent.Type.REGISTERED)) {
400 pipeconfMappingStore.getDevices(event.subject())
401 .forEach(PiPipeconfWatchdogManager.this::triggerProbe);
402 }
403 });
Carmelo Cascone75a9a892019-04-22 12:12:23 -0700404 }
405 }
406
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700407 private class StatusMapListener
408 implements EventuallyConsistentMapListener<DeviceId, PipelineStatus> {
409
410 @Override
411 public void event(EventuallyConsistentMapEvent<DeviceId, PipelineStatus> event) {
412 final DeviceId deviceId = event.key();
413 final PipelineStatus status = event.value();
414 switch (event.type()) {
415 case PUT:
416 postStatusEvent(deviceId, status);
417 break;
418 case REMOVE:
419 postStatusEvent(deviceId, PipelineStatus.UNKNOWN);
420 break;
421 default:
422 log.error("Unknown map event type {}", event.type());
423 }
424 }
425
426 private void postStatusEvent(DeviceId deviceId, PipelineStatus newStatus) {
427 PipelineStatus oldStatus = localStatusMap.put(deviceId, newStatus);
428 oldStatus = oldStatus == null ? PipelineStatus.UNKNOWN : oldStatus;
429 final PiPipeconfWatchdogEvent.Type eventType =
430 newStatus == PipelineStatus.READY
431 ? PiPipeconfWatchdogEvent.Type.PIPELINE_READY
432 : PiPipeconfWatchdogEvent.Type.PIPELINE_UNKNOWN;
433 if (newStatus != oldStatus) {
434 log.info("Pipeline status of {} is {}", deviceId, newStatus);
435 post(new PiPipeconfWatchdogEvent(eventType, deviceId));
436 }
437 }
438 }
439}