blob: e2dc0d4211681f84acd6ab899404556608418299 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent.impl;
weibitf32383b2014-10-22 10:17:31 -070017
Ray Milkey71ade562015-02-18 15:08:07 -080018import java.util.List;
19import java.util.Set;
20
weibitf32383b2014-10-22 10:17:31 -070021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.core.ApplicationId;
27import org.onosproject.core.CoreService;
28import org.onosproject.net.ConnectPoint;
29import org.onosproject.net.Link;
30import org.onosproject.net.flow.DefaultFlowRule;
31import org.onosproject.net.flow.DefaultTrafficSelector;
32import org.onosproject.net.flow.DefaultTrafficTreatment;
33import org.onosproject.net.flow.FlowRule;
Ray Milkey71ade562015-02-18 15:08:07 -080034import org.onosproject.net.flow.FlowRuleOperation;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.flow.FlowRuleService;
36import org.onosproject.net.flow.TrafficSelector;
37import org.onosproject.net.flow.TrafficTreatment;
38import org.onosproject.net.intent.IntentExtensionService;
39import org.onosproject.net.intent.IntentInstaller;
40import org.onosproject.net.intent.OpticalPathIntent;
41import org.onosproject.net.resource.DefaultLinkResourceRequest;
42import org.onosproject.net.resource.Lambda;
43import org.onosproject.net.resource.LambdaResourceAllocation;
44import org.onosproject.net.resource.LinkResourceAllocations;
45import org.onosproject.net.resource.LinkResourceRequest;
46import org.onosproject.net.resource.LinkResourceService;
47import org.onosproject.net.resource.ResourceAllocation;
48import org.onosproject.net.resource.ResourceType;
49import org.onosproject.net.topology.TopologyService;
weibitf32383b2014-10-22 10:17:31 -070050import org.slf4j.Logger;
51
Ray Milkey71ade562015-02-18 15:08:07 -080052import com.google.common.collect.ImmutableSet;
53import com.google.common.collect.Lists;
54import com.google.common.collect.Sets;
Thomas Vachuska425a2d72014-10-29 11:28:28 -070055
Brian O'Connorabafb502014-12-02 22:26:20 -080056import static org.onosproject.net.flow.DefaultTrafficTreatment.builder;
Thomas Vachuska425a2d72014-10-29 11:28:28 -070057import static org.slf4j.LoggerFactory.getLogger;
weibitf32383b2014-10-22 10:17:31 -070058
59/**
Brian O'Connorabafb502014-12-02 22:26:20 -080060 * Installer for {@link org.onosproject.net.intent.OpticalPathIntent optical path connectivity intents}.
weibitf32383b2014-10-22 10:17:31 -070061 */
weibitf32383b2014-10-22 10:17:31 -070062@Component(immediate = true)
63public class OpticalPathIntentInstaller implements IntentInstaller<OpticalPathIntent> {
weibitf32383b2014-10-22 10:17:31 -070064 private final Logger log = getLogger(getClass());
65
66 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected IntentExtensionService intentManager;
68
69 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 protected FlowRuleService flowRuleService;
71
72 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
73 protected CoreService coreService;
74
weibit7e583462014-10-23 10:14:05 -070075 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
76 protected TopologyService topologyService;
77
78 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
79 protected LinkResourceService resourceService;
80
weibitf32383b2014-10-22 10:17:31 -070081 private ApplicationId appId;
82
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -080083 static final short SIGNAL_TYPE = (short) 1;
weibit7e583462014-10-23 10:14:05 -070084
weibitf32383b2014-10-22 10:17:31 -070085 @Activate
86 public void activate() {
Brian O'Connorabafb502014-12-02 22:26:20 -080087 appId = coreService.registerApplication("org.onosproject.net.intent");
weibitf32383b2014-10-22 10:17:31 -070088 intentManager.registerInstaller(OpticalPathIntent.class, this);
89 }
90
91 @Deactivate
92 public void deactivate() {
93 intentManager.unregisterInstaller(OpticalPathIntent.class);
94 }
95
96 @Override
Ray Milkey71ade562015-02-18 15:08:07 -080097 public List<Set<FlowRuleOperation>> install(OpticalPathIntent intent) {
weibit9e622ac2014-10-23 13:45:44 -070098 LinkResourceAllocations allocations = assignWavelength(intent);
Ray Milkey71ade562015-02-18 15:08:07 -080099 return generateRules(intent, allocations, FlowRuleOperation.Type.ADD);
Brian O'Connor41718fc2014-10-30 16:57:21 -0700100 }
weibitf32383b2014-10-22 10:17:31 -0700101
Brian O'Connor41718fc2014-10-30 16:57:21 -0700102 @Override
Ray Milkey71ade562015-02-18 15:08:07 -0800103 public List<Set<FlowRuleOperation>> uninstall(OpticalPathIntent intent) {
Brian O'Connor41718fc2014-10-30 16:57:21 -0700104 LinkResourceAllocations allocations = resourceService.getAllocations(intent.id());
Ray Milkey71ade562015-02-18 15:08:07 -0800105 List<Set<FlowRuleOperation>> rules = generateRules(intent, allocations, FlowRuleOperation.Type.REMOVE);
Brian O'Connor772852a2014-11-17 15:51:19 -0800106 log.info("uninstall rules: {}", rules);
107 return rules;
Brian O'Connor41718fc2014-10-30 16:57:21 -0700108 }
109
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700110 @Override
Ray Milkey71ade562015-02-18 15:08:07 -0800111 public List<Set<FlowRuleOperation>> replace(OpticalPathIntent oldIntent,
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700112 OpticalPathIntent newIntent) {
113 // FIXME: implement this
Ray Milkey71ade562015-02-18 15:08:07 -0800114 List<Set<FlowRuleOperation>> batches = Lists.newArrayList();
Brian O'Connor772852a2014-11-17 15:51:19 -0800115 batches.addAll(uninstall(oldIntent));
116 batches.addAll(install(newIntent));
117 return batches;
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700118 }
119
Brian O'Connor41718fc2014-10-30 16:57:21 -0700120 private LinkResourceAllocations assignWavelength(OpticalPathIntent intent) {
121 LinkResourceRequest.Builder request = DefaultLinkResourceRequest.builder(intent.id(),
122 intent.path().links())
123 .addLambdaRequest();
124 LinkResourceAllocations retLambda = resourceService.requestResources(request.build());
125 return retLambda;
126 }
127
Ray Milkey71ade562015-02-18 15:08:07 -0800128 private List<Set<FlowRuleOperation>> generateRules(OpticalPathIntent intent,
129 LinkResourceAllocations allocations,
130 FlowRuleOperation.Type operation) {
weibit9e622ac2014-10-23 13:45:44 -0700131 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800132 selectorBuilder.matchInPort(intent.src().port());
weibitf32383b2014-10-22 10:17:31 -0700133
Ray Milkey71ade562015-02-18 15:08:07 -0800134 Set<FlowRuleOperation> rules = Sets.newHashSet();
Brian O'Connor086724e2014-10-23 15:47:32 -0700135 ConnectPoint prev = intent.src();
weibitf32383b2014-10-22 10:17:31 -0700136
Brian O'Connor8b6b1722014-12-08 01:28:50 -0800137 //FIXME check for null allocations
weibit9e622ac2014-10-23 13:45:44 -0700138 //TODO throw exception if the lambda was not assigned successfully
139 for (Link link : intent.path().links()) {
140 Lambda la = null;
141 for (ResourceAllocation allocation : allocations.getResourceAllocation(link)) {
142 if (allocation.type() == ResourceType.LAMBDA) {
143 la = ((LambdaResourceAllocation) allocation).lambda();
144 break;
145 }
146 }
147
148 if (la == null) {
149 log.info("Lambda was not assigned successfully");
150 return null;
151 }
152
weibit253c8652014-10-23 16:30:03 -0700153 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
weibit9e622ac2014-10-23 13:45:44 -0700154 treatmentBuilder.setOutput(link.src().port());
weibit2543d5a2014-10-23 14:13:35 -0700155 treatmentBuilder.setLambda((short) la.toInt());
weibit9e622ac2014-10-23 13:45:44 -0700156
157 FlowRule rule = new DefaultFlowRule(prev.deviceId(),
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700158 selectorBuilder.build(),
159 treatmentBuilder.build(),
160 100,
161 appId,
162 100,
163 true);
weibitaca14602014-10-24 10:26:26 -0700164
Ray Milkey71ade562015-02-18 15:08:07 -0800165 rules.add(new FlowRuleOperation(rule, operation));
weibit9e622ac2014-10-23 13:45:44 -0700166
weibit7e583462014-10-23 10:14:05 -0700167 prev = link.dst();
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800168 selectorBuilder.matchInPort(link.dst().port());
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800169 selectorBuilder.matchOpticalSignalType(SIGNAL_TYPE); //todo
weibit2543d5a2014-10-23 14:13:35 -0700170 selectorBuilder.matchLambda((short) la.toInt());
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800171
weibit7e583462014-10-23 10:14:05 -0700172 }
weibit9e622ac2014-10-23 13:45:44 -0700173
174 // build the last T port rule
Brian O'Connor41718fc2014-10-30 16:57:21 -0700175 TrafficTreatment.Builder treatmentLast = builder();
176 treatmentLast.setOutput(intent.dst().port());
Brian O'Connor086724e2014-10-23 15:47:32 -0700177 FlowRule rule = new DefaultFlowRule(intent.dst().deviceId(),
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700178 selectorBuilder.build(),
Brian O'Connor41718fc2014-10-30 16:57:21 -0700179 treatmentLast.build(),
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700180 100,
181 appId,
182 100,
183 true);
Ray Milkey71ade562015-02-18 15:08:07 -0800184 rules.add(new FlowRuleOperation(rule, operation));
weibit9e622ac2014-10-23 13:45:44 -0700185
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800186 //FIXME change to new api
Ray Milkey71ade562015-02-18 15:08:07 -0800187 return Lists.newArrayList(ImmutableSet.of(rules));
weibit7e583462014-10-23 10:14:05 -0700188 }
weibitf32383b2014-10-22 10:17:31 -0700189}