blob: b76ece8c53b5f3a0ab2c2a94e0654d571895c36e [file] [log] [blame]
Marc De Leenheer8c2caac2015-05-28 16:37:33 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
16package org.onosproject.net.intent.impl.compiler;
17
18import org.apache.commons.lang3.tuple.Pair;
19import org.apache.felix.scr.annotations.Activate;
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070020import org.apache.felix.scr.annotations.Component;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070021import org.apache.felix.scr.annotations.Deactivate;
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070022import org.apache.felix.scr.annotations.Modified;
23import org.apache.felix.scr.annotations.Property;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070024import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070026import org.onlab.util.Tools;
27import org.onosproject.cfg.ComponentConfigService;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070028import org.onosproject.core.ApplicationId;
29import org.onosproject.core.CoreService;
Marc De Leenheer723f5532015-06-03 20:16:17 -070030import org.onosproject.net.AnnotationKeys;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070031import org.onosproject.net.ConnectPoint;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070032import org.onosproject.net.OchPort;
33import org.onosproject.net.OduCltPort;
34import org.onosproject.net.OduSignalType;
35import org.onosproject.net.Port;
36import org.onosproject.net.device.DeviceService;
37import org.onosproject.net.flow.DefaultFlowRule;
38import org.onosproject.net.flow.DefaultTrafficSelector;
39import org.onosproject.net.flow.DefaultTrafficTreatment;
40import org.onosproject.net.flow.FlowRule;
41import org.onosproject.net.flow.TrafficSelector;
42import org.onosproject.net.flow.TrafficTreatment;
43import org.onosproject.net.intent.FlowRuleIntent;
44import org.onosproject.net.intent.Intent;
45import org.onosproject.net.intent.IntentCompiler;
46import org.onosproject.net.intent.IntentExtensionService;
47import org.onosproject.net.intent.IntentId;
48import org.onosproject.net.intent.IntentService;
49import org.onosproject.net.intent.OpticalCircuitIntent;
50import org.onosproject.net.intent.OpticalConnectivityIntent;
51import org.onosproject.net.intent.impl.IntentCompilationException;
Sho SHIMIZU5c16df82015-09-29 12:52:07 -070052import org.onosproject.net.newresource.ResourceAllocation;
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080053import org.onosproject.net.newresource.Resource;
Sho SHIMIZU5c16df82015-09-29 12:52:07 -070054import org.onosproject.net.newresource.ResourceService;
Sho SHIMIZU460b9722016-01-28 10:48:26 -080055import org.onosproject.net.newresource.Resources;
Sho SHIMIZU7d20af12015-10-01 16:03:51 -070056import org.onosproject.net.resource.device.IntentSetMultimap;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070057import org.onosproject.net.resource.link.LinkResourceAllocations;
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070058import org.osgi.service.component.ComponentContext;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070059import org.slf4j.Logger;
60import org.slf4j.LoggerFactory;
61
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070062import java.util.Collections;
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070063import java.util.Dictionary;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070064import java.util.LinkedList;
65import java.util.List;
Sho SHIMIZU5c16df82015-09-29 12:52:07 -070066import java.util.Optional;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070067import java.util.Set;
68
69import static com.google.common.base.Preconditions.checkArgument;
70
71/**
72 * An intent compiler for {@link org.onosproject.net.intent.OpticalCircuitIntent}.
73 */
Ray Milkeye6099412015-10-05 10:53:17 -070074// For now, remove component designation until dependency on the new resource manager is available.
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070075@Component(immediate = true)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070076public class OpticalCircuitIntentCompiler implements IntentCompiler<OpticalCircuitIntent> {
77
78 private static final Logger log = LoggerFactory.getLogger(OpticalCircuitIntentCompiler.class);
79
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -070080 private static final int DEFAULT_MAX_CAPACITY = 10;
81
82 @Property(name = "maxCapacity", intValue = DEFAULT_MAX_CAPACITY,
83 label = "Maximum utilization of an optical connection.")
84
85 private int maxCapacity = DEFAULT_MAX_CAPACITY;
86
87 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
88 protected ComponentConfigService cfgService;
89
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070090 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
91 protected IntentExtensionService intentManager;
92
93 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
94 protected CoreService coreService;
95
96 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
97 protected DeviceService deviceService;
98
99 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700100 protected ResourceService resourceService;
101
102 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZU7d20af12015-10-01 16:03:51 -0700103 protected IntentSetMultimap intentSetMultimap;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700104
105 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
106 protected IntentService intentService;
107
108 private ApplicationId appId;
109
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700110 @Modified
111 public void modified(ComponentContext context) {
112 Dictionary properties = context.getProperties();
113
114 //TODO for reduction check if the new capacity is smaller than the size of the current mapping
115 String propertyString = Tools.get(properties, "maxCapacity");
116
117 //Ignore if propertyString is empty
118 if (!propertyString.isEmpty()) {
119 try {
120 int temp = Integer.parseInt(propertyString);
121 //Ensure value is non-negative but allow zero as a way to shutdown the link
122 if (temp >= 0) {
123 maxCapacity = temp;
124 }
125 } catch (NumberFormatException e) {
126 //Malformed arguments lead to no change of value (user should be notified of error)
127 log.error("The value '{}' for maxCapacity was not parsable as an integer.", propertyString, e);
128 }
129 } else {
130 //Notify of empty value but do not return (other properties will also go in this function)
131 log.error("The value for maxCapacity was set to an empty value.");
132 }
133
134 }
135
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700136 @Activate
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700137 public void activate(ComponentContext context) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700138 appId = coreService.registerApplication("org.onosproject.net.intent");
139 intentManager.registerCompiler(OpticalCircuitIntent.class, this);
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700140 cfgService.registerProperties(getClass());
141 modified(context);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700142 }
143
144 @Deactivate
145 public void deactivate() {
146 intentManager.unregisterCompiler(OpticalCircuitIntent.class);
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700147 cfgService.unregisterProperties(getClass(), false);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700148 }
149
150 @Override
151 public List<Intent> compile(OpticalCircuitIntent intent, List<Intent> installable,
152 Set<LinkResourceAllocations> resources) {
153 // Check if ports are OduClt ports
154 ConnectPoint src = intent.getSrc();
155 ConnectPoint dst = intent.getDst();
156 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
157 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
158 checkArgument(srcPort instanceof OduCltPort);
159 checkArgument(dstPort instanceof OduCltPort);
160
161 log.debug("Compiling optical circuit intent between {} and {}", src, dst);
162
163 // Reserve OduClt ports
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800164 Resource srcPortResource = Resources.discrete(src.deviceId(), src.port()).resource();
165 Resource dstPortResource = Resources.discrete(dst.deviceId(), dst.port()).resource();
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800166 List<ResourceAllocation> allocation = resourceService.allocate(intent.id(), srcPortResource, dstPortResource);
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700167 if (allocation.isEmpty()) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700168 throw new IntentCompilationException("Unable to reserve ports for intent " + intent);
169 }
170
171 LinkedList<Intent> intents = new LinkedList<>();
172
173 FlowRuleIntent circuitIntent;
174 OpticalConnectivityIntent connIntent = findOpticalConnectivityIntent(intent);
175
176 // Create optical connectivity intent if needed
177 if (connIntent == null) {
178 // Find OCh ports with available resources
179 Pair<OchPort, OchPort> ochPorts = findPorts(intent);
180
181 if (ochPorts == null) {
182 return Collections.emptyList();
183 }
184
185 // Create optical connectivity intent
186 ConnectPoint srcCP = new ConnectPoint(src.elementId(), ochPorts.getLeft().number());
187 ConnectPoint dstCP = new ConnectPoint(dst.elementId(), ochPorts.getRight().number());
188 // FIXME: hardcoded ODU signal type
189 connIntent = OpticalConnectivityIntent.builder()
190 .appId(appId)
191 .src(srcCP)
192 .dst(dstCP)
193 .signalType(OduSignalType.ODU4)
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700194 .bidirectional(intent.isBidirectional())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700195 .build();
Marc De Leenheer723f5532015-06-03 20:16:17 -0700196 intentService.submit(connIntent);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700197 }
198
199 // Create optical circuit intent
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700200 List<FlowRule> rules = new LinkedList<>();
Brian O'Connor81134662015-06-25 17:23:33 -0400201 rules.add(connectPorts(src, connIntent.getSrc(), intent.priority()));
202 rules.add(connectPorts(connIntent.getDst(), dst, intent.priority()));
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700203
204 // Create flow rules for reverse path
205 if (intent.isBidirectional()) {
Brian O'Connor81134662015-06-25 17:23:33 -0400206 rules.add(connectPorts(connIntent.getSrc(), src, intent.priority()));
207 rules.add(connectPorts(dst, connIntent.getDst(), intent.priority()));
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700208 }
209
210 circuitIntent = new FlowRuleIntent(appId, rules, intent.resources());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700211
212 // Save circuit to connectivity intent mapping
Sho SHIMIZU7d20af12015-10-01 16:03:51 -0700213 intentSetMultimap.allocateMapping(connIntent.id(), intent.id());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700214 intents.add(circuitIntent);
215
216 return intents;
217 }
218
219 /**
220 * Checks if current allocations on given resource can satisfy request.
Marc De Leenheer723f5532015-06-03 20:16:17 -0700221 * If the resource is null, return true.
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700222 *
Marc De Leenheerc9733082015-06-04 12:22:38 -0700223 * @param resource the resource on which to map the intent
224 * @return true if the resource can accept the request, false otherwise
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700225 */
Sho SHIMIZU0f9a68c2015-09-29 12:45:36 -0700226 private boolean isAvailable(IntentId resource) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700227 if (resource == null) {
228 return true;
229 }
230
Sho SHIMIZU7d20af12015-10-01 16:03:51 -0700231 Set<IntentId> mapping = intentSetMultimap.getMapping(resource);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700232
Marc De Leenheer723f5532015-06-03 20:16:17 -0700233 if (mapping == null) {
234 return true;
235 }
236
Aaron Kruglikov6490a3b2015-06-05 14:25:32 -0700237 return mapping.size() < maxCapacity;
Marc De Leenheer723f5532015-06-03 20:16:17 -0700238 }
239
240 private boolean isAllowed(OpticalCircuitIntent circuitIntent, OpticalConnectivityIntent connIntent) {
241 ConnectPoint srcStaticPort = staticPort(circuitIntent.getSrc());
242 if (srcStaticPort != null) {
243 if (!srcStaticPort.equals(connIntent.getSrc())) {
244 return false;
245 }
246 }
247
248 ConnectPoint dstStaticPort = staticPort(circuitIntent.getDst());
249 if (dstStaticPort != null) {
250 if (!dstStaticPort.equals(connIntent.getDst())) {
251 return false;
252 }
253 }
254
255 return true;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700256 }
257
258 /**
259 * Returns existing and available optical connectivity intent that matches the given circuit intent.
260 *
261 * @param circuitIntent optical circuit intent
262 * @return existing optical connectivity intent, null otherwise.
263 */
264 private OpticalConnectivityIntent findOpticalConnectivityIntent(OpticalCircuitIntent circuitIntent) {
265 for (Intent intent : intentService.getIntents()) {
266 if (!(intent instanceof OpticalConnectivityIntent)) {
267 continue;
268 }
269
270 OpticalConnectivityIntent connIntent = (OpticalConnectivityIntent) intent;
271
272 ConnectPoint src = circuitIntent.getSrc();
273 ConnectPoint dst = circuitIntent.getDst();
Marc De Leenheer723f5532015-06-03 20:16:17 -0700274 // Ignore if the intents don't have identical src and dst devices
275 if (!src.deviceId().equals(connIntent.getSrc().deviceId()) &&
276 !dst.deviceId().equals(connIntent.getDst().deviceId())) {
277 continue;
278 }
279
280 if (!isAllowed(circuitIntent, connIntent)) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700281 continue;
282 }
283
Sho SHIMIZU0f9a68c2015-09-29 12:45:36 -0700284 if (isAvailable(connIntent.id())) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700285 return connIntent;
286 }
287 }
288
289 return null;
290 }
291
Marc De Leenheer723f5532015-06-03 20:16:17 -0700292 private ConnectPoint staticPort(ConnectPoint connectPoint) {
293 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
294
295 String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT);
296
297 // FIXME: need a better way to match the port
298 if (staticPort != null) {
299 for (Port p : deviceService.getPorts(connectPoint.deviceId())) {
300 if (staticPort.equals(p.number().name())) {
301 return new ConnectPoint(p.element().id(), p.number());
302 }
303 }
304 }
305
306 return null;
307 }
308
Sho SHIMIZUd5bdbf82015-09-29 12:46:09 -0700309 private OchPort findAvailableOchPort(ConnectPoint oduPort) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700310 // First see if the port mappings are constrained
311 ConnectPoint ochCP = staticPort(oduPort);
312
313 if (ochCP != null) {
314 OchPort ochPort = (OchPort) deviceService.getPort(ochCP.deviceId(), ochCP.port());
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700315 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800316 resourceService.getResourceAllocations(Resources.discrete(ochCP.deviceId(), ochCP.port()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800317 .stream()
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700318 .map(ResourceAllocation::consumer)
319 .filter(x -> x instanceof IntentId)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800320 .map(x -> (IntentId) x)
321 .findAny();
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700322
323 if (isAvailable(intentId.orElse(null))) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700324 return ochPort;
325 }
326 }
327
328 // No port constraints, so find any port that works
329 List<Port> ports = deviceService.getPorts(oduPort.deviceId());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700330
331 for (Port port : ports) {
332 if (!(port instanceof OchPort)) {
333 continue;
334 }
335
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700336 Optional<IntentId> intentId =
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800337 resourceService.getResourceAllocations(Resources.discrete(oduPort.deviceId(), port.number()).id())
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800338 .stream()
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700339 .map(ResourceAllocation::consumer)
340 .filter(x -> x instanceof IntentId)
Sho SHIMIZU6c9e33a2016-01-07 18:45:27 -0800341 .map(x -> (IntentId) x)
342 .findAny();
343
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700344 if (isAvailable(intentId.orElse(null))) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700345 return (OchPort) port;
346 }
347 }
348
349 return null;
350 }
351
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700352 private Pair<OchPort, OchPort> findPorts(OpticalCircuitIntent intent) {
353
Sho SHIMIZUd5bdbf82015-09-29 12:46:09 -0700354 OchPort srcPort = findAvailableOchPort(intent.getSrc());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700355 if (srcPort == null) {
356 return null;
357 }
358
Sho SHIMIZUd5bdbf82015-09-29 12:46:09 -0700359 OchPort dstPort = findAvailableOchPort(intent.getDst());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700360 if (dstPort == null) {
361 return null;
362 }
363
364 return Pair.of(srcPort, dstPort);
365 }
366
367 /**
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700368 * Builds flow rule for mapping between two ports.
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700369 *
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700370 * @param src source port
371 * @param dst destination port
372 * @return flow rules
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700373 */
Brian O'Connor81134662015-06-25 17:23:33 -0400374 private FlowRule connectPorts(ConnectPoint src, ConnectPoint dst, int priority) {
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700375 checkArgument(src.deviceId().equals(dst.deviceId()));
376
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700377 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
378 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
379
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700380 selectorBuilder.matchInPort(src.port());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700381 //selectorBuilder.add(Criteria.matchCltSignalType)
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700382 treatmentBuilder.setOutput(dst.port());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700383 //treatmentBuilder.add(Instructions.modL1OduSignalType)
384
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700385 FlowRule flowRule = DefaultFlowRule.builder()
386 .forDevice(src.deviceId())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700387 .withSelector(selectorBuilder.build())
388 .withTreatment(treatmentBuilder.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400389 .withPriority(priority)
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700390 .fromApp(appId)
391 .makePermanent()
392 .build();
393
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700394 return flowRule;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700395 }
396}