2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.net.intent.impl.compiler;
18 import com.google.common.collect.Sets;
19 import org.apache.commons.lang3.tuple.Pair;
20 import org.apache.felix.scr.annotations.Activate;
21 import org.apache.felix.scr.annotations.Component;
22 import org.apache.felix.scr.annotations.Deactivate;
23 import org.apache.felix.scr.annotations.Modified;
24 import org.apache.felix.scr.annotations.Property;
25 import org.apache.felix.scr.annotations.Reference;
26 import org.apache.felix.scr.annotations.ReferenceCardinality;
27 import org.onlab.util.Tools;
28 import org.onosproject.cfg.ComponentConfigService;
29 import org.onosproject.core.ApplicationId;
30 import org.onosproject.core.CoreService;
31 import org.onosproject.net.AnnotationKeys;
32 import org.onosproject.net.ConnectPoint;
33 import org.onosproject.net.OchPort;
34 import org.onosproject.net.OduCltPort;
35 import org.onosproject.net.OduSignalType;
36 import org.onosproject.net.Port;
37 import org.onosproject.net.device.DeviceService;
38 import org.onosproject.net.flow.DefaultFlowRule;
39 import org.onosproject.net.flow.DefaultTrafficSelector;
40 import org.onosproject.net.flow.DefaultTrafficTreatment;
41 import org.onosproject.net.flow.FlowRule;
42 import org.onosproject.net.flow.TrafficSelector;
43 import org.onosproject.net.flow.TrafficTreatment;
44 import org.onosproject.net.intent.FlowRuleIntent;
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.IntentId;
49 import org.onosproject.net.intent.IntentService;
50 import org.onosproject.net.intent.OpticalCircuitIntent;
51 import org.onosproject.net.intent.OpticalConnectivityIntent;
52 import org.onosproject.net.intent.impl.IntentCompilationException;
53 import org.onosproject.net.resource.device.DeviceResourceService;
54 import org.onosproject.net.resource.link.LinkResourceAllocations;
55 import org.osgi.service.component.ComponentContext;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
59 import java.util.Collections;
60 import java.util.Dictionary;
61 import java.util.LinkedList;
62 import java.util.List;
65 import static com.google.common.base.Preconditions.checkArgument;
68 * An intent compiler for {@link org.onosproject.net.intent.OpticalCircuitIntent}.
70 @Component(immediate = true)
71 public class OpticalCircuitIntentCompiler implements IntentCompiler<OpticalCircuitIntent> {
73 private static final Logger log = LoggerFactory.getLogger(OpticalCircuitIntentCompiler.class);
75 private static final int DEFAULT_MAX_CAPACITY = 10;
77 @Property(name = "maxCapacity", intValue = DEFAULT_MAX_CAPACITY,
78 label = "Maximum utilization of an optical connection.")
80 private int maxCapacity = DEFAULT_MAX_CAPACITY;
82 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
83 protected ComponentConfigService cfgService;
85 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
86 protected IntentExtensionService intentManager;
88 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
89 protected CoreService coreService;
91 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
92 protected DeviceService deviceService;
94 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
95 protected DeviceResourceService deviceResourceService;
97 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
98 protected IntentService intentService;
100 private ApplicationId appId;
103 public void modified(ComponentContext context) {
104 Dictionary properties = context.getProperties();
106 //TODO for reduction check if the new capacity is smaller than the size of the current mapping
107 String propertyString = Tools.get(properties, "maxCapacity");
109 //Ignore if propertyString is empty
110 if (!propertyString.isEmpty()) {
112 int temp = Integer.parseInt(propertyString);
113 //Ensure value is non-negative but allow zero as a way to shutdown the link
117 } catch (NumberFormatException e) {
118 //Malformed arguments lead to no change of value (user should be notified of error)
119 log.error("The value '{}' for maxCapacity was not parsable as an integer.", propertyString, e);
122 //Notify of empty value but do not return (other properties will also go in this function)
123 log.error("The value for maxCapacity was set to an empty value.");
129 public void activate(ComponentContext context) {
130 appId = coreService.registerApplication("org.onosproject.net.intent");
131 intentManager.registerCompiler(OpticalCircuitIntent.class, this);
132 cfgService.registerProperties(getClass());
137 public void deactivate() {
138 intentManager.unregisterCompiler(OpticalCircuitIntent.class);
139 cfgService.unregisterProperties(getClass(), false);
143 public List<Intent> compile(OpticalCircuitIntent intent, List<Intent> installable,
144 Set<LinkResourceAllocations> resources) {
145 // Check if ports are OduClt ports
146 ConnectPoint src = intent.getSrc();
147 ConnectPoint dst = intent.getDst();
148 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
149 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
150 checkArgument(srcPort instanceof OduCltPort);
151 checkArgument(dstPort instanceof OduCltPort);
153 log.debug("Compiling optical circuit intent between {} and {}", src, dst);
155 // Reserve OduClt ports
156 if (!deviceResourceService.requestPorts(Sets.newHashSet(srcPort, dstPort), intent)) {
157 throw new IntentCompilationException("Unable to reserve ports for intent " + intent);
160 LinkedList<Intent> intents = new LinkedList<>();
162 FlowRuleIntent circuitIntent;
163 OpticalConnectivityIntent connIntent = findOpticalConnectivityIntent(intent);
165 // Create optical connectivity intent if needed
166 if (connIntent == null) {
167 // Find OCh ports with available resources
168 Pair<OchPort, OchPort> ochPorts = findPorts(intent);
170 if (ochPorts == null) {
171 return Collections.emptyList();
174 // Create optical connectivity intent
175 ConnectPoint srcCP = new ConnectPoint(src.elementId(), ochPorts.getLeft().number());
176 ConnectPoint dstCP = new ConnectPoint(dst.elementId(), ochPorts.getRight().number());
177 // FIXME: hardcoded ODU signal type
178 connIntent = OpticalConnectivityIntent.builder()
182 .signalType(OduSignalType.ODU4)
183 .bidirectional(intent.isBidirectional())
185 intentService.submit(connIntent);
188 // Create optical circuit intent
189 List<FlowRule> rules = new LinkedList<>();
190 rules.add(connectPorts(src, connIntent.getSrc(), intent.priority()));
191 rules.add(connectPorts(connIntent.getDst(), dst, intent.priority()));
193 // Create flow rules for reverse path
194 if (intent.isBidirectional()) {
195 rules.add(connectPorts(connIntent.getSrc(), src, intent.priority()));
196 rules.add(connectPorts(dst, connIntent.getDst(), intent.priority()));
199 circuitIntent = new FlowRuleIntent(appId, rules, intent.resources());
201 // Save circuit to connectivity intent mapping
202 deviceResourceService.requestMapping(connIntent.id(), intent.id());
203 intents.add(circuitIntent);
209 * Checks if current allocations on given resource can satisfy request.
210 * If the resource is null, return true.
212 * @param request the intent making the request
213 * @param resource the resource on which to map the intent
214 * @return true if the resource can accept the request, false otherwise
216 private boolean isAvailable(Intent request, IntentId resource) {
217 if (resource == null) {
221 Set<IntentId> mapping = deviceResourceService.getMapping(resource);
223 if (mapping == null) {
227 return mapping.size() < maxCapacity;
230 private boolean isAllowed(OpticalCircuitIntent circuitIntent, OpticalConnectivityIntent connIntent) {
231 ConnectPoint srcStaticPort = staticPort(circuitIntent.getSrc());
232 if (srcStaticPort != null) {
233 if (!srcStaticPort.equals(connIntent.getSrc())) {
238 ConnectPoint dstStaticPort = staticPort(circuitIntent.getDst());
239 if (dstStaticPort != null) {
240 if (!dstStaticPort.equals(connIntent.getDst())) {
249 * Returns existing and available optical connectivity intent that matches the given circuit intent.
251 * @param circuitIntent optical circuit intent
252 * @return existing optical connectivity intent, null otherwise.
254 private OpticalConnectivityIntent findOpticalConnectivityIntent(OpticalCircuitIntent circuitIntent) {
255 for (Intent intent : intentService.getIntents()) {
256 if (!(intent instanceof OpticalConnectivityIntent)) {
260 OpticalConnectivityIntent connIntent = (OpticalConnectivityIntent) intent;
262 ConnectPoint src = circuitIntent.getSrc();
263 ConnectPoint dst = circuitIntent.getDst();
264 // Ignore if the intents don't have identical src and dst devices
265 if (!src.deviceId().equals(connIntent.getSrc().deviceId()) &&
266 !dst.deviceId().equals(connIntent.getDst().deviceId())) {
270 if (!isAllowed(circuitIntent, connIntent)) {
274 if (isAvailable(circuitIntent, connIntent.id())) {
282 private ConnectPoint staticPort(ConnectPoint connectPoint) {
283 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
285 String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT);
287 // FIXME: need a better way to match the port
288 if (staticPort != null) {
289 for (Port p : deviceService.getPorts(connectPoint.deviceId())) {
290 if (staticPort.equals(p.number().name())) {
291 return new ConnectPoint(p.element().id(), p.number());
299 private OchPort findAvailableOchPort(ConnectPoint oduPort, OpticalCircuitIntent circuitIntent) {
300 // First see if the port mappings are constrained
301 ConnectPoint ochCP = staticPort(oduPort);
304 OchPort ochPort = (OchPort) deviceService.getPort(ochCP.deviceId(), ochCP.port());
305 IntentId intentId = deviceResourceService.getAllocations(ochPort);
306 if (isAvailable(circuitIntent, intentId)) {
311 // No port constraints, so find any port that works
312 List<Port> ports = deviceService.getPorts(oduPort.deviceId());
314 for (Port port : ports) {
315 if (!(port instanceof OchPort)) {
319 IntentId intentId = deviceResourceService.getAllocations(port);
320 if (isAvailable(circuitIntent, intentId)) {
321 return (OchPort) port;
328 private Pair<OchPort, OchPort> findPorts(OpticalCircuitIntent intent) {
330 OchPort srcPort = findAvailableOchPort(intent.getSrc(), intent);
331 if (srcPort == null) {
335 OchPort dstPort = findAvailableOchPort(intent.getDst(), intent);
336 if (dstPort == null) {
340 return Pair.of(srcPort, dstPort);
344 * Builds flow rule for mapping between two ports.
346 * @param src source port
347 * @param dst destination port
350 private FlowRule connectPorts(ConnectPoint src, ConnectPoint dst, int priority) {
351 checkArgument(src.deviceId().equals(dst.deviceId()));
353 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
354 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
356 selectorBuilder.matchInPort(src.port());
357 //selectorBuilder.add(Criteria.matchCltSignalType)
358 treatmentBuilder.setOutput(dst.port());
359 //treatmentBuilder.add(Instructions.modL1OduSignalType)
361 FlowRule flowRule = DefaultFlowRule.builder()
362 .forDevice(src.deviceId())
363 .withSelector(selectorBuilder.build())
364 .withTreatment(treatmentBuilder.build())
365 .withPriority(priority)