blob: 1e2b97c542bab02c728747567fbdc9c35fb9aed4 [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent.impl;
weibitf32383b2014-10-22 10:17:31 -070017
Thomas Vachuska425a2d72014-10-29 11:28:28 -070018import com.google.common.collect.Lists;
weibitf32383b2014-10-22 10:17:31 -070019import 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;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.core.ApplicationId;
25import org.onosproject.core.CoreService;
26import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.Link;
28import org.onosproject.net.flow.DefaultFlowRule;
29import org.onosproject.net.flow.DefaultTrafficSelector;
30import org.onosproject.net.flow.DefaultTrafficTreatment;
31import org.onosproject.net.flow.FlowRule;
32import org.onosproject.net.flow.FlowRuleBatchEntry;
33import org.onosproject.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
34import org.onosproject.net.flow.FlowRuleBatchOperation;
35import org.onosproject.net.flow.FlowRuleService;
36import org.onosproject.net.flow.TrafficSelector;
37import org.onosproject.net.flow.TrafficTreatment;
38import org.onosproject.net.intent.IntentExtensionService;
39import org.onosproject.net.intent.IntentInstaller;
40import org.onosproject.net.intent.OpticalPathIntent;
41import org.onosproject.net.resource.DefaultLinkResourceRequest;
42import org.onosproject.net.resource.Lambda;
43import org.onosproject.net.resource.LambdaResourceAllocation;
44import org.onosproject.net.resource.LinkResourceAllocations;
45import org.onosproject.net.resource.LinkResourceRequest;
46import org.onosproject.net.resource.LinkResourceService;
47import org.onosproject.net.resource.ResourceAllocation;
48import org.onosproject.net.resource.ResourceType;
49import org.onosproject.net.topology.TopologyService;
weibitf32383b2014-10-22 10:17:31 -070050import org.slf4j.Logger;
51
Thomas Vachuska425a2d72014-10-29 11:28:28 -070052import java.util.List;
53
Brian O'Connorabafb502014-12-02 22:26:20 -080054import static org.onosproject.net.flow.DefaultTrafficTreatment.builder;
Thomas Vachuska425a2d72014-10-29 11:28:28 -070055import static org.slf4j.LoggerFactory.getLogger;
weibitf32383b2014-10-22 10:17:31 -070056
57/**
Brian O'Connorabafb502014-12-02 22:26:20 -080058 * Installer for {@link org.onosproject.net.intent.OpticalPathIntent optical path connectivity intents}.
weibitf32383b2014-10-22 10:17:31 -070059 */
weibitf32383b2014-10-22 10:17:31 -070060@Component(immediate = true)
61public class OpticalPathIntentInstaller implements IntentInstaller<OpticalPathIntent> {
weibitf32383b2014-10-22 10:17:31 -070062 private final Logger log = getLogger(getClass());
63
64 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 protected IntentExtensionService intentManager;
66
67 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
68 protected FlowRuleService flowRuleService;
69
70 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
71 protected CoreService coreService;
72
weibit7e583462014-10-23 10:14:05 -070073 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
74 protected TopologyService topologyService;
75
76 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
77 protected LinkResourceService resourceService;
78
weibitf32383b2014-10-22 10:17:31 -070079 private ApplicationId appId;
80
weibit9e622ac2014-10-23 13:45:44 -070081 //final short WAVELENGTH = 80;
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -080082 static final short SIGNAL_TYPE = (short) 1;
weibit7e583462014-10-23 10:14:05 -070083
weibitf32383b2014-10-22 10:17:31 -070084 @Activate
85 public void activate() {
Brian O'Connorabafb502014-12-02 22:26:20 -080086 appId = coreService.registerApplication("org.onosproject.net.intent");
weibitf32383b2014-10-22 10:17:31 -070087 intentManager.registerInstaller(OpticalPathIntent.class, this);
88 }
89
90 @Deactivate
91 public void deactivate() {
92 intentManager.unregisterInstaller(OpticalPathIntent.class);
93 }
94
95 @Override
weibit7e583462014-10-23 10:14:05 -070096 public List<FlowRuleBatchOperation> install(OpticalPathIntent intent) {
weibit9e622ac2014-10-23 13:45:44 -070097 LinkResourceAllocations allocations = assignWavelength(intent);
Brian O'Connor41718fc2014-10-30 16:57:21 -070098 return generateRules(intent, allocations, FlowRuleOperation.ADD);
99 }
weibitf32383b2014-10-22 10:17:31 -0700100
Brian O'Connor41718fc2014-10-30 16:57:21 -0700101 @Override
102 public List<FlowRuleBatchOperation> uninstall(OpticalPathIntent intent) {
103 LinkResourceAllocations allocations = resourceService.getAllocations(intent.id());
Brian O'Connor772852a2014-11-17 15:51:19 -0800104 List<FlowRuleBatchOperation> rules = generateRules(intent, allocations, FlowRuleOperation.REMOVE);
105 log.info("uninstall rules: {}", rules);
106 return rules;
Brian O'Connor41718fc2014-10-30 16:57:21 -0700107 }
108
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700109 @Override
Brian O'Connor772852a2014-11-17 15:51:19 -0800110 public List<FlowRuleBatchOperation> replace(OpticalPathIntent oldIntent,
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700111 OpticalPathIntent newIntent) {
112 // FIXME: implement this
Brian O'Connor772852a2014-11-17 15:51:19 -0800113 List<FlowRuleBatchOperation> batches = Lists.newArrayList();
114 batches.addAll(uninstall(oldIntent));
115 batches.addAll(install(newIntent));
116 return batches;
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700117 }
118
Brian O'Connor41718fc2014-10-30 16:57:21 -0700119 private LinkResourceAllocations assignWavelength(OpticalPathIntent intent) {
120 LinkResourceRequest.Builder request = DefaultLinkResourceRequest.builder(intent.id(),
121 intent.path().links())
122 .addLambdaRequest();
123 LinkResourceAllocations retLambda = resourceService.requestResources(request.build());
124 return retLambda;
125 }
126
127 private List<FlowRuleBatchOperation> generateRules(OpticalPathIntent intent,
128 LinkResourceAllocations allocations,
129 FlowRuleOperation operation) {
weibit9e622ac2014-10-23 13:45:44 -0700130 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Brian O'Connor086724e2014-10-23 15:47:32 -0700131 selectorBuilder.matchInport(intent.src().port());
weibitf32383b2014-10-22 10:17:31 -0700132
weibitf32383b2014-10-22 10:17:31 -0700133 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
Brian O'Connor086724e2014-10-23 15:47:32 -0700134 ConnectPoint prev = intent.src();
weibitf32383b2014-10-22 10:17:31 -0700135
Brian O'Connor8b6b1722014-12-08 01:28:50 -0800136 //FIXME check for null allocations
weibit9e622ac2014-10-23 13:45:44 -0700137 //TODO throw exception if the lambda was not assigned successfully
138 for (Link link : intent.path().links()) {
139 Lambda la = null;
140 for (ResourceAllocation allocation : allocations.getResourceAllocation(link)) {
141 if (allocation.type() == ResourceType.LAMBDA) {
142 la = ((LambdaResourceAllocation) allocation).lambda();
143 break;
144 }
145 }
146
147 if (la == null) {
148 log.info("Lambda was not assigned successfully");
149 return null;
150 }
151
weibit253c8652014-10-23 16:30:03 -0700152 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
weibit9e622ac2014-10-23 13:45:44 -0700153 treatmentBuilder.setOutput(link.src().port());
weibit2543d5a2014-10-23 14:13:35 -0700154 treatmentBuilder.setLambda((short) la.toInt());
weibit9e622ac2014-10-23 13:45:44 -0700155
156 FlowRule rule = new DefaultFlowRule(prev.deviceId(),
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700157 selectorBuilder.build(),
158 treatmentBuilder.build(),
159 100,
160 appId,
161 100,
162 true);
weibitaca14602014-10-24 10:26:26 -0700163
Brian O'Connor41718fc2014-10-30 16:57:21 -0700164 rules.add(new FlowRuleBatchEntry(operation, rule));
weibit9e622ac2014-10-23 13:45:44 -0700165
weibit7e583462014-10-23 10:14:05 -0700166 prev = link.dst();
weibit9e622ac2014-10-23 13:45:44 -0700167 selectorBuilder.matchInport(link.dst().port());
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800168 selectorBuilder.matchOpticalSignalType(SIGNAL_TYPE); //todo
weibit2543d5a2014-10-23 14:13:35 -0700169 selectorBuilder.matchLambda((short) la.toInt());
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800170
weibit7e583462014-10-23 10:14:05 -0700171 }
weibit9e622ac2014-10-23 13:45:44 -0700172
173 // build the last T port rule
Brian O'Connor41718fc2014-10-30 16:57:21 -0700174 TrafficTreatment.Builder treatmentLast = builder();
175 treatmentLast.setOutput(intent.dst().port());
Brian O'Connor086724e2014-10-23 15:47:32 -0700176 FlowRule rule = new DefaultFlowRule(intent.dst().deviceId(),
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700177 selectorBuilder.build(),
Brian O'Connor41718fc2014-10-30 16:57:21 -0700178 treatmentLast.build(),
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700179 100,
180 appId,
181 100,
182 true);
Brian O'Connor41718fc2014-10-30 16:57:21 -0700183 rules.add(new FlowRuleBatchEntry(operation, rule));
weibit9e622ac2014-10-23 13:45:44 -0700184
weibit7e583462014-10-23 10:14:05 -0700185 return Lists.newArrayList(new FlowRuleBatchOperation(rules));
186 }
weibitf32383b2014-10-22 10:17:31 -0700187
weibit9e622ac2014-10-23 13:45:44 -0700188 /*private Lambda assignWavelength(List<Link> links) {
weibit7e583462014-10-23 10:14:05 -0700189 // TODO More wavelength assignment algorithm
190 int wavenum = 0;
191 Iterator<Link> itrlink = links.iterator();
192 for (int i = 1; i <= WAVELENGTH; i++) {
193 wavenum = i;
194 boolean found = true;
195 while (itrlink.hasNext()) {
196 Link link = itrlink.next();
197 if (isWavelengthUsed(link, i)) {
198 found = false;
199 break;
200 }
201 }
202 // First-Fit wavelength assignment algorithm
203 if (found) {
204 break;
205 }
206 }
207
208 if (wavenum == 0) {
209 return null;
210 }
211
212 Lambda wave = Lambda.valueOf(wavenum);
213 return wave;
214 }
215
216 private boolean isWavelengthUsed(Link link, int i) {
217 Iterable<LinkResourceAllocations> wave = resourceService.getAllocations(link);
218 for (LinkResourceAllocations ir : wave) {
219 //if ir.resources().contains(i) {
220 //}
221 }
222 return false;
weibit9e622ac2014-10-23 13:45:44 -0700223 }*/
weibitf32383b2014-10-22 10:17:31 -0700224}