blob: 6fe8c1fbac509c6a7f3ddbf09a028d5a931d7d2d [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 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 */
weibitf32383b2014-10-22 10:17:31 -070016package org.onlab.onos.net.intent.impl;
17
18import static org.onlab.onos.net.flow.DefaultTrafficTreatment.builder;
19import static org.slf4j.LoggerFactory.getLogger;
20
weibitf32383b2014-10-22 10:17:31 -070021import java.util.List;
weibitf32383b2014-10-22 10:17:31 -070022
23import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
26import org.apache.felix.scr.annotations.Reference;
27import org.apache.felix.scr.annotations.ReferenceCardinality;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070028import org.onlab.onos.core.ApplicationId;
29import org.onlab.onos.core.CoreService;
weibitf32383b2014-10-22 10:17:31 -070030import org.onlab.onos.net.ConnectPoint;
31import org.onlab.onos.net.Link;
weibitf32383b2014-10-22 10:17:31 -070032import org.onlab.onos.net.flow.DefaultFlowRule;
33import org.onlab.onos.net.flow.DefaultTrafficSelector;
weibit7e583462014-10-23 10:14:05 -070034import org.onlab.onos.net.flow.DefaultTrafficTreatment;
weibitf32383b2014-10-22 10:17:31 -070035import org.onlab.onos.net.flow.FlowRule;
36import org.onlab.onos.net.flow.FlowRuleBatchEntry;
Brian O'Connor086724e2014-10-23 15:47:32 -070037import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
weibitf32383b2014-10-22 10:17:31 -070038import org.onlab.onos.net.flow.FlowRuleBatchOperation;
39import org.onlab.onos.net.flow.FlowRuleService;
40import org.onlab.onos.net.flow.TrafficSelector;
41import org.onlab.onos.net.flow.TrafficTreatment;
weibitf32383b2014-10-22 10:17:31 -070042import org.onlab.onos.net.intent.IntentExtensionService;
43import org.onlab.onos.net.intent.IntentInstaller;
44import org.onlab.onos.net.intent.OpticalPathIntent;
weibit9e622ac2014-10-23 13:45:44 -070045import org.onlab.onos.net.resource.DefaultLinkResourceRequest;
weibit7e583462014-10-23 10:14:05 -070046import org.onlab.onos.net.resource.Lambda;
weibit9e622ac2014-10-23 13:45:44 -070047import org.onlab.onos.net.resource.LambdaResourceAllocation;
weibit7e583462014-10-23 10:14:05 -070048import org.onlab.onos.net.resource.LinkResourceAllocations;
49import org.onlab.onos.net.resource.LinkResourceRequest;
50import org.onlab.onos.net.resource.LinkResourceService;
weibit9e622ac2014-10-23 13:45:44 -070051import org.onlab.onos.net.resource.ResourceAllocation;
52import org.onlab.onos.net.resource.ResourceType;
weibit7e583462014-10-23 10:14:05 -070053import org.onlab.onos.net.topology.TopologyService;
weibitf32383b2014-10-22 10:17:31 -070054import org.slf4j.Logger;
55
56import com.google.common.collect.Lists;
57
58/**
59 * OpticaliIntentInstaller for optical path intents.
60 * It essentially generates optical FlowRules and
61 * call the flowRule service to execute them.
62 */
63
64@Component(immediate = true)
65public class OpticalPathIntentInstaller implements IntentInstaller<OpticalPathIntent> {
weibitf32383b2014-10-22 10:17:31 -070066 private final Logger log = getLogger(getClass());
67
68 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
69 protected IntentExtensionService intentManager;
70
71 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
72 protected FlowRuleService flowRuleService;
73
74 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
75 protected CoreService coreService;
76
weibit7e583462014-10-23 10:14:05 -070077 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
78 protected TopologyService topologyService;
79
80 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
81 protected LinkResourceService resourceService;
82
weibitf32383b2014-10-22 10:17:31 -070083 private ApplicationId appId;
84
weibit9e622ac2014-10-23 13:45:44 -070085 //final short WAVELENGTH = 80;
weibit7e583462014-10-23 10:14:05 -070086
weibitf32383b2014-10-22 10:17:31 -070087 @Activate
88 public void activate() {
89 appId = coreService.registerApplication("org.onlab.onos.net.intent");
90 intentManager.registerInstaller(OpticalPathIntent.class, this);
91 }
92
93 @Deactivate
94 public void deactivate() {
95 intentManager.unregisterInstaller(OpticalPathIntent.class);
96 }
97
98 @Override
weibit7e583462014-10-23 10:14:05 -070099 public List<FlowRuleBatchOperation> install(OpticalPathIntent intent) {
weibit9e622ac2014-10-23 13:45:44 -0700100 LinkResourceAllocations allocations = assignWavelength(intent);
weibitf32383b2014-10-22 10:17:31 -0700101
weibit9e622ac2014-10-23 13:45:44 -0700102 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Brian O'Connor086724e2014-10-23 15:47:32 -0700103 selectorBuilder.matchInport(intent.src().port());
weibitf32383b2014-10-22 10:17:31 -0700104
weibitf32383b2014-10-22 10:17:31 -0700105 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
Brian O'Connor086724e2014-10-23 15:47:32 -0700106 ConnectPoint prev = intent.src();
weibitf32383b2014-10-22 10:17:31 -0700107
weibit9e622ac2014-10-23 13:45:44 -0700108 //TODO throw exception if the lambda was not assigned successfully
109 for (Link link : intent.path().links()) {
110 Lambda la = null;
111 for (ResourceAllocation allocation : allocations.getResourceAllocation(link)) {
112 if (allocation.type() == ResourceType.LAMBDA) {
113 la = ((LambdaResourceAllocation) allocation).lambda();
114 break;
115 }
116 }
117
118 if (la == null) {
119 log.info("Lambda was not assigned successfully");
120 return null;
121 }
122
weibit253c8652014-10-23 16:30:03 -0700123 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
weibit9e622ac2014-10-23 13:45:44 -0700124 treatmentBuilder.setOutput(link.src().port());
weibit2543d5a2014-10-23 14:13:35 -0700125 treatmentBuilder.setLambda((short) la.toInt());
weibit9e622ac2014-10-23 13:45:44 -0700126
127 FlowRule rule = new DefaultFlowRule(prev.deviceId(),
128 selectorBuilder.build(),
129 treatmentBuilder.build(),
weibitf32383b2014-10-22 10:17:31 -0700130 100,
131 appId,
weibit7e583462014-10-23 10:14:05 -0700132 100,
133 true);
weibitaca14602014-10-24 10:26:26 -0700134
weibit7e583462014-10-23 10:14:05 -0700135 rules.add(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule));
weibit9e622ac2014-10-23 13:45:44 -0700136
weibit7e583462014-10-23 10:14:05 -0700137 prev = link.dst();
weibit9e622ac2014-10-23 13:45:44 -0700138 selectorBuilder.matchInport(link.dst().port());
weibit2543d5a2014-10-23 14:13:35 -0700139 selectorBuilder.matchLambda((short) la.toInt());
weibit7e583462014-10-23 10:14:05 -0700140 }
weibit9e622ac2014-10-23 13:45:44 -0700141
142 // build the last T port rule
143 TrafficTreatment treatmentLast = builder()
Brian O'Connor086724e2014-10-23 15:47:32 -0700144 .setOutput(intent.dst().port()).build();
145 FlowRule rule = new DefaultFlowRule(intent.dst().deviceId(),
weibit9e622ac2014-10-23 13:45:44 -0700146 selectorBuilder.build(),
147 treatmentLast,
148 100,
149 appId,
150 100,
151 true);
152 rules.add(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule));
153
weibit7e583462014-10-23 10:14:05 -0700154 return Lists.newArrayList(new FlowRuleBatchOperation(rules));
155 }
weibitf32383b2014-10-22 10:17:31 -0700156
weibit9e622ac2014-10-23 13:45:44 -0700157 private LinkResourceAllocations assignWavelength(OpticalPathIntent intent) {
158 LinkResourceRequest.Builder request = DefaultLinkResourceRequest.builder(intent.id(),
159 intent.path().links())
160 .addLambdaRequest();
161 LinkResourceAllocations retLambda = resourceService.requestResources(request.build());
162 return retLambda;
163 }
164
165 /*private Lambda assignWavelength(List<Link> links) {
weibit7e583462014-10-23 10:14:05 -0700166 // TODO More wavelength assignment algorithm
167 int wavenum = 0;
168 Iterator<Link> itrlink = links.iterator();
169 for (int i = 1; i <= WAVELENGTH; i++) {
170 wavenum = i;
171 boolean found = true;
172 while (itrlink.hasNext()) {
173 Link link = itrlink.next();
174 if (isWavelengthUsed(link, i)) {
175 found = false;
176 break;
177 }
178 }
179 // First-Fit wavelength assignment algorithm
180 if (found) {
181 break;
182 }
183 }
184
185 if (wavenum == 0) {
186 return null;
187 }
188
189 Lambda wave = Lambda.valueOf(wavenum);
190 return wave;
191 }
192
193 private boolean isWavelengthUsed(Link link, int i) {
194 Iterable<LinkResourceAllocations> wave = resourceService.getAllocations(link);
195 for (LinkResourceAllocations ir : wave) {
196 //if ir.resources().contains(i) {
197 //}
198 }
199 return false;
weibit9e622ac2014-10-23 13:45:44 -0700200 }*/
weibit7e583462014-10-23 10:14:05 -0700201
202 @Override
203 public List<FlowRuleBatchOperation> uninstall(OpticalPathIntent intent) {
Toshio Koide86160f52014-10-23 14:59:07 -0700204 LinkResourceAllocations allocations = resourceService.getAllocations(intent.id());
weibit9e622ac2014-10-23 13:45:44 -0700205
206 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Brian O'Connor086724e2014-10-23 15:47:32 -0700207 selectorBuilder.matchInport(intent.src().port());
weibit9e622ac2014-10-23 13:45:44 -0700208
209 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
210
weibit7e583462014-10-23 10:14:05 -0700211 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
Brian O'Connor086724e2014-10-23 15:47:32 -0700212 ConnectPoint prev = intent.src();
weibit9e622ac2014-10-23 13:45:44 -0700213
214 //TODO throw exception if the lambda was not retrieved successfully
215 for (Link link : intent.path().links()) {
216 Lambda la = null;
217 for (ResourceAllocation allocation : allocations.getResourceAllocation(link)) {
218 if (allocation.type() == ResourceType.LAMBDA) {
219 la = ((LambdaResourceAllocation) allocation).lambda();
220 break;
221 }
222 }
223
224 if (la == null) {
225 log.info("Lambda was not retrieved successfully");
226 return null;
227 }
228
229 treatmentBuilder.setOutput(link.src().port());
weibit2543d5a2014-10-23 14:13:35 -0700230 treatmentBuilder.setLambda((short) la.toInt());
weibit9e622ac2014-10-23 13:45:44 -0700231
232 FlowRule rule = new DefaultFlowRule(prev.deviceId(),
233 selectorBuilder.build(),
234 treatmentBuilder.build(),
weibit7e583462014-10-23 10:14:05 -0700235 100,
236 appId,
237 100,
238 true);
weibitf32383b2014-10-22 10:17:31 -0700239 rules.add(new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, rule));
weibit9e622ac2014-10-23 13:45:44 -0700240
weibitf32383b2014-10-22 10:17:31 -0700241 prev = link.dst();
weibit9e622ac2014-10-23 13:45:44 -0700242 selectorBuilder.matchInport(link.dst().port());
weibit2543d5a2014-10-23 14:13:35 -0700243 selectorBuilder.matchLambda((short) la.toInt());
weibitf32383b2014-10-22 10:17:31 -0700244 }
weibit9e622ac2014-10-23 13:45:44 -0700245
246 // build the last T port rule
247 TrafficTreatment treatmentLast = builder()
Brian O'Connor086724e2014-10-23 15:47:32 -0700248 .setOutput(intent.dst().port()).build();
249 FlowRule rule = new DefaultFlowRule(intent.dst().deviceId(),
weibit9e622ac2014-10-23 13:45:44 -0700250 selectorBuilder.build(),
251 treatmentLast,
252 100,
253 appId,
254 100,
255 true);
256 rules.add(new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, rule));
257
weibit7e583462014-10-23 10:14:05 -0700258 return Lists.newArrayList(new FlowRuleBatchOperation(rules));
weibitf32383b2014-10-22 10:17:31 -0700259 }
260
261}