blob: c102a6ee978387f49ae0b30f88d12c857389585c [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
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;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070024import org.onlab.onos.core.ApplicationId;
25import org.onlab.onos.core.CoreService;
weibitf32383b2014-10-22 10:17:31 -070026import org.onlab.onos.net.ConnectPoint;
27import org.onlab.onos.net.Link;
weibitf32383b2014-10-22 10:17:31 -070028import org.onlab.onos.net.flow.DefaultFlowRule;
29import org.onlab.onos.net.flow.DefaultTrafficSelector;
weibit7e583462014-10-23 10:14:05 -070030import org.onlab.onos.net.flow.DefaultTrafficTreatment;
weibitf32383b2014-10-22 10:17:31 -070031import org.onlab.onos.net.flow.FlowRule;
32import org.onlab.onos.net.flow.FlowRuleBatchEntry;
Brian O'Connor086724e2014-10-23 15:47:32 -070033import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
weibitf32383b2014-10-22 10:17:31 -070034import org.onlab.onos.net.flow.FlowRuleBatchOperation;
35import org.onlab.onos.net.flow.FlowRuleService;
36import org.onlab.onos.net.flow.TrafficSelector;
37import org.onlab.onos.net.flow.TrafficTreatment;
weibitf32383b2014-10-22 10:17:31 -070038import org.onlab.onos.net.intent.IntentExtensionService;
39import org.onlab.onos.net.intent.IntentInstaller;
40import org.onlab.onos.net.intent.OpticalPathIntent;
weibit9e622ac2014-10-23 13:45:44 -070041import org.onlab.onos.net.resource.DefaultLinkResourceRequest;
weibit7e583462014-10-23 10:14:05 -070042import org.onlab.onos.net.resource.Lambda;
weibit9e622ac2014-10-23 13:45:44 -070043import org.onlab.onos.net.resource.LambdaResourceAllocation;
weibit7e583462014-10-23 10:14:05 -070044import org.onlab.onos.net.resource.LinkResourceAllocations;
45import org.onlab.onos.net.resource.LinkResourceRequest;
46import org.onlab.onos.net.resource.LinkResourceService;
weibit9e622ac2014-10-23 13:45:44 -070047import org.onlab.onos.net.resource.ResourceAllocation;
48import org.onlab.onos.net.resource.ResourceType;
weibit7e583462014-10-23 10:14:05 -070049import org.onlab.onos.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
54import static org.onlab.onos.net.flow.DefaultTrafficTreatment.builder;
55import static org.slf4j.LoggerFactory.getLogger;
weibitf32383b2014-10-22 10:17:31 -070056
57/**
Thomas Vachuska425a2d72014-10-29 11:28:28 -070058 * Installer for {@link org.onlab.onos.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() {
86 appId = coreService.registerApplication("org.onlab.onos.net.intent");
87 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());
104 return generateRules(intent, allocations, FlowRuleOperation.REMOVE);
105 }
106
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700107 @Override
108 public List<FlowRuleBatchOperation> replace(OpticalPathIntent intent,
109 OpticalPathIntent newIntent) {
110 // FIXME: implement this
111 return null;
112 }
113
Brian O'Connor41718fc2014-10-30 16:57:21 -0700114 private LinkResourceAllocations assignWavelength(OpticalPathIntent intent) {
115 LinkResourceRequest.Builder request = DefaultLinkResourceRequest.builder(intent.id(),
116 intent.path().links())
117 .addLambdaRequest();
118 LinkResourceAllocations retLambda = resourceService.requestResources(request.build());
119 return retLambda;
120 }
121
122 private List<FlowRuleBatchOperation> generateRules(OpticalPathIntent intent,
123 LinkResourceAllocations allocations,
124 FlowRuleOperation operation) {
weibit9e622ac2014-10-23 13:45:44 -0700125 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Brian O'Connor086724e2014-10-23 15:47:32 -0700126 selectorBuilder.matchInport(intent.src().port());
weibitf32383b2014-10-22 10:17:31 -0700127
weibitf32383b2014-10-22 10:17:31 -0700128 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
Brian O'Connor086724e2014-10-23 15:47:32 -0700129 ConnectPoint prev = intent.src();
weibitf32383b2014-10-22 10:17:31 -0700130
weibit9e622ac2014-10-23 13:45:44 -0700131 //TODO throw exception if the lambda was not assigned successfully
132 for (Link link : intent.path().links()) {
133 Lambda la = null;
134 for (ResourceAllocation allocation : allocations.getResourceAllocation(link)) {
135 if (allocation.type() == ResourceType.LAMBDA) {
136 la = ((LambdaResourceAllocation) allocation).lambda();
137 break;
138 }
139 }
140
141 if (la == null) {
142 log.info("Lambda was not assigned successfully");
143 return null;
144 }
145
weibit253c8652014-10-23 16:30:03 -0700146 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
weibit9e622ac2014-10-23 13:45:44 -0700147 treatmentBuilder.setOutput(link.src().port());
weibit2543d5a2014-10-23 14:13:35 -0700148 treatmentBuilder.setLambda((short) la.toInt());
weibit9e622ac2014-10-23 13:45:44 -0700149
150 FlowRule rule = new DefaultFlowRule(prev.deviceId(),
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700151 selectorBuilder.build(),
152 treatmentBuilder.build(),
153 100,
154 appId,
155 100,
156 true);
weibitaca14602014-10-24 10:26:26 -0700157
Brian O'Connor41718fc2014-10-30 16:57:21 -0700158 rules.add(new FlowRuleBatchEntry(operation, rule));
weibit9e622ac2014-10-23 13:45:44 -0700159
weibit7e583462014-10-23 10:14:05 -0700160 prev = link.dst();
weibit9e622ac2014-10-23 13:45:44 -0700161 selectorBuilder.matchInport(link.dst().port());
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800162 selectorBuilder.matchOpticalSignalType(SIGNAL_TYPE); //todo
weibit2543d5a2014-10-23 14:13:35 -0700163 selectorBuilder.matchLambda((short) la.toInt());
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800164
weibit7e583462014-10-23 10:14:05 -0700165 }
weibit9e622ac2014-10-23 13:45:44 -0700166
167 // build the last T port rule
Brian O'Connor41718fc2014-10-30 16:57:21 -0700168 TrafficTreatment.Builder treatmentLast = builder();
169 treatmentLast.setOutput(intent.dst().port());
Brian O'Connor086724e2014-10-23 15:47:32 -0700170 FlowRule rule = new DefaultFlowRule(intent.dst().deviceId(),
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700171 selectorBuilder.build(),
Brian O'Connor41718fc2014-10-30 16:57:21 -0700172 treatmentLast.build(),
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700173 100,
174 appId,
175 100,
176 true);
Brian O'Connor41718fc2014-10-30 16:57:21 -0700177 rules.add(new FlowRuleBatchEntry(operation, rule));
weibit9e622ac2014-10-23 13:45:44 -0700178
weibit7e583462014-10-23 10:14:05 -0700179 return Lists.newArrayList(new FlowRuleBatchOperation(rules));
180 }
weibitf32383b2014-10-22 10:17:31 -0700181
weibit9e622ac2014-10-23 13:45:44 -0700182 /*private Lambda assignWavelength(List<Link> links) {
weibit7e583462014-10-23 10:14:05 -0700183 // TODO More wavelength assignment algorithm
184 int wavenum = 0;
185 Iterator<Link> itrlink = links.iterator();
186 for (int i = 1; i <= WAVELENGTH; i++) {
187 wavenum = i;
188 boolean found = true;
189 while (itrlink.hasNext()) {
190 Link link = itrlink.next();
191 if (isWavelengthUsed(link, i)) {
192 found = false;
193 break;
194 }
195 }
196 // First-Fit wavelength assignment algorithm
197 if (found) {
198 break;
199 }
200 }
201
202 if (wavenum == 0) {
203 return null;
204 }
205
206 Lambda wave = Lambda.valueOf(wavenum);
207 return wave;
208 }
209
210 private boolean isWavelengthUsed(Link link, int i) {
211 Iterable<LinkResourceAllocations> wave = resourceService.getAllocations(link);
212 for (LinkResourceAllocations ir : wave) {
213 //if ir.resources().contains(i) {
214 //}
215 }
216 return false;
weibit9e622ac2014-10-23 13:45:44 -0700217 }*/
weibitf32383b2014-10-22 10:17:31 -0700218}