blob: 2e4f2fd9209c214f31cb70a78f52d7f06f3d0866 [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;
22import org.onlab.onos.net.flow.FlowRule;
23import org.onlab.onos.net.flow.FlowRuleBatchEntry;
24import org.onlab.onos.net.flow.FlowRuleBatchOperation;
25import org.onlab.onos.net.flow.FlowRuleService;
26import org.onlab.onos.net.flow.TrafficSelector;
27import org.onlab.onos.net.flow.TrafficTreatment;
28import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
29import org.onlab.onos.net.intent.IntentExtensionService;
30import org.onlab.onos.net.intent.IntentInstaller;
31import org.onlab.onos.net.intent.OpticalPathIntent;
32import org.slf4j.Logger;
33
34import com.google.common.collect.Lists;
35
36/**
37 * OpticaliIntentInstaller for optical path intents.
38 * It essentially generates optical FlowRules and
39 * call the flowRule service to execute them.
40 */
41
42@Component(immediate = true)
43public class OpticalPathIntentInstaller implements IntentInstaller<OpticalPathIntent> {
44
45 private final Logger log = getLogger(getClass());
46
47 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
48 protected IntentExtensionService intentManager;
49
50 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
51 protected FlowRuleService flowRuleService;
52
53 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
54 protected CoreService coreService;
55
56 private ApplicationId appId;
57
58 @Activate
59 public void activate() {
60 appId = coreService.registerApplication("org.onlab.onos.net.intent");
61 intentManager.registerInstaller(OpticalPathIntent.class, this);
62 }
63
64 @Deactivate
65 public void deactivate() {
66 intentManager.unregisterInstaller(OpticalPathIntent.class);
67 }
68
69 @Override
70 public Future<CompletedBatchOperation> install(OpticalPathIntent intent) {
71 TrafficSelector.Builder builder =
72 DefaultTrafficSelector.builder(intent.selector());
73 Iterator<Link> links = intent.path().links().iterator();
74 ConnectPoint prev = links.next().dst();
75 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
76 while (links.hasNext()) {
77 builder.matchInport(prev.port());
78 Link link = links.next();
79 TrafficTreatment treatment = builder()
80 .setOutput(link.src().port()).build();
81
82 FlowRule rule = new DefaultFlowRule(link.src().deviceId(),
83 builder.build(),
84 treatment,
85 100, // priority is set to 100
86 appId,
87 100); // ROADM may need longer cross-connect time, here it is assumed 100ms.
88
89 rules.add(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule));
90 prev = link.dst();
91 }
92
93 FlowRuleBatchOperation batch = new FlowRuleBatchOperation(rules);
94 Future<CompletedBatchOperation> future = flowRuleService.applyBatch(batch);
95 return future;
96 }
97
98 @Override
99 public Future<CompletedBatchOperation> uninstall(OpticalPathIntent intent) {
100 TrafficSelector.Builder builder =
101 DefaultTrafficSelector.builder(intent.selector());
102 Iterator<Link> links = intent.path().links().iterator();
103 ConnectPoint prev = links.next().dst();
104 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
105
106 while (links.hasNext()) {
107 builder.matchInport(prev.port());
108 Link link = links.next();
109 TrafficTreatment treatment = builder()
110 .setOutput(link.src().port()).build();
111
112 FlowRule rule = new DefaultFlowRule(link.src().deviceId(),
113 builder.build(),
114 treatment,
115 100,
116 appId,
117 100);
118
119 rules.add(new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, rule));
120 prev = link.dst();
121 }
122
123 FlowRuleBatchOperation batch = new FlowRuleBatchOperation(rules);
124 Future<CompletedBatchOperation> future = flowRuleService.applyBatch(batch);
125 return future;
126 }
127
128}