Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [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 | */ |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 16 | package org.onosproject.net.optical.intent.impl.compiler; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 17 | |
Marc De Leenheer | 0b8b2ef | 2015-08-03 15:39:00 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Sho SHIMIZU | 07ade9b | 2016-01-28 19:38:50 -0800 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Sets; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
Sho SHIMIZU | 9a2b829 | 2015-10-28 13:00:16 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Component; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 27 | import org.onlab.graph.ScalarWeight; |
| 28 | import org.onlab.graph.Weight; |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 29 | import org.onosproject.net.AnnotationKeys; |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 30 | import org.onosproject.net.Annotations; |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 31 | import org.onosproject.net.ChannelSpacing; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.net.ConnectPoint; |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 33 | import org.onosproject.net.DefaultAnnotations; |
| 34 | import org.onosproject.net.DefaultLink; |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 35 | import org.onosproject.net.DefaultOchSignalComparator; |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 36 | import org.onosproject.net.DefaultPath; |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 37 | import org.onosproject.net.DeviceId; |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 38 | import org.onosproject.net.GridType; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 39 | import org.onosproject.net.Link; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 40 | import org.onosproject.net.OchSignal; |
Marc De Leenheer | d24420f | 2015-05-27 09:40:59 -0700 | [diff] [blame] | 41 | import org.onosproject.net.OchSignalType; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 42 | import org.onosproject.net.Path; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 43 | import org.onosproject.net.Port; |
| 44 | import org.onosproject.net.device.DeviceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 45 | import org.onosproject.net.intent.Intent; |
| 46 | import org.onosproject.net.intent.IntentCompiler; |
| 47 | import org.onosproject.net.intent.IntentExtensionService; |
| 48 | import org.onosproject.net.intent.OpticalConnectivityIntent; |
| 49 | import org.onosproject.net.intent.OpticalPathIntent; |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 50 | import org.onosproject.net.optical.OchPort; |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 51 | import org.onosproject.net.provider.ProviderId; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 52 | import org.onosproject.net.resource.Resource; |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 53 | import org.onosproject.net.resource.ResourceAllocation; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 54 | import org.onosproject.net.resource.ResourceService; |
| 55 | import org.onosproject.net.resource.Resources; |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 56 | import org.onosproject.net.topology.LinkWeigher; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 57 | import org.onosproject.net.topology.Topology; |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 58 | import org.onosproject.net.topology.TopologyEdge; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 59 | import org.onosproject.net.topology.TopologyService; |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 60 | import org.slf4j.Logger; |
| 61 | import org.slf4j.LoggerFactory; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 62 | |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 63 | import java.util.ArrayList; |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 64 | import java.util.Collection; |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 65 | import java.util.Collections; |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 66 | import java.util.LinkedList; |
Marc De Leenheer | 0b8b2ef | 2015-08-03 15:39:00 -0700 | [diff] [blame] | 67 | import java.util.List; |
Sho SHIMIZU | 07ade9b | 2016-01-28 19:38:50 -0800 | [diff] [blame] | 68 | import java.util.Map; |
| 69 | import java.util.Optional; |
Marc De Leenheer | 0b8b2ef | 2015-08-03 15:39:00 -0700 | [diff] [blame] | 70 | import java.util.Set; |
Sho SHIMIZU | 280e791 | 2015-09-29 13:42:03 -0700 | [diff] [blame] | 71 | import java.util.stream.Collectors; |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 72 | import java.util.stream.IntStream; |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 73 | import java.util.stream.Stream; |
Marc De Leenheer | 0b8b2ef | 2015-08-03 15:39:00 -0700 | [diff] [blame] | 74 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 75 | import static com.google.common.base.Preconditions.checkArgument; |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 76 | import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 77 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 78 | /** |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 79 | * An intent compiler for {@link org.onosproject.net.intent.OpticalConnectivityIntent}. |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 80 | */ |
Sho SHIMIZU | 9a2b829 | 2015-10-28 13:00:16 -0700 | [diff] [blame] | 81 | @Component(immediate = true) |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 82 | public class OpticalConnectivityIntentCompiler implements IntentCompiler<OpticalConnectivityIntent> { |
| 83 | |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame] | 84 | private static final Logger log = LoggerFactory.getLogger(OpticalConnectivityIntentCompiler.class); |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 85 | // By default, allocate 50 GHz lambdas (4 slots of 12.5 GHz) for each intent. |
| 86 | private static final int SLOT_COUNT = 4; |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 87 | private static final ProviderId PROVIDER_ID = new ProviderId("opticalConnectivityIntent", |
| 88 | "org.onosproject.net.optical.intent"); |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 89 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 90 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 91 | protected IntentExtensionService intentManager; |
| 92 | |
| 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 94 | protected TopologyService topologyService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 95 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 97 | protected DeviceService deviceService; |
| 98 | |
| 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Sho SHIMIZU | 5c16df8 | 2015-09-29 12:52:07 -0700 | [diff] [blame] | 100 | protected ResourceService resourceService; |
| 101 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 102 | @Activate |
| 103 | public void activate() { |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 104 | deviceService = opticalView(deviceService); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 105 | intentManager.registerCompiler(OpticalConnectivityIntent.class, this); |
| 106 | } |
| 107 | |
| 108 | @Deactivate |
| 109 | public void deactivate() { |
| 110 | intentManager.unregisterCompiler(OpticalConnectivityIntent.class); |
| 111 | } |
| 112 | |
| 113 | @Override |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 114 | public List<Intent> compile(OpticalConnectivityIntent intent, |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 115 | List<Intent> installable) { |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 116 | // Check if source and destination are optical OCh ports |
| 117 | ConnectPoint src = intent.getSrc(); |
| 118 | ConnectPoint dst = intent.getDst(); |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 119 | checkArgument(deviceService.getPort(src.deviceId(), src.port()) instanceof OchPort); |
| 120 | checkArgument(deviceService.getPort(dst.deviceId(), dst.port()) instanceof OchPort); |
| 121 | List<Resource> resources = new LinkedList<>(); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 122 | |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 123 | log.debug("Compiling optical connectivity intent between {} and {}", src, dst); |
| 124 | |
Rimon Ashkenazy | f069970 | 2016-01-17 19:28:49 +0200 | [diff] [blame] | 125 | // Release of intent resources here is only a temporary solution for handling the |
| 126 | // case of recompiling due to intent restoration (when intent state is FAILED). |
| 127 | // TODO: try to release intent resources in IntentManager. |
Yuta HIGUCHI | 65d9d0e | 2017-05-04 12:44:32 -0700 | [diff] [blame] | 128 | resourceService.release(intent.key()); |
Rimon Ashkenazy | f069970 | 2016-01-17 19:28:49 +0200 | [diff] [blame] | 129 | |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 130 | // Check OCh port availability |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 131 | // If ports are not available, compilation fails |
| 132 | // Else add port to resource reservation list |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 133 | Resource srcPortResource = Resources.discrete(src.deviceId(), src.port()).resource(); |
| 134 | Resource dstPortResource = Resources.discrete(dst.deviceId(), dst.port()).resource(); |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 135 | if (!Stream.of(srcPortResource, dstPortResource).allMatch(resourceService::isAvailable)) { |
Yuta HIGUCHI | 0164c1c | 2017-05-04 15:43:55 -0700 | [diff] [blame] | 136 | log.error("Ports for the intent are not available. Intent: {}", intent); |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 137 | throw new OpticalIntentCompilationException("Ports for the intent are not available. Intent: " + intent); |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 138 | } |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 139 | resources.add(srcPortResource); |
| 140 | resources.add(dstPortResource); |
| 141 | |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 142 | // Find first path that has the required resources |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 143 | Stream<Path> paths = getOpticalPaths(intent); |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 144 | Optional<Map.Entry<Path, List<OchSignal>>> found = paths |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 145 | .map(path -> Maps.immutableEntry(path, findFirstAvailableLambda(intent, path))) |
Sho SHIMIZU | 07ade9b | 2016-01-28 19:38:50 -0800 | [diff] [blame] | 146 | .filter(entry -> !entry.getValue().isEmpty()) |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 147 | .filter(entry -> convertToResources(entry.getKey(), |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 148 | entry.getValue()).stream().allMatch(resourceService::isAvailable)) |
Sho SHIMIZU | 07ade9b | 2016-01-28 19:38:50 -0800 | [diff] [blame] | 149 | .findFirst(); |
| 150 | |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 151 | // Allocate resources and create optical path intent |
Sho SHIMIZU | 07ade9b | 2016-01-28 19:38:50 -0800 | [diff] [blame] | 152 | if (found.isPresent()) { |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 153 | resources.addAll(convertToResources(found.get().getKey(), found.get().getValue())); |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 154 | allocateResources(intent, resources); |
Sho SHIMIZU | 07ade9b | 2016-01-28 19:38:50 -0800 | [diff] [blame] | 155 | OchSignal ochSignal = OchSignal.toFixedGrid(found.get().getValue(), ChannelSpacing.CHL_50GHZ); |
| 156 | return ImmutableList.of(createIntent(intent, found.get().getKey(), ochSignal)); |
| 157 | } else { |
Yuta HIGUCHI | 0164c1c | 2017-05-04 15:43:55 -0700 | [diff] [blame] | 158 | log.error("Unable to find suitable lightpath for intent {}", intent); |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 159 | throw new OpticalIntentCompilationException("Unable to find suitable lightpath for intent " + intent); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 160 | } |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 163 | /** |
| 164 | * Create installable optical path intent. |
| 165 | * Only supports fixed grid for now. |
| 166 | * |
| 167 | * @param parentIntent this intent (used for resource tracking) |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 168 | * @param path the path to use |
| 169 | * @param lambda the lambda to use |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 170 | * @return optical path intent |
| 171 | */ |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 172 | private Intent createIntent(OpticalConnectivityIntent parentIntent, Path path, OchSignal lambda) { |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 173 | OchSignalType signalType = OchSignalType.FIXED_GRID; |
| 174 | |
| 175 | return OpticalPathIntent.builder() |
| 176 | .appId(parentIntent.appId()) |
Yuta HIGUCHI | 652f27f | 2016-10-31 16:54:30 -0700 | [diff] [blame] | 177 | .key(parentIntent.key()) |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 178 | .src(parentIntent.getSrc()) |
| 179 | .dst(parentIntent.getDst()) |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 180 | .path(path) |
| 181 | .lambda(lambda) |
| 182 | .signalType(signalType) |
| 183 | .bidirectional(parentIntent.isBidirectional()) |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 184 | .resourceGroup(parentIntent.resourceGroup()) |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 185 | .build(); |
| 186 | } |
| 187 | |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 188 | /** |
| 189 | * Convert given lambda as discrete resource of all path ports. |
| 190 | * |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 191 | * @param path the path |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 192 | * @param lambda the lambda |
| 193 | * @return list of discrete resources |
| 194 | */ |
| 195 | private List<Resource> convertToResources(Path path, Collection<OchSignal> lambda) { |
| 196 | return path.links().stream() |
| 197 | .flatMap(x -> Stream.of( |
| 198 | Resources.discrete(x.src().deviceId(), x.src().port()).resource(), |
| 199 | Resources.discrete(x.dst().deviceId(), x.dst().port()).resource() |
| 200 | )) |
| 201 | .flatMap(x -> lambda.stream().map(x::child)) |
| 202 | .collect(Collectors.toList()); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Reserve all required resources for this intent. |
| 207 | * |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 208 | * @param intent the intent |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 209 | * @param resources list of resources to reserve |
| 210 | */ |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 211 | private void allocateResources(Intent intent, List<Resource> resources) { |
Yuta HIGUCHI | 65d9d0e | 2017-05-04 12:44:32 -0700 | [diff] [blame] | 212 | List<ResourceAllocation> allocations = resourceService.allocate(intent.key(), resources); |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 213 | if (allocations.isEmpty()) { |
Yuta HIGUCHI | 65d9d0e | 2017-05-04 12:44:32 -0700 | [diff] [blame] | 214 | log.error("Resource allocation for {} failed (resource request: {})", intent.key(), resources); |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 215 | if (log.isDebugEnabled()) { |
| 216 | log.debug("requested resources:\n\t{}", resources.stream() |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 217 | .map(Resource::toString) |
| 218 | .collect(Collectors.joining("\n\t"))); |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 219 | } |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 220 | throw new OpticalIntentCompilationException("Unable to allocate resources: " + resources); |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 224 | /** |
| 225 | * Find the first available lambda on the given path by checking all the port resources. |
| 226 | * |
| 227 | * @param path the path |
| 228 | * @return list of consecutive and available OChSignals |
| 229 | */ |
Marc De Leenheer | adfeffd | 2017-06-22 16:05:34 -0700 | [diff] [blame] | 230 | private List<OchSignal> findFirstAvailableLambda(OpticalConnectivityIntent intent, Path path) { |
| 231 | if (intent.ochSignal().isPresent()) { |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 232 | //create lambdas w.r.t. slotGanularity/slotWidth |
| 233 | OchSignal ochSignal = intent.ochSignal().get(); |
| 234 | if (ochSignal.gridType() == GridType.FLEX) { |
| 235 | // multiplier sits in the middle of slots |
| 236 | int startMultiplier = ochSignal.spacingMultiplier() - (ochSignal.slotGranularity() / 2); |
| 237 | return IntStream.range(0, ochSignal.slotGranularity()) |
| 238 | .mapToObj(x -> OchSignal.newFlexGridSlot(startMultiplier + (2 * x))) |
| 239 | .collect(Collectors.toList()); |
| 240 | } else if (ochSignal.gridType() == GridType.DWDM) { |
| 241 | int startMultiplier = (int) (1 - ochSignal.slotGranularity() + |
| 242 | ochSignal.spacingMultiplier() * ochSignal.channelSpacing().frequency().asHz() / |
| 243 | ChannelSpacing.CHL_6P25GHZ.frequency().asHz()); |
| 244 | return IntStream.range(0, ochSignal.slotGranularity()) |
| 245 | .mapToObj(x -> OchSignal.newFlexGridSlot(startMultiplier + (2 * x))) |
| 246 | .collect(Collectors.toList()); |
| 247 | } |
| 248 | //TODO: add support for other gridTypes |
| 249 | log.error("Grid type: {} not supported for user defined signal intents", ochSignal.gridType()); |
| 250 | return Collections.emptyList(); |
Marc De Leenheer | adfeffd | 2017-06-22 16:05:34 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 253 | Set<OchSignal> lambdas = findCommonLambdas(path); |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 254 | if (lambdas.isEmpty()) { |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 255 | return Collections.emptyList(); |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 256 | } |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 257 | |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 258 | return findFirstLambda(lambdas, slotCount()); |
| 259 | } |
| 260 | |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 261 | /** |
| 262 | * Get the number of 12.5 GHz slots required for the path. |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 263 | * <p> |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 264 | * For now this returns a constant value of 4 (i.e., fixed grid 50 GHz slot), |
| 265 | * but in the future can depend on optical reach, line rate, transponder port capabilities, etc. |
| 266 | * |
| 267 | * @return number of slots |
| 268 | */ |
| 269 | private int slotCount() { |
| 270 | return SLOT_COUNT; |
| 271 | } |
| 272 | |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 273 | /** |
| 274 | * Find common lambdas on all ports that compose the path. |
| 275 | * |
| 276 | * @param path the path |
| 277 | * @return set of common lambdas |
| 278 | */ |
| 279 | private Set<OchSignal> findCommonLambdas(Path path) { |
| 280 | return path.links().stream() |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 281 | .flatMap(x -> Stream.of( |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 282 | Resources.discrete(x.src().deviceId(), x.src().port()).id(), |
| 283 | Resources.discrete(x.dst().deviceId(), x.dst().port()).id() |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 284 | )) |
Sho SHIMIZU | 7332fe4 | 2016-02-15 14:58:33 -0800 | [diff] [blame] | 285 | .map(x -> resourceService.getAvailableResourceValues(x, OchSignal.class)) |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 286 | .map(x -> (Set<OchSignal>) ImmutableSet.copyOf(x)) |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 287 | .reduce(Sets::intersection) |
| 288 | .orElse(Collections.emptySet()); |
| 289 | } |
| 290 | |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 291 | /** |
| 292 | * Returns list of consecutive resources in given set of lambdas. |
| 293 | * |
| 294 | * @param lambdas list of lambdas |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 295 | * @param count number of consecutive lambdas to return |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 296 | * @return list of consecutive lambdas |
| 297 | */ |
| 298 | private List<OchSignal> findFirstLambda(Set<OchSignal> lambdas, int count) { |
| 299 | // Sort available lambdas |
| 300 | List<OchSignal> lambdaList = new ArrayList<>(lambdas); |
| 301 | lambdaList.sort(new DefaultOchSignalComparator()); |
| 302 | |
| 303 | // Look ahead by count and ensure spacing multiplier is as expected (i.e., no gaps) |
| 304 | for (int i = 0; i < lambdaList.size() - count; i++) { |
| 305 | if (lambdaList.get(i).spacingMultiplier() + 2 * count == |
| 306 | lambdaList.get(i + count).spacingMultiplier()) { |
| 307 | return lambdaList.subList(i, i + count); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | return Collections.emptyList(); |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 314 | private ConnectPoint staticPort(ConnectPoint connectPoint) { |
| 315 | Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port()); |
| 316 | |
| 317 | String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT); |
| 318 | |
| 319 | // FIXME: need a better way to match the port |
| 320 | if (staticPort != null) { |
| 321 | for (Port p : deviceService.getPorts(connectPoint.deviceId())) { |
| 322 | if (staticPort.equals(p.number().name())) { |
| 323 | return new ConnectPoint(p.element().id(), p.number()); |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | return null; |
| 329 | } |
| 330 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 331 | /** |
| 332 | * Calculates optical paths in WDM topology. |
| 333 | * |
| 334 | * @param intent optical connectivity intent |
| 335 | * @return set of paths in WDM topology |
| 336 | */ |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 337 | private Stream<Path> getOpticalPaths(OpticalConnectivityIntent intent) { |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 338 | // Route in WDM topology |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 339 | Topology topology = topologyService.currentTopology(); |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 340 | //TODO: refactor with LinkWeigher class Implementation |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 341 | LinkWeigher weight = new LinkWeigher() { |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 342 | |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 343 | @Override |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 344 | public Weight getInitialWeight() { |
| 345 | return ScalarWeight.toWeight(0.0); |
| 346 | } |
| 347 | |
| 348 | @Override |
| 349 | public Weight getNonViableWeight() { |
| 350 | return ScalarWeight.NON_VIABLE_WEIGHT; |
| 351 | } |
| 352 | |
| 353 | @Override |
| 354 | public Weight weight(TopologyEdge edge) { |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 355 | // Disregard inactive or non-optical links |
| 356 | if (edge.link().state() == Link.State.INACTIVE) { |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 357 | return ScalarWeight.toWeight(-1); |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 358 | } |
| 359 | if (edge.link().type() != Link.Type.OPTICAL) { |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 360 | return ScalarWeight.toWeight(-1); |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 361 | } |
| 362 | // Adhere to static port mappings |
| 363 | DeviceId srcDeviceId = edge.link().src().deviceId(); |
| 364 | if (srcDeviceId.equals(intent.getSrc().deviceId())) { |
| 365 | ConnectPoint srcStaticPort = staticPort(intent.getSrc()); |
| 366 | if (srcStaticPort != null) { |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 367 | return ScalarWeight.toWeight(srcStaticPort.equals(edge.link().src()) ? 1 : -1); |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | DeviceId dstDeviceId = edge.link().dst().deviceId(); |
| 371 | if (dstDeviceId.equals(intent.getDst().deviceId())) { |
| 372 | ConnectPoint dstStaticPort = staticPort(intent.getDst()); |
| 373 | if (dstStaticPort != null) { |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 374 | return ScalarWeight.toWeight(dstStaticPort.equals(edge.link().dst()) ? 1 : -1); |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 378 | return ScalarWeight.toWeight(1); |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 379 | } |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 380 | }; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 381 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 382 | ConnectPoint start = intent.getSrc(); |
| 383 | ConnectPoint end = intent.getDst(); |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 384 | |
| 385 | // 0 hop case |
| 386 | if (start.deviceId().equals(end.deviceId())) { |
| 387 | log.debug("install optical intent for 0 hop i.e srcDeviceId=dstDeviceId"); |
| 388 | DefaultLink defaultLink = DefaultLink.builder() |
| 389 | .providerId(PROVIDER_ID) |
| 390 | .src(start) |
| 391 | .dst(end) |
| 392 | .state(Link.State.ACTIVE) |
| 393 | .type(Link.Type.DIRECT) |
| 394 | .isExpected(true) |
| 395 | .build(); |
| 396 | List<Link> links = ImmutableList.<Link>builder().add(defaultLink).build(); |
| 397 | Annotations annotations = DefaultAnnotations.builder().build(); |
| 398 | DefaultPath defaultPath = new DefaultPath(PROVIDER_ID, links, null, annotations); |
| 399 | return ImmutableList.<Path>builder().add(defaultPath).build().stream(); |
| 400 | } |
| 401 | |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 402 | //head link's src port should be same as intent src port and tail link dst port |
| 403 | //should be same as intent dst port in the path. |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 404 | Stream<Path> paths = topologyService.getKShortestPaths(topology, |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 405 | start.deviceId(), |
| 406 | end.deviceId(), |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 407 | weight) |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 408 | .filter(p -> p.links().get(0).src().port().equals(start.port()) && |
| 409 | p.links().get(p.links().size() - 1).dst().port().equals(end.port())); |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 410 | if (log.isDebugEnabled()) { |
| 411 | return paths |
| 412 | .map(path -> { |
| 413 | // no-op map stage to add debug logging |
| 414 | log.debug("Candidate path: {}", |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 415 | path.links().stream() |
| 416 | .map(lk -> lk.src() + "-" + lk.dst()) |
| 417 | .collect(Collectors.toList())); |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 418 | return path; |
| 419 | }); |
| 420 | } |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 421 | return paths; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 422 | } |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 423 | } |