blob: 1f7ef68cb99cc21828cf475d15c8a5abf5b271a8 [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
6import java.util.Iterator;
7import java.util.List;
8import java.util.concurrent.Future;
9
10import org.apache.felix.scr.annotations.Activate;
11import org.apache.felix.scr.annotations.Component;
12import org.apache.felix.scr.annotations.Deactivate;
13import org.apache.felix.scr.annotations.Reference;
14import org.apache.felix.scr.annotations.ReferenceCardinality;
15import org.onlab.onos.ApplicationId;
16import org.onlab.onos.CoreService;
17import org.onlab.onos.net.ConnectPoint;
18import org.onlab.onos.net.Link;
19import org.onlab.onos.net.flow.CompletedBatchOperation;
20import org.onlab.onos.net.flow.DefaultFlowRule;
21import org.onlab.onos.net.flow.DefaultTrafficSelector;
weibit7e583462014-10-23 10:14:05 -070022import org.onlab.onos.net.flow.DefaultTrafficTreatment;
weibitf32383b2014-10-22 10:17:31 -070023import org.onlab.onos.net.flow.FlowRule;
24import org.onlab.onos.net.flow.FlowRuleBatchEntry;
25import org.onlab.onos.net.flow.FlowRuleBatchOperation;
26import org.onlab.onos.net.flow.FlowRuleService;
27import org.onlab.onos.net.flow.TrafficSelector;
28import org.onlab.onos.net.flow.TrafficTreatment;
29import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
30import org.onlab.onos.net.intent.IntentExtensionService;
31import org.onlab.onos.net.intent.IntentInstaller;
32import org.onlab.onos.net.intent.OpticalPathIntent;
weibit7e583462014-10-23 10:14:05 -070033import org.onlab.onos.net.resource.Lambda;
34import org.onlab.onos.net.resource.LinkResourceAllocations;
35import org.onlab.onos.net.resource.LinkResourceRequest;
36import org.onlab.onos.net.resource.LinkResourceService;
37import org.onlab.onos.net.resource.ResourceRequest;
38import org.onlab.onos.net.topology.TopologyService;
weibitf32383b2014-10-22 10:17:31 -070039import org.slf4j.Logger;
40
41import com.google.common.collect.Lists;
42
43/**
44 * OpticaliIntentInstaller for optical path intents.
45 * It essentially generates optical FlowRules and
46 * call the flowRule service to execute them.
47 */
48
49@Component(immediate = true)
50public class OpticalPathIntentInstaller implements IntentInstaller<OpticalPathIntent> {
weibitf32383b2014-10-22 10:17:31 -070051 private final Logger log = getLogger(getClass());
52
53 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
54 protected IntentExtensionService intentManager;
55
56 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
57 protected FlowRuleService flowRuleService;
58
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected CoreService coreService;
61
weibit7e583462014-10-23 10:14:05 -070062 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected TopologyService topologyService;
64
65 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected LinkResourceService resourceService;
67
weibitf32383b2014-10-22 10:17:31 -070068 private ApplicationId appId;
69
weibit7e583462014-10-23 10:14:05 -070070 final static short WAVELENGTH = 80;
71
weibitf32383b2014-10-22 10:17:31 -070072 @Activate
73 public void activate() {
74 appId = coreService.registerApplication("org.onlab.onos.net.intent");
75 intentManager.registerInstaller(OpticalPathIntent.class, this);
76 }
77
78 @Deactivate
79 public void deactivate() {
80 intentManager.unregisterInstaller(OpticalPathIntent.class);
81 }
82
83 @Override
weibit7e583462014-10-23 10:14:05 -070084 public List<FlowRuleBatchOperation> install(OpticalPathIntent intent) {
85 Lambda la = assignWavelength(intent.path().links());
86 if (la == null) {
87 return null;
weibitf32383b2014-10-22 10:17:31 -070088 }
weibit7e583462014-10-23 10:14:05 -070089 // resourceService.requestResources(la);
90 // la.toInt();
91 //intent.selector().criteria();
weibitf32383b2014-10-22 10:17:31 -070092
weibit7e583462014-10-23 10:14:05 -070093 //TrafficSelector.Builder builder = DefaultTrafficSelector.builder();
94 //builder.matchLambdaType(la.toInt())
95 // .matchInport(intent.getSrcConnectPoint().port());
weibitf32383b2014-10-22 10:17:31 -070096
weibitf32383b2014-10-22 10:17:31 -070097 TrafficSelector.Builder builder =
98 DefaultTrafficSelector.builder(intent.selector());
99 Iterator<Link> links = intent.path().links().iterator();
100 ConnectPoint prev = links.next().dst();
101 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
weibit7e583462014-10-23 10:14:05 -0700102 // TODO Generate multiple batches
weibitf32383b2014-10-22 10:17:31 -0700103 while (links.hasNext()) {
104 builder.matchInport(prev.port());
105 Link link = links.next();
106 TrafficTreatment treatment = builder()
107 .setOutput(link.src().port()).build();
108
109 FlowRule rule = new DefaultFlowRule(link.src().deviceId(),
110 builder.build(),
111 treatment,
112 100,
113 appId,
weibit7e583462014-10-23 10:14:05 -0700114 100,
115 true);
116 rules.add(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule));
117 prev = link.dst();
118 }
119 return Lists.newArrayList(new FlowRuleBatchOperation(rules));
120 }
weibitf32383b2014-10-22 10:17:31 -0700121
weibit7e583462014-10-23 10:14:05 -0700122 private Lambda assignWavelength(List<Link> links) {
123 // TODO More wavelength assignment algorithm
124 int wavenum = 0;
125 Iterator<Link> itrlink = links.iterator();
126 for (int i = 1; i <= WAVELENGTH; i++) {
127 wavenum = i;
128 boolean found = true;
129 while (itrlink.hasNext()) {
130 Link link = itrlink.next();
131 if (isWavelengthUsed(link, i)) {
132 found = false;
133 break;
134 }
135 }
136 // First-Fit wavelength assignment algorithm
137 if (found) {
138 break;
139 }
140 }
141
142 if (wavenum == 0) {
143 return null;
144 }
145
146 Lambda wave = Lambda.valueOf(wavenum);
147 return wave;
148 }
149
150 private boolean isWavelengthUsed(Link link, int i) {
151 Iterable<LinkResourceAllocations> wave = resourceService.getAllocations(link);
152 for (LinkResourceAllocations ir : wave) {
153 //if ir.resources().contains(i) {
154 //}
155 }
156 return false;
157 }
158
159 @Override
160 public List<FlowRuleBatchOperation> uninstall(OpticalPathIntent intent) {
161 TrafficSelector.Builder builder =
162 DefaultTrafficSelector.builder(intent.selector());
163 Iterator<Link> links = intent.path().links().iterator();
164 ConnectPoint prev = links.next().dst();
165 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
166 // TODO Generate multiple batches
167 while (links.hasNext()) {
168 builder.matchInport(prev.port());
169 Link link = links.next();
170 TrafficTreatment treatment = builder()
171 .setOutput(link.src().port()).build();
172 FlowRule rule = new DefaultFlowRule(link.src().deviceId(),
173 builder.build(),
174 treatment,
175 100,
176 appId,
177 100,
178 true);
weibitf32383b2014-10-22 10:17:31 -0700179 rules.add(new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, rule));
180 prev = link.dst();
181 }
weibit7e583462014-10-23 10:14:05 -0700182 return Lists.newArrayList(new FlowRuleBatchOperation(rules));
weibitf32383b2014-10-22 10:17:31 -0700183 }
184
185}