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