blob: 856499ce119323f18a9acb37735eca0273097f38 [file] [log] [blame]
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08001/*
2 * Copyright 2015 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 */
16package org.onosproject.net.intent.impl.compiler;
17
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
23import org.onosproject.core.ApplicationId;
24import org.onosproject.core.CoreService;
25import org.onosproject.net.ConnectPoint;
26import org.onosproject.net.Link;
27import org.onosproject.net.flow.DefaultFlowRule;
28import org.onosproject.net.flow.DefaultTrafficSelector;
29import org.onosproject.net.flow.DefaultTrafficTreatment;
30import org.onosproject.net.flow.FlowRule;
31import org.onosproject.net.flow.TrafficSelector;
32import org.onosproject.net.flow.TrafficTreatment;
33import org.onosproject.net.intent.FlowRuleIntent;
34import org.onosproject.net.intent.Intent;
35import org.onosproject.net.intent.IntentCompiler;
36import org.onosproject.net.intent.IntentExtensionService;
37import org.onosproject.net.intent.OpticalPathIntent;
38import org.onosproject.net.intent.impl.IntentCompilationException;
39import org.onosproject.net.resource.DefaultLinkResourceRequest;
40import org.onosproject.net.resource.Lambda;
41import org.onosproject.net.resource.LambdaResourceAllocation;
42import org.onosproject.net.resource.LinkResourceAllocations;
43import org.onosproject.net.resource.LinkResourceRequest;
44import org.onosproject.net.resource.LinkResourceService;
45import org.onosproject.net.resource.ResourceAllocation;
46import org.onosproject.net.resource.ResourceType;
47import org.onosproject.net.topology.TopologyService;
48
49import java.util.Arrays;
50import java.util.LinkedList;
51import java.util.List;
52import java.util.Set;
53
54import static org.onosproject.net.flow.DefaultTrafficTreatment.builder;
55
56@Component(immediate = true)
57public class OpticalPathIntentCompiler implements IntentCompiler<OpticalPathIntent> {
58
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected IntentExtensionService intentManager;
61
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected CoreService coreService;
64
65 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected TopologyService topologyService;
67
68 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
69 protected LinkResourceService resourceService;
70
71 private ApplicationId appId;
72
73 static final short SIGNAL_TYPE = (short) 1;
74
75 @Activate
76 public void activate() {
77 appId = coreService.registerApplication("org.onosproject.net.intent");
78 intentManager.registerCompiler(OpticalPathIntent.class, this);
79 }
80
81 @Deactivate
82 public void deactivate() {
83 intentManager.unregisterCompiler(OpticalPathIntent.class);
84 }
85
86 @Override
87 public List<Intent> compile(OpticalPathIntent intent, List<Intent> installable,
88 Set<LinkResourceAllocations> resources) {
89 LinkResourceAllocations allocations = assignWavelength(intent);
90
91 return Arrays.asList(new FlowRuleIntent(appId, createRules(intent, allocations)));
92 }
93
94 private LinkResourceAllocations assignWavelength(OpticalPathIntent intent) {
95 LinkResourceRequest.Builder request = DefaultLinkResourceRequest
96 .builder(intent.id(), intent.path().links())
97 .addLambdaRequest();
98 return resourceService.requestResources(request.build());
99 }
100
101 private List<FlowRule> createRules(OpticalPathIntent intent, LinkResourceAllocations allocations) {
102 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
103 selectorBuilder.matchInPort(intent.src().port());
104
105 List<FlowRule> rules = new LinkedList<>();
106 ConnectPoint prev = intent.src();
107
108 for (Link link : intent.path().links()) {
109 ResourceAllocation allocation = allocations.getResourceAllocation(link).stream()
110 .filter(x -> x.type() == ResourceType.LAMBDA)
111 .findFirst()
112 .orElseThrow(() -> new IntentCompilationException("Lambda was not assigned successfully"));
113 Lambda la = ((LambdaResourceAllocation) allocation).lambda();
114
115 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
116 treatmentBuilder.setLambda((short) la.toInt());
117 treatmentBuilder.setOutput(link.src().port());
118
119 FlowRule rule = new DefaultFlowRule(prev.deviceId(),
120 selectorBuilder.build(),
121 treatmentBuilder.build(),
122 100,
123 appId,
124 100,
125 true);
126
127 rules.add(rule);
128
129 prev = link.dst();
130 selectorBuilder.matchInPort(link.dst().port());
131 selectorBuilder.matchOpticalSignalType(SIGNAL_TYPE);
132 selectorBuilder.matchLambda((short) la.toInt());
133
134 }
135
136 // build the last T port rule
137 TrafficTreatment.Builder treatmentLast = builder();
138 treatmentLast.setOutput(intent.dst().port());
139 FlowRule rule = new DefaultFlowRule(intent.dst().deviceId(),
140 selectorBuilder.build(),
141 treatmentLast.build(),
142 100,
143 appId,
144 100,
145 true);
146 rules.add(rule);
147
148 return rules;
149 }
150}