blob: 2cf4ccb3368b251eb32bec340cb3b43b2a834218 [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;
Sho SHIMIZU94b7ff42015-05-06 17:51:49 -070040import org.onosproject.net.resource.LambdaResource;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080041import 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
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070049import java.util.Collections;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080050import 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
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070091 return Collections.singletonList(
92 new FlowRuleIntent(appId, createRules(intent, allocations), intent.resources()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080093 }
94
95 private LinkResourceAllocations assignWavelength(OpticalPathIntent intent) {
96 LinkResourceRequest.Builder request = DefaultLinkResourceRequest
97 .builder(intent.id(), intent.path().links())
98 .addLambdaRequest();
99 return resourceService.requestResources(request.build());
100 }
101
102 private List<FlowRule> createRules(OpticalPathIntent intent, LinkResourceAllocations allocations) {
103 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
104 selectorBuilder.matchInPort(intent.src().port());
105
106 List<FlowRule> rules = new LinkedList<>();
107 ConnectPoint prev = intent.src();
108
109 for (Link link : intent.path().links()) {
110 ResourceAllocation allocation = allocations.getResourceAllocation(link).stream()
111 .filter(x -> x.type() == ResourceType.LAMBDA)
112 .findFirst()
113 .orElseThrow(() -> new IntentCompilationException("Lambda was not assigned successfully"));
Sho SHIMIZU94b7ff42015-05-06 17:51:49 -0700114 LambdaResource la = ((LambdaResourceAllocation) allocation).lambda();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800115
116 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
117 treatmentBuilder.setLambda((short) la.toInt());
118 treatmentBuilder.setOutput(link.src().port());
119
120 FlowRule rule = new DefaultFlowRule(prev.deviceId(),
121 selectorBuilder.build(),
122 treatmentBuilder.build(),
123 100,
124 appId,
125 100,
126 true);
127
128 rules.add(rule);
129
130 prev = link.dst();
131 selectorBuilder.matchInPort(link.dst().port());
132 selectorBuilder.matchOpticalSignalType(SIGNAL_TYPE);
133 selectorBuilder.matchLambda((short) la.toInt());
134
135 }
136
137 // build the last T port rule
138 TrafficTreatment.Builder treatmentLast = builder();
139 treatmentLast.setOutput(intent.dst().port());
140 FlowRule rule = new DefaultFlowRule(intent.dst().deviceId(),
141 selectorBuilder.build(),
142 treatmentLast.build(),
143 100,
144 appId,
145 100,
146 true);
147 rules.add(rule);
148
149 return rules;
150 }
151}