blob: 637ebebd65a5c5ad3ed29f2f6b3e52b90705afdf [file] [log] [blame]
weibitf32383b2014-10-22 10:17:31 -07001package org.onlab.onos.net.intent.impl;
2
3import static org.onlab.onos.net.flow.DefaultTrafficTreatment.builder;
4import static org.slf4j.LoggerFactory.getLogger;
5
weibit9e622ac2014-10-23 13:45:44 -07006
weibitf32383b2014-10-22 10:17:31 -07007import java.util.List;
weibitf32383b2014-10-22 10:17:31 -07008
9import org.apache.felix.scr.annotations.Activate;
10import org.apache.felix.scr.annotations.Component;
11import org.apache.felix.scr.annotations.Deactivate;
12import org.apache.felix.scr.annotations.Reference;
13import org.apache.felix.scr.annotations.ReferenceCardinality;
14import org.onlab.onos.ApplicationId;
15import org.onlab.onos.CoreService;
16import org.onlab.onos.net.ConnectPoint;
17import org.onlab.onos.net.Link;
weibitf32383b2014-10-22 10:17:31 -070018import org.onlab.onos.net.flow.DefaultFlowRule;
19import org.onlab.onos.net.flow.DefaultTrafficSelector;
weibit7e583462014-10-23 10:14:05 -070020import org.onlab.onos.net.flow.DefaultTrafficTreatment;
weibitf32383b2014-10-22 10:17:31 -070021import org.onlab.onos.net.flow.FlowRule;
22import org.onlab.onos.net.flow.FlowRuleBatchEntry;
23import org.onlab.onos.net.flow.FlowRuleBatchOperation;
24import org.onlab.onos.net.flow.FlowRuleService;
25import org.onlab.onos.net.flow.TrafficSelector;
26import org.onlab.onos.net.flow.TrafficTreatment;
27import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
28import org.onlab.onos.net.intent.IntentExtensionService;
29import org.onlab.onos.net.intent.IntentInstaller;
30import org.onlab.onos.net.intent.OpticalPathIntent;
weibit9e622ac2014-10-23 13:45:44 -070031import org.onlab.onos.net.resource.DefaultLinkResourceRequest;
weibit7e583462014-10-23 10:14:05 -070032import org.onlab.onos.net.resource.Lambda;
weibit9e622ac2014-10-23 13:45:44 -070033import org.onlab.onos.net.resource.LambdaResourceAllocation;
weibit7e583462014-10-23 10:14:05 -070034import org.onlab.onos.net.resource.LinkResourceAllocations;
35import org.onlab.onos.net.resource.LinkResourceRequest;
36import org.onlab.onos.net.resource.LinkResourceService;
weibit9e622ac2014-10-23 13:45:44 -070037import org.onlab.onos.net.resource.ResourceAllocation;
38import org.onlab.onos.net.resource.ResourceType;
weibit7e583462014-10-23 10:14:05 -070039import org.onlab.onos.net.topology.TopologyService;
weibitf32383b2014-10-22 10:17:31 -070040import org.slf4j.Logger;
41
42import com.google.common.collect.Lists;
43
44/**
45 * OpticaliIntentInstaller for optical path intents.
46 * It essentially generates optical FlowRules and
47 * call the flowRule service to execute them.
48 */
49
50@Component(immediate = true)
51public class OpticalPathIntentInstaller implements IntentInstaller<OpticalPathIntent> {
weibitf32383b2014-10-22 10:17:31 -070052 private final Logger log = getLogger(getClass());
53
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected IntentExtensionService intentManager;
56
57 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58 protected FlowRuleService flowRuleService;
59
60 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
61 protected CoreService coreService;
62
weibit7e583462014-10-23 10:14:05 -070063 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 protected TopologyService topologyService;
65
66 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected LinkResourceService resourceService;
68
weibitf32383b2014-10-22 10:17:31 -070069 private ApplicationId appId;
70
weibit9e622ac2014-10-23 13:45:44 -070071 //final short WAVELENGTH = 80;
weibit7e583462014-10-23 10:14:05 -070072
weibitf32383b2014-10-22 10:17:31 -070073 @Activate
74 public void activate() {
75 appId = coreService.registerApplication("org.onlab.onos.net.intent");
76 intentManager.registerInstaller(OpticalPathIntent.class, this);
77 }
78
79 @Deactivate
80 public void deactivate() {
81 intentManager.unregisterInstaller(OpticalPathIntent.class);
82 }
83
84 @Override
weibit7e583462014-10-23 10:14:05 -070085 public List<FlowRuleBatchOperation> install(OpticalPathIntent intent) {
weibit9e622ac2014-10-23 13:45:44 -070086 LinkResourceAllocations allocations = assignWavelength(intent);
weibitf32383b2014-10-22 10:17:31 -070087
weibit9e622ac2014-10-23 13:45:44 -070088 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
89 selectorBuilder.matchInport(intent.getSrcConnectPoint().port());
weibitf32383b2014-10-22 10:17:31 -070090
weibit9e622ac2014-10-23 13:45:44 -070091 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
92
weibitf32383b2014-10-22 10:17:31 -070093 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
weibit9e622ac2014-10-23 13:45:44 -070094 ConnectPoint prev = intent.getSrcConnectPoint();
weibitf32383b2014-10-22 10:17:31 -070095
weibit9e622ac2014-10-23 13:45:44 -070096 //TODO throw exception if the lambda was not assigned successfully
97 for (Link link : intent.path().links()) {
98 Lambda la = null;
99 for (ResourceAllocation allocation : allocations.getResourceAllocation(link)) {
100 if (allocation.type() == ResourceType.LAMBDA) {
101 la = ((LambdaResourceAllocation) allocation).lambda();
102 break;
103 }
104 }
105
106 if (la == null) {
107 log.info("Lambda was not assigned successfully");
108 return null;
109 }
110
111 treatmentBuilder.setOutput(link.src().port());
weibit2543d5a2014-10-23 14:13:35 -0700112 treatmentBuilder.setLambda((short) la.toInt());
weibit9e622ac2014-10-23 13:45:44 -0700113
114 FlowRule rule = new DefaultFlowRule(prev.deviceId(),
115 selectorBuilder.build(),
116 treatmentBuilder.build(),
weibitf32383b2014-10-22 10:17:31 -0700117 100,
118 appId,
weibit7e583462014-10-23 10:14:05 -0700119 100,
120 true);
121 rules.add(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule));
weibit9e622ac2014-10-23 13:45:44 -0700122
weibit7e583462014-10-23 10:14:05 -0700123 prev = link.dst();
weibit9e622ac2014-10-23 13:45:44 -0700124 selectorBuilder.matchInport(link.dst().port());
weibit2543d5a2014-10-23 14:13:35 -0700125 selectorBuilder.matchLambda((short) la.toInt());
weibit7e583462014-10-23 10:14:05 -0700126 }
weibit9e622ac2014-10-23 13:45:44 -0700127
128 // build the last T port rule
129 TrafficTreatment treatmentLast = builder()
130 .setOutput(intent.getDst().port()).build();
131 FlowRule rule = new DefaultFlowRule(intent.getDst().deviceId(),
132 selectorBuilder.build(),
133 treatmentLast,
134 100,
135 appId,
136 100,
137 true);
138 rules.add(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule));
139
weibit7e583462014-10-23 10:14:05 -0700140 return Lists.newArrayList(new FlowRuleBatchOperation(rules));
141 }
weibitf32383b2014-10-22 10:17:31 -0700142
weibit9e622ac2014-10-23 13:45:44 -0700143 private LinkResourceAllocations assignWavelength(OpticalPathIntent intent) {
144 LinkResourceRequest.Builder request = DefaultLinkResourceRequest.builder(intent.id(),
145 intent.path().links())
146 .addLambdaRequest();
147 LinkResourceAllocations retLambda = resourceService.requestResources(request.build());
148 return retLambda;
149 }
150
151 /*private Lambda assignWavelength(List<Link> links) {
weibit7e583462014-10-23 10:14:05 -0700152 // TODO More wavelength assignment algorithm
153 int wavenum = 0;
154 Iterator<Link> itrlink = links.iterator();
155 for (int i = 1; i <= WAVELENGTH; i++) {
156 wavenum = i;
157 boolean found = true;
158 while (itrlink.hasNext()) {
159 Link link = itrlink.next();
160 if (isWavelengthUsed(link, i)) {
161 found = false;
162 break;
163 }
164 }
165 // First-Fit wavelength assignment algorithm
166 if (found) {
167 break;
168 }
169 }
170
171 if (wavenum == 0) {
172 return null;
173 }
174
175 Lambda wave = Lambda.valueOf(wavenum);
176 return wave;
177 }
178
179 private boolean isWavelengthUsed(Link link, int i) {
180 Iterable<LinkResourceAllocations> wave = resourceService.getAllocations(link);
181 for (LinkResourceAllocations ir : wave) {
182 //if ir.resources().contains(i) {
183 //}
184 }
185 return false;
weibit9e622ac2014-10-23 13:45:44 -0700186 }*/
weibit7e583462014-10-23 10:14:05 -0700187
188 @Override
189 public List<FlowRuleBatchOperation> uninstall(OpticalPathIntent intent) {
weibit9e622ac2014-10-23 13:45:44 -0700190 LinkResourceAllocations allocations = resourceService.getAllocation(intent.id());
191
192 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
193 selectorBuilder.matchInport(intent.getSrcConnectPoint().port());
194
195 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
196
weibit7e583462014-10-23 10:14:05 -0700197 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
weibit9e622ac2014-10-23 13:45:44 -0700198 ConnectPoint prev = intent.getSrcConnectPoint();
199
200 //TODO throw exception if the lambda was not retrieved successfully
201 for (Link link : intent.path().links()) {
202 Lambda la = null;
203 for (ResourceAllocation allocation : allocations.getResourceAllocation(link)) {
204 if (allocation.type() == ResourceType.LAMBDA) {
205 la = ((LambdaResourceAllocation) allocation).lambda();
206 break;
207 }
208 }
209
210 if (la == null) {
211 log.info("Lambda was not retrieved successfully");
212 return null;
213 }
214
215 treatmentBuilder.setOutput(link.src().port());
weibit2543d5a2014-10-23 14:13:35 -0700216 treatmentBuilder.setLambda((short) la.toInt());
weibit9e622ac2014-10-23 13:45:44 -0700217
218 FlowRule rule = new DefaultFlowRule(prev.deviceId(),
219 selectorBuilder.build(),
220 treatmentBuilder.build(),
weibit7e583462014-10-23 10:14:05 -0700221 100,
222 appId,
223 100,
224 true);
weibitf32383b2014-10-22 10:17:31 -0700225 rules.add(new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, rule));
weibit9e622ac2014-10-23 13:45:44 -0700226
weibitf32383b2014-10-22 10:17:31 -0700227 prev = link.dst();
weibit9e622ac2014-10-23 13:45:44 -0700228 selectorBuilder.matchInport(link.dst().port());
weibit2543d5a2014-10-23 14:13:35 -0700229 selectorBuilder.matchLambda((short) la.toInt());
weibitf32383b2014-10-22 10:17:31 -0700230 }
weibit9e622ac2014-10-23 13:45:44 -0700231
232 // build the last T port rule
233 TrafficTreatment treatmentLast = builder()
234 .setOutput(intent.getDst().port()).build();
235 FlowRule rule = new DefaultFlowRule(intent.getDst().deviceId(),
236 selectorBuilder.build(),
237 treatmentLast,
238 100,
239 appId,
240 100,
241 true);
242 rules.add(new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, rule));
243
weibit7e583462014-10-23 10:14:05 -0700244 return Lists.newArrayList(new FlowRuleBatchOperation(rules));
weibitf32383b2014-10-22 10:17:31 -0700245 }
246
247}