Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015-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 | package org.onosproject.inbandtelemetry.impl; |
| 17 | |
| 18 | import com.google.common.collect.Maps; |
| 19 | import com.google.common.util.concurrent.Striped; |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 20 | import org.onlab.util.KryoNamespace; |
| 21 | import org.onlab.util.SharedScheduledExecutors; |
| 22 | import org.onosproject.core.ApplicationId; |
| 23 | import org.onosproject.core.CoreService; |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 24 | import org.onosproject.net.behaviour.inbandtelemetry.IntReportConfig; |
Carmelo Cascone | b330fc7 | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 25 | import org.onosproject.net.behaviour.inbandtelemetry.IntMetadataType; |
| 26 | import org.onosproject.net.behaviour.inbandtelemetry.IntDeviceConfig; |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 27 | import org.onosproject.inbandtelemetry.api.IntIntent; |
| 28 | import org.onosproject.inbandtelemetry.api.IntIntentId; |
Carmelo Cascone | b330fc7 | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 29 | import org.onosproject.net.behaviour.inbandtelemetry.IntObjective; |
| 30 | import org.onosproject.net.behaviour.inbandtelemetry.IntProgrammable; |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 31 | import org.onosproject.inbandtelemetry.api.IntService; |
| 32 | import org.onosproject.mastership.MastershipService; |
| 33 | import org.onosproject.net.ConnectPoint; |
| 34 | import org.onosproject.net.Device; |
| 35 | import org.onosproject.net.DeviceId; |
| 36 | import org.onosproject.net.MastershipRole; |
| 37 | import org.onosproject.net.PortNumber; |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 38 | import org.onosproject.net.config.ConfigFactory; |
| 39 | import org.onosproject.net.config.NetworkConfigEvent; |
| 40 | import org.onosproject.net.config.NetworkConfigListener; |
| 41 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 42 | import org.onosproject.net.config.NetworkConfigService; |
| 43 | import org.onosproject.net.config.basics.SubjectFactories; |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 44 | import org.onosproject.net.device.DeviceEvent; |
| 45 | import org.onosproject.net.device.DeviceListener; |
| 46 | import org.onosproject.net.device.DeviceService; |
| 47 | import org.onosproject.net.host.HostEvent; |
| 48 | import org.onosproject.net.host.HostListener; |
| 49 | import org.onosproject.net.host.HostService; |
| 50 | import org.onosproject.store.serializers.KryoNamespaces; |
| 51 | import org.onosproject.store.service.AtomicIdGenerator; |
| 52 | import org.onosproject.store.service.AtomicValue; |
| 53 | import org.onosproject.store.service.AtomicValueEvent; |
| 54 | import org.onosproject.store.service.AtomicValueEventListener; |
| 55 | import org.onosproject.store.service.ConsistentMap; |
| 56 | import org.onosproject.store.service.MapEvent; |
| 57 | import org.onosproject.store.service.MapEventListener; |
| 58 | import org.onosproject.store.service.Serializer; |
| 59 | import org.onosproject.store.service.StorageService; |
| 60 | import org.onosproject.store.service.Versioned; |
Ray Milkey | db57f1c | 2018-10-09 10:39:29 -0700 | [diff] [blame] | 61 | import org.osgi.service.component.annotations.Activate; |
| 62 | import org.osgi.service.component.annotations.Component; |
| 63 | import org.osgi.service.component.annotations.Deactivate; |
| 64 | import org.osgi.service.component.annotations.Reference; |
| 65 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 66 | import org.slf4j.Logger; |
| 67 | |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 68 | import java.util.Map; |
| 69 | import java.util.Optional; |
| 70 | import java.util.Set; |
| 71 | import java.util.concurrent.ConcurrentMap; |
| 72 | import java.util.concurrent.ExecutionException; |
| 73 | import java.util.concurrent.ScheduledFuture; |
| 74 | import java.util.concurrent.TimeUnit; |
| 75 | import java.util.concurrent.TimeoutException; |
| 76 | import java.util.concurrent.locks.Lock; |
| 77 | import java.util.stream.Collectors; |
| 78 | |
| 79 | import static com.google.common.base.Preconditions.checkNotNull; |
| 80 | import static org.slf4j.LoggerFactory.getLogger; |
| 81 | |
| 82 | /** |
| 83 | * Simple implementation of IntService, for controlling INT-capable pipelines. |
| 84 | * <p> |
| 85 | * All INT intents are converted to an equivalent INT objective and applied to |
| 86 | * all SOURCE_SINK devices. A device is deemed SOURCE_SINK if it has at least |
| 87 | * one host attached. |
| 88 | * <p> |
| 89 | * The implementation listens for different types of events and when required it |
| 90 | * configures a device by cleaning-up any previous state and applying the new |
| 91 | * one. |
| 92 | */ |
Ray Milkey | db57f1c | 2018-10-09 10:39:29 -0700 | [diff] [blame] | 93 | @Component(immediate = true, service = IntService.class) |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 94 | public class SimpleIntManager implements IntService { |
| 95 | |
| 96 | private final Logger log = getLogger(getClass()); |
| 97 | |
| 98 | private static final int CONFIG_EVENT_DELAY = 5; // Seconds. |
| 99 | |
| 100 | private static final String APP_NAME = "org.onosproject.inbandtelemetry"; |
| 101 | |
Ray Milkey | db57f1c | 2018-10-09 10:39:29 -0700 | [diff] [blame] | 102 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame^] | 103 | protected CoreService coreService; |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 104 | |
Ray Milkey | db57f1c | 2018-10-09 10:39:29 -0700 | [diff] [blame] | 105 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame^] | 106 | protected DeviceService deviceService; |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 107 | |
Ray Milkey | db57f1c | 2018-10-09 10:39:29 -0700 | [diff] [blame] | 108 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame^] | 109 | protected StorageService storageService; |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 110 | |
Ray Milkey | db57f1c | 2018-10-09 10:39:29 -0700 | [diff] [blame] | 111 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame^] | 112 | protected MastershipService mastershipService; |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 113 | |
Ray Milkey | db57f1c | 2018-10-09 10:39:29 -0700 | [diff] [blame] | 114 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame^] | 115 | protected HostService hostService; |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 116 | |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 117 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame^] | 118 | protected NetworkConfigService netcfgService; |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 119 | |
| 120 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame^] | 121 | protected NetworkConfigRegistry netcfgRegistry; |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 122 | |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 123 | private final Striped<Lock> deviceLocks = Striped.lock(10); |
| 124 | |
| 125 | private final ConcurrentMap<DeviceId, ScheduledFuture<?>> scheduledDeviceTasks = Maps.newConcurrentMap(); |
| 126 | |
| 127 | // Distributed state. |
| 128 | private ConsistentMap<IntIntentId, IntIntent> intentMap; |
| 129 | private ConsistentMap<DeviceId, Long> devicesToConfigure; |
Carmelo Cascone | b330fc7 | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 130 | private AtomicValue<IntDeviceConfig> intConfig; |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 131 | private AtomicValue<Boolean> intStarted; |
| 132 | private AtomicIdGenerator intentIds; |
| 133 | |
| 134 | // Event listeners. |
| 135 | private final InternalHostListener hostListener = new InternalHostListener(); |
| 136 | private final InternalDeviceListener deviceListener = new InternalDeviceListener(); |
| 137 | private final InternalIntentMapListener intentMapListener = new InternalIntentMapListener(); |
| 138 | private final InternalIntConfigListener intConfigListener = new InternalIntConfigListener(); |
| 139 | private final InternalIntStartedListener intStartedListener = new InternalIntStartedListener(); |
| 140 | private final InternalDeviceToConfigureListener devicesToConfigureListener = |
| 141 | new InternalDeviceToConfigureListener(); |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 142 | private final NetworkConfigListener appConfigListener = new IntAppConfigListener(); |
| 143 | |
| 144 | private final ConfigFactory<ApplicationId, IntReportConfig> intAppConfigFactory = |
| 145 | new ConfigFactory<>(SubjectFactories.APP_SUBJECT_FACTORY, |
| 146 | IntReportConfig.class, "report") { |
| 147 | @Override |
| 148 | public IntReportConfig createConfig() { |
| 149 | return new IntReportConfig(); |
| 150 | } |
| 151 | }; |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 152 | |
| 153 | @Activate |
| 154 | public void activate() { |
| 155 | |
| 156 | final ApplicationId appId = coreService.registerApplication(APP_NAME); |
| 157 | |
| 158 | KryoNamespace.Builder serializer = KryoNamespace.newBuilder() |
| 159 | .register(KryoNamespaces.API) |
| 160 | .register(IntIntent.class) |
| 161 | .register(IntIntentId.class) |
| 162 | .register(IntDeviceRole.class) |
| 163 | .register(IntIntent.IntHeaderType.class) |
Carmelo Cascone | b330fc7 | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 164 | .register(IntMetadataType.class) |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 165 | .register(IntIntent.IntReportType.class) |
| 166 | .register(IntIntent.TelemetryMode.class) |
Carmelo Cascone | b330fc7 | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 167 | .register(IntDeviceConfig.class) |
| 168 | .register(IntDeviceConfig.TelemetrySpec.class); |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 169 | |
| 170 | devicesToConfigure = storageService.<DeviceId, Long>consistentMapBuilder() |
| 171 | .withSerializer(Serializer.using(serializer.build())) |
| 172 | .withName("onos-int-devices-to-configure") |
| 173 | .withApplicationId(appId) |
| 174 | .withPurgeOnUninstall() |
| 175 | .build(); |
| 176 | devicesToConfigure.addListener(devicesToConfigureListener); |
| 177 | |
| 178 | intentMap = storageService.<IntIntentId, IntIntent>consistentMapBuilder() |
| 179 | .withSerializer(Serializer.using(serializer.build())) |
| 180 | .withName("onos-int-intents") |
| 181 | .withApplicationId(appId) |
| 182 | .withPurgeOnUninstall() |
| 183 | .build(); |
| 184 | intentMap.addListener(intentMapListener); |
| 185 | |
| 186 | intStarted = storageService.<Boolean>atomicValueBuilder() |
| 187 | .withSerializer(Serializer.using(serializer.build())) |
| 188 | .withName("onos-int-started") |
| 189 | .withApplicationId(appId) |
| 190 | .build() |
| 191 | .asAtomicValue(); |
| 192 | intStarted.addListener(intStartedListener); |
| 193 | |
Carmelo Cascone | b330fc7 | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 194 | intConfig = storageService.<IntDeviceConfig>atomicValueBuilder() |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 195 | .withSerializer(Serializer.using(serializer.build())) |
| 196 | .withName("onos-int-config") |
| 197 | .withApplicationId(appId) |
| 198 | .build() |
| 199 | .asAtomicValue(); |
| 200 | intConfig.addListener(intConfigListener); |
| 201 | |
| 202 | intentIds = storageService.getAtomicIdGenerator("int-intent-id-generator"); |
| 203 | |
| 204 | // Bootstrap config for already existing devices. |
| 205 | triggerAllDeviceConfigure(); |
| 206 | |
| 207 | hostService.addListener(hostListener); |
| 208 | deviceService.addListener(deviceListener); |
| 209 | |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 210 | netcfgRegistry.registerConfigFactory(intAppConfigFactory); |
| 211 | netcfgService.addListener(appConfigListener); |
Yi Tseng | c4f90a5 | 2020-10-04 22:33:22 -0700 | [diff] [blame] | 212 | // Initialize the INT report |
| 213 | IntReportConfig reportConfig = netcfgService.getConfig(appId, IntReportConfig.class); |
| 214 | if (reportConfig != null) { |
| 215 | IntDeviceConfig intDeviceConfig = IntDeviceConfig.builder() |
| 216 | .withMinFlowHopLatencyChangeNs(reportConfig.minFlowHopLatencyChangeNs()) |
| 217 | .withCollectorPort(reportConfig.collectorPort()) |
| 218 | .withCollectorIp(reportConfig.collectorIp()) |
| 219 | .enabled(true) |
| 220 | .build(); |
| 221 | setConfig(intDeviceConfig); |
| 222 | } |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 223 | |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 224 | startInt(); |
Yi Tseng | c4f90a5 | 2020-10-04 22:33:22 -0700 | [diff] [blame] | 225 | log.info("Started"); |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | @Deactivate |
| 229 | public void deactivate() { |
| 230 | deviceService.removeListener(deviceListener); |
| 231 | hostService.removeListener(hostListener); |
| 232 | intentIds = null; |
| 233 | intConfig.removeListener(intConfigListener); |
| 234 | intConfig = null; |
| 235 | intStarted.removeListener(intStartedListener); |
| 236 | intStarted = null; |
| 237 | intentMap.removeListener(intentMapListener); |
| 238 | intentMap = null; |
| 239 | devicesToConfigure.removeListener(devicesToConfigureListener); |
| 240 | devicesToConfigure.destroy(); |
| 241 | devicesToConfigure = null; |
| 242 | // Cancel tasks (if any). |
| 243 | scheduledDeviceTasks.values().forEach(f -> { |
| 244 | f.cancel(true); |
| 245 | if (!f.isDone()) { |
| 246 | try { |
| 247 | f.get(1, TimeUnit.SECONDS); |
| 248 | } catch (InterruptedException | ExecutionException | TimeoutException e) { |
| 249 | // Don't care, we are terminating the service anyways. |
| 250 | } |
| 251 | } |
| 252 | }); |
| 253 | // Clean up INT rules from existing devices. |
| 254 | deviceService.getDevices().forEach(d -> cleanupDevice(d.id())); |
Yi Tseng | c4f90a5 | 2020-10-04 22:33:22 -0700 | [diff] [blame] | 255 | netcfgService.removeListener(appConfigListener); |
| 256 | netcfgRegistry.unregisterConfigFactory(intAppConfigFactory); |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 257 | log.info("Deactivated"); |
| 258 | } |
| 259 | |
| 260 | @Override |
| 261 | public void startInt() { |
| 262 | // Atomic value event will trigger device configure. |
| 263 | intStarted.set(true); |
| 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public void startInt(Set<DeviceId> deviceIds) { |
| 268 | log.warn("Starting INT for a subset of devices is not supported"); |
| 269 | } |
| 270 | |
| 271 | @Override |
| 272 | public void stopInt() { |
| 273 | // Atomic value event will trigger device configure. |
| 274 | intStarted.set(false); |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public void stopInt(Set<DeviceId> deviceIds) { |
| 279 | log.warn("Stopping INT for a subset of devices is not supported"); |
| 280 | } |
| 281 | |
| 282 | @Override |
Carmelo Cascone | b330fc7 | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 283 | public void setConfig(IntDeviceConfig cfg) { |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 284 | checkNotNull(cfg); |
| 285 | // Atomic value event will trigger device configure. |
| 286 | intConfig.set(cfg); |
| 287 | } |
| 288 | |
| 289 | @Override |
Carmelo Cascone | b330fc7 | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 290 | public IntDeviceConfig getConfig() { |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 291 | return intConfig.get(); |
| 292 | } |
| 293 | |
| 294 | @Override |
| 295 | public IntIntentId installIntIntent(IntIntent intent) { |
| 296 | checkNotNull(intent); |
| 297 | final Integer intentId = (int) intentIds.nextId(); |
| 298 | final IntIntentId intIntentId = IntIntentId.valueOf(intentId); |
| 299 | // Intent map event will trigger device configure. |
| 300 | intentMap.put(intIntentId, intent); |
| 301 | return intIntentId; |
| 302 | } |
| 303 | |
| 304 | @Override |
| 305 | public void removeIntIntent(IntIntentId intentId) { |
| 306 | checkNotNull(intentId); |
| 307 | // Intent map event will trigger device configure. |
Yi Tseng | c4f90a5 | 2020-10-04 22:33:22 -0700 | [diff] [blame] | 308 | if (!intentMap.containsKey(intentId)) { |
| 309 | log.warn("INT intent {} does not exists, skip removing the intent.", intentId); |
| 310 | return; |
| 311 | } |
| 312 | intentMap.remove(intentId); |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | @Override |
| 316 | public IntIntent getIntIntent(IntIntentId intentId) { |
| 317 | return Optional.ofNullable(intentMap.get(intentId).value()).orElse(null); |
| 318 | } |
| 319 | |
| 320 | @Override |
| 321 | public Map<IntIntentId, IntIntent> getIntIntents() { |
| 322 | return intentMap.asJavaMap(); |
| 323 | } |
| 324 | |
| 325 | private boolean isConfigTaskValid(DeviceId deviceId, long creationTime) { |
| 326 | Versioned<?> versioned = devicesToConfigure.get(deviceId); |
| 327 | return versioned != null && versioned.creationTime() == creationTime; |
| 328 | } |
| 329 | |
| 330 | private boolean isIntStarted() { |
| 331 | return intStarted.get(); |
| 332 | } |
| 333 | |
| 334 | private boolean isNotIntConfigured() { |
| 335 | return intConfig.get() == null; |
| 336 | } |
| 337 | |
| 338 | private boolean isIntProgrammable(DeviceId deviceId) { |
| 339 | final Device device = deviceService.getDevice(deviceId); |
| 340 | return device != null && device.is(IntProgrammable.class); |
| 341 | } |
| 342 | |
| 343 | private void triggerDeviceConfigure(DeviceId deviceId) { |
| 344 | if (isIntProgrammable(deviceId)) { |
| 345 | devicesToConfigure.put(deviceId, System.nanoTime()); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | private void triggerAllDeviceConfigure() { |
| 350 | deviceService.getDevices().forEach(d -> triggerDeviceConfigure(d.id())); |
| 351 | } |
| 352 | |
| 353 | private void configDeviceTask(DeviceId deviceId, long creationTime) { |
| 354 | if (isConfigTaskValid(deviceId, creationTime)) { |
| 355 | // Task outdated. |
| 356 | return; |
| 357 | } |
| 358 | if (!deviceService.isAvailable(deviceId)) { |
| 359 | return; |
| 360 | } |
| 361 | final MastershipRole role = mastershipService.requestRoleForSync(deviceId); |
| 362 | if (!role.equals(MastershipRole.MASTER)) { |
| 363 | return; |
| 364 | } |
| 365 | deviceLocks.get(deviceId).lock(); |
| 366 | try { |
| 367 | // Clean up first. |
| 368 | cleanupDevice(deviceId); |
| 369 | if (!configDevice(deviceId)) { |
| 370 | // Clean up if fails. |
| 371 | cleanupDevice(deviceId); |
| 372 | return; |
| 373 | } |
| 374 | devicesToConfigure.remove(deviceId); |
| 375 | } finally { |
| 376 | deviceLocks.get(deviceId).unlock(); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | private void cleanupDevice(DeviceId deviceId) { |
| 381 | final Device device = deviceService.getDevice(deviceId); |
| 382 | if (device == null || !device.is(IntProgrammable.class)) { |
| 383 | return; |
| 384 | } |
| 385 | device.as(IntProgrammable.class).cleanup(); |
| 386 | } |
| 387 | |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame^] | 388 | protected boolean configDevice(DeviceId deviceId) { |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 389 | // Returns true if config was successful, false if not and a clean up is |
| 390 | // needed. |
| 391 | final Device device = deviceService.getDevice(deviceId); |
| 392 | if (device == null || !device.is(IntProgrammable.class)) { |
| 393 | return true; |
| 394 | } |
| 395 | |
| 396 | if (isNotIntConfigured()) { |
| 397 | log.warn("Missing INT config, aborting programming of INT device {}", deviceId); |
| 398 | return true; |
| 399 | } |
| 400 | |
| 401 | final boolean isEdge = !hostService.getConnectedHosts(deviceId).isEmpty(); |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame^] | 402 | final IntDeviceRole intDeviceRole = |
| 403 | isEdge ? IntDeviceRole.SOURCE_SINK : IntDeviceRole.TRANSIT; |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 404 | |
| 405 | log.info("Started programming of INT device {} with role {}...", |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame^] | 406 | deviceId, intDeviceRole); |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 407 | |
| 408 | final IntProgrammable intProg = device.as(IntProgrammable.class); |
| 409 | |
| 410 | if (!isIntStarted()) { |
| 411 | // Leave device with no INT configuration. |
| 412 | return true; |
| 413 | } |
| 414 | |
| 415 | if (!intProg.init()) { |
| 416 | log.warn("Unable to init INT pipeline on {}", deviceId); |
| 417 | return false; |
| 418 | } |
| 419 | |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame^] | 420 | boolean supportSource = intProg.supportsFunctionality(IntProgrammable.IntFunctionality.SOURCE); |
| 421 | boolean supportSink = intProg.supportsFunctionality(IntProgrammable.IntFunctionality.SINK); |
| 422 | boolean supportPostcard = intProg.supportsFunctionality(IntProgrammable.IntFunctionality.POSTCARD); |
| 423 | |
| 424 | if (intDeviceRole != IntDeviceRole.SOURCE_SINK && !supportPostcard) { |
| 425 | // Stop here, no more configuration needed for transit devices unless it support postcard. |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 426 | return true; |
| 427 | } |
| 428 | |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame^] | 429 | if (supportSink || supportPostcard) { |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 430 | if (!intProg.setupIntConfig(intConfig.get())) { |
| 431 | log.warn("Unable to apply INT report config on {}", deviceId); |
| 432 | return false; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | // Port configuration. |
| 437 | final Set<PortNumber> hostPorts = deviceService.getPorts(deviceId) |
| 438 | .stream() |
| 439 | .map(port -> new ConnectPoint(deviceId, port.number())) |
| 440 | .filter(cp -> !hostService.getConnectedHosts(cp).isEmpty()) |
| 441 | .map(ConnectPoint::port) |
| 442 | .collect(Collectors.toSet()); |
| 443 | |
| 444 | for (PortNumber port : hostPorts) { |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame^] | 445 | if (supportSource) { |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 446 | log.info("Setting port {}/{} as INT source port...", deviceId, port); |
| 447 | if (!intProg.setSourcePort(port)) { |
| 448 | log.warn("Unable to set INT source port {} on {}", port, deviceId); |
| 449 | return false; |
| 450 | } |
| 451 | } |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame^] | 452 | if (supportSink) { |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 453 | log.info("Setting port {}/{} as INT sink port...", deviceId, port); |
| 454 | if (!intProg.setSinkPort(port)) { |
| 455 | log.warn("Unable to set INT sink port {} on {}", port, deviceId); |
| 456 | return false; |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame^] | 461 | if (!supportSource && !supportPostcard) { |
| 462 | // Stop here, no more configuration needed for sink devices unless |
| 463 | // it supports postcard mode. |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 464 | return true; |
| 465 | } |
| 466 | |
| 467 | // Apply intents. |
| 468 | // This is a trivial implementation where we simply get the |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame^] | 469 | // corresponding INT objective from an intent and we apply to all |
| 470 | // device which support reporting. |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 471 | int appliedCount = 0; |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame^] | 472 | for (Versioned<IntIntent> versionedIntent : intentMap.values()) { |
| 473 | IntIntent intent = versionedIntent.value(); |
| 474 | IntObjective intObjective = getIntObjective(intent); |
| 475 | if (intent.telemetryMode() == IntIntent.TelemetryMode.INBAND_TELEMETRY && supportSource) { |
| 476 | intProg.addIntObjective(intObjective); |
| 477 | appliedCount++; |
| 478 | } else if (intent.telemetryMode() == IntIntent.TelemetryMode.POSTCARD && supportPostcard) { |
| 479 | intProg.addIntObjective(intObjective); |
| 480 | appliedCount++; |
| 481 | } else { |
| 482 | log.warn("Device {} does not support intent {}.", deviceId, intent); |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 483 | } |
| 484 | } |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 485 | log.info("Completed programming of {}, applied {} INT objectives of {} total", |
Yi Tseng | d14f1a3 | 2020-10-13 19:15:12 -0700 | [diff] [blame^] | 486 | deviceId, appliedCount, intentMap.size()); |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 487 | return true; |
| 488 | } |
| 489 | |
| 490 | private IntObjective getIntObjective(IntIntent intent) { |
Carmelo Cascone | b330fc7 | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 491 | // FIXME: we are ignore intent.headerType() |
| 492 | // what should we do with it? |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 493 | return new IntObjective.Builder() |
| 494 | .withSelector(intent.selector()) |
| 495 | .withMetadataTypes(intent.metadataTypes()) |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 496 | .build(); |
| 497 | } |
| 498 | |
| 499 | /* Event listeners which trigger device configuration. */ |
| 500 | |
| 501 | private class InternalHostListener implements HostListener { |
| 502 | @Override |
| 503 | public void event(HostEvent event) { |
| 504 | final DeviceId deviceId = event.subject().location().deviceId(); |
| 505 | triggerDeviceConfigure(deviceId); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | private class InternalDeviceListener implements DeviceListener { |
| 510 | @Override |
| 511 | public void event(DeviceEvent event) { |
| 512 | switch (event.type()) { |
| 513 | case DEVICE_ADDED: |
| 514 | case DEVICE_UPDATED: |
| 515 | case DEVICE_REMOVED: |
| 516 | case DEVICE_SUSPENDED: |
| 517 | case DEVICE_AVAILABILITY_CHANGED: |
| 518 | case PORT_ADDED: |
| 519 | case PORT_UPDATED: |
| 520 | case PORT_REMOVED: |
| 521 | triggerDeviceConfigure(event.subject().id()); |
| 522 | return; |
| 523 | case PORT_STATS_UPDATED: |
| 524 | return; |
| 525 | default: |
| 526 | log.warn("Unknown device event type {}", event.type()); |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | private class InternalIntentMapListener |
| 532 | implements MapEventListener<IntIntentId, IntIntent> { |
| 533 | @Override |
| 534 | public void event(MapEvent<IntIntentId, IntIntent> event) { |
| 535 | triggerAllDeviceConfigure(); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | private class InternalIntConfigListener |
Carmelo Cascone | b330fc7 | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 540 | implements AtomicValueEventListener<IntDeviceConfig> { |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 541 | @Override |
Carmelo Cascone | b330fc7 | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 542 | public void event(AtomicValueEvent<IntDeviceConfig> event) { |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 543 | triggerAllDeviceConfigure(); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | private class InternalIntStartedListener |
| 548 | implements AtomicValueEventListener<Boolean> { |
| 549 | @Override |
| 550 | public void event(AtomicValueEvent<Boolean> event) { |
| 551 | triggerAllDeviceConfigure(); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | private class InternalDeviceToConfigureListener |
| 556 | implements MapEventListener<DeviceId, Long> { |
| 557 | @Override |
| 558 | public void event(MapEvent<DeviceId, Long> event) { |
| 559 | if (event.type().equals(MapEvent.Type.REMOVE) || |
| 560 | event.newValue() == null) { |
| 561 | return; |
| 562 | } |
| 563 | // Schedule task in the future. Wait for events for this device to |
| 564 | // stabilize. |
| 565 | final DeviceId deviceId = event.key(); |
| 566 | final long creationTime = event.newValue().creationTime(); |
| 567 | ScheduledFuture<?> newTask = SharedScheduledExecutors.newTimeout( |
| 568 | () -> configDeviceTask(deviceId, creationTime), |
| 569 | CONFIG_EVENT_DELAY, TimeUnit.SECONDS); |
| 570 | ScheduledFuture<?> oldTask = scheduledDeviceTasks.put(deviceId, newTask); |
| 571 | if (oldTask != null) { |
| 572 | oldTask.cancel(false); |
| 573 | } |
| 574 | } |
| 575 | } |
Yi Tseng | da70796 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 576 | |
| 577 | private class IntAppConfigListener implements NetworkConfigListener { |
| 578 | |
| 579 | @Override |
| 580 | public void event(NetworkConfigEvent event) { |
| 581 | switch (event.type()) { |
| 582 | case CONFIG_ADDED: |
| 583 | case CONFIG_UPDATED: |
| 584 | event.config() |
| 585 | .map(config -> (IntReportConfig) config) |
| 586 | .ifPresent(config -> { |
| 587 | IntDeviceConfig intDeviceConfig = IntDeviceConfig.builder() |
| 588 | .withMinFlowHopLatencyChangeNs(config.minFlowHopLatencyChangeNs()) |
| 589 | .withCollectorPort(config.collectorPort()) |
| 590 | .withCollectorIp(config.collectorIp()) |
| 591 | .enabled(true) |
| 592 | .build(); |
| 593 | setConfig(intDeviceConfig); |
| 594 | }); |
| 595 | break; |
| 596 | // TODO: Support removing INT config. |
| 597 | default: |
| 598 | break; |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | @Override |
| 603 | public boolean isRelevant(NetworkConfigEvent event) { |
| 604 | return event.configClass() == IntReportConfig.class; |
| 605 | } |
| 606 | } |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 607 | } |