blob: fc62b257d3d92620777634f91f0c37bbab4c1a26 [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;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.onosproject.core.ApplicationId;
25import org.onosproject.core.CoreService;
26import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.OchPort;
29import org.onosproject.net.OduCltPort;
30import org.onosproject.net.OduSignalType;
31import org.onosproject.net.Port;
32import org.onosproject.net.device.DeviceService;
33import org.onosproject.net.flow.DefaultFlowRule;
34import org.onosproject.net.flow.DefaultTrafficSelector;
35import org.onosproject.net.flow.DefaultTrafficTreatment;
36import org.onosproject.net.flow.FlowRule;
37import org.onosproject.net.flow.TrafficSelector;
38import org.onosproject.net.flow.TrafficTreatment;
39import org.onosproject.net.intent.FlowRuleIntent;
40import org.onosproject.net.intent.Intent;
41import org.onosproject.net.intent.IntentCompiler;
42import org.onosproject.net.intent.IntentExtensionService;
43import org.onosproject.net.intent.IntentId;
44import org.onosproject.net.intent.IntentService;
45import org.onosproject.net.intent.OpticalCircuitIntent;
46import org.onosproject.net.intent.OpticalConnectivityIntent;
47import org.onosproject.net.intent.impl.IntentCompilationException;
48import org.onosproject.net.resource.device.DeviceResourceService;
49import org.onosproject.net.resource.link.LinkResourceAllocations;
50import org.slf4j.Logger;
51import org.slf4j.LoggerFactory;
52
53import java.util.Arrays;
54import java.util.Collections;
55import java.util.HashSet;
56import java.util.LinkedList;
57import java.util.List;
58import java.util.Set;
59
60import static com.google.common.base.Preconditions.checkArgument;
61
62/**
63 * An intent compiler for {@link org.onosproject.net.intent.OpticalCircuitIntent}.
64 */
65@Component(immediate = true)
66public class OpticalCircuitIntentCompiler implements IntentCompiler<OpticalCircuitIntent> {
67
68 private static final Logger log = LoggerFactory.getLogger(OpticalCircuitIntentCompiler.class);
69
70 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
71 protected IntentExtensionService intentManager;
72
73 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
74 protected CoreService coreService;
75
76 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
77 protected DeviceService deviceService;
78
79 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
80 protected DeviceResourceService deviceResourceService;
81
82 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
83 protected IntentService intentService;
84
85 private ApplicationId appId;
86
87 @Activate
88 public void activate() {
89 appId = coreService.registerApplication("org.onosproject.net.intent");
90 intentManager.registerCompiler(OpticalCircuitIntent.class, this);
91 }
92
93 @Deactivate
94 public void deactivate() {
95 intentManager.unregisterCompiler(OpticalCircuitIntent.class);
96 }
97
98 @Override
99 public List<Intent> compile(OpticalCircuitIntent intent, List<Intent> installable,
100 Set<LinkResourceAllocations> resources) {
101 // Check if ports are OduClt ports
102 ConnectPoint src = intent.getSrc();
103 ConnectPoint dst = intent.getDst();
104 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
105 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
106 checkArgument(srcPort instanceof OduCltPort);
107 checkArgument(dstPort instanceof OduCltPort);
108
109 log.debug("Compiling optical circuit intent between {} and {}", src, dst);
110
111 // Reserve OduClt ports
112 if (!deviceResourceService.requestPorts(new HashSet(Arrays.asList(srcPort, dstPort)), intent)) {
113 throw new IntentCompilationException("Unable to reserve ports for intent " + intent);
114 }
115
116 LinkedList<Intent> intents = new LinkedList<>();
117
118 FlowRuleIntent circuitIntent;
119 OpticalConnectivityIntent connIntent = findOpticalConnectivityIntent(intent);
120
121 // Create optical connectivity intent if needed
122 if (connIntent == null) {
123 // Find OCh ports with available resources
124 Pair<OchPort, OchPort> ochPorts = findPorts(intent);
125
126 if (ochPorts == null) {
127 return Collections.emptyList();
128 }
129
130 // Create optical connectivity intent
131 ConnectPoint srcCP = new ConnectPoint(src.elementId(), ochPorts.getLeft().number());
132 ConnectPoint dstCP = new ConnectPoint(dst.elementId(), ochPorts.getRight().number());
133 // FIXME: hardcoded ODU signal type
134 connIntent = OpticalConnectivityIntent.builder()
135 .appId(appId)
136 .src(srcCP)
137 .dst(dstCP)
138 .signalType(OduSignalType.ODU4)
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700139 .bidirectional(intent.isBidirectional())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700140 .build();
141 intents.add(connIntent);
142 }
143
144 // Create optical circuit intent
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700145 List<FlowRule> rules = new LinkedList<>();
146 rules.add(connectPorts(src, connIntent.getSrc()));
147 rules.add(connectPorts(connIntent.getDst(), dst));
148
149 // Create flow rules for reverse path
150 if (intent.isBidirectional()) {
151 rules.add(connectPorts(connIntent.getSrc(), src));
152 rules.add(connectPorts(dst, connIntent.getDst()));
153 }
154
155 circuitIntent = new FlowRuleIntent(appId, rules, intent.resources());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700156
157 // Save circuit to connectivity intent mapping
158 deviceResourceService.requestMapping(connIntent.id(), circuitIntent.id());
159 intents.add(circuitIntent);
160
161 return intents;
162 }
163
164 /**
165 * Checks if current allocations on given resource can satisfy request.
166 *
167 * @param request
168 * @param resource
169 * @return
170 */
171 private boolean isAvailable(Intent request, IntentId resource) {
172 Set<IntentId> mapping = deviceResourceService.getMapping(resource);
173
174 // TODO: hardcoded 10 x 10G
175 return mapping.size() < 10;
176 }
177
178 /**
179 * Returns existing and available optical connectivity intent that matches the given circuit intent.
180 *
181 * @param circuitIntent optical circuit intent
182 * @return existing optical connectivity intent, null otherwise.
183 */
184 private OpticalConnectivityIntent findOpticalConnectivityIntent(OpticalCircuitIntent circuitIntent) {
185 for (Intent intent : intentService.getIntents()) {
186 if (!(intent instanceof OpticalConnectivityIntent)) {
187 continue;
188 }
189
190 OpticalConnectivityIntent connIntent = (OpticalConnectivityIntent) intent;
191
192 ConnectPoint src = circuitIntent.getSrc();
193 ConnectPoint dst = circuitIntent.getDst();
194 if (!src.equals(connIntent.getSrc()) && !dst.equals(connIntent.getDst())) {
195 continue;
196 }
197
198 if (isAvailable(circuitIntent, connIntent.id())) {
199 return connIntent;
200 }
201 }
202
203 return null;
204 }
205
206 private OchPort findAvailableOchPort(DeviceId deviceId, OpticalCircuitIntent circuitIntent) {
207 List<Port> ports = deviceService.getPorts(deviceId);
208
209 for (Port port : ports) {
210 if (!(port instanceof OchPort)) {
211 continue;
212 }
213
214 // Port is not used
215 IntentId intentId = deviceResourceService.getAllocations(port);
216 if (intentId == null) {
217 return (OchPort) port;
218 }
219
220 // Port is used but has free resources
221 if (isAvailable(circuitIntent, intentId)) {
222 return (OchPort) port;
223 }
224 }
225
226 return null;
227 }
228
229 // TODO: Add constraints for OduClt to OCh port mappings
230 // E.g., ports need to belong to same line card.
231 private Pair<OchPort, OchPort> findPorts(OpticalCircuitIntent intent) {
232
233 OchPort srcPort = findAvailableOchPort(intent.getSrc().deviceId(), intent);
234 if (srcPort == null) {
235 return null;
236 }
237
238 OchPort dstPort = findAvailableOchPort(intent.getDst().deviceId(), intent);
239 if (dstPort == null) {
240 return null;
241 }
242
243 return Pair.of(srcPort, dstPort);
244 }
245
246 /**
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700247 * Builds flow rule for mapping between two ports.
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700248 *
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700249 * @param src source port
250 * @param dst destination port
251 * @return flow rules
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700252 */
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700253 private FlowRule connectPorts(ConnectPoint src, ConnectPoint dst) {
254 checkArgument(src.deviceId().equals(dst.deviceId()));
255
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700256 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
257 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
258
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700259 selectorBuilder.matchInPort(src.port());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700260 //selectorBuilder.add(Criteria.matchCltSignalType)
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700261 treatmentBuilder.setOutput(dst.port());
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700262 //treatmentBuilder.add(Instructions.modL1OduSignalType)
263
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700264 FlowRule flowRule = DefaultFlowRule.builder()
265 .forDevice(src.deviceId())
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700266 .withSelector(selectorBuilder.build())
267 .withTreatment(treatmentBuilder.build())
268 .withPriority(100)
269 .fromApp(appId)
270 .makePermanent()
271 .build();
272
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700273 return flowRule;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700274 }
275}