blob: 43f6bfec0561506b92af62b22b371d826c7c691b [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 */
Sho SHIMIZU3a7c98e2015-02-25 17:27:04 -080016package org.onosproject.net.intent.impl.installer;
weibitf32383b2014-10-22 10:17:31 -070017
Brian O'Connor64a0369d2015-02-20 22:02:59 -080018import com.google.common.collect.ImmutableSet;
19import com.google.common.collect.Lists;
weibitf32383b2014-10-22 10:17:31 -070020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
27import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.Link;
29import org.onosproject.net.flow.DefaultFlowRule;
30import org.onosproject.net.flow.DefaultTrafficSelector;
31import org.onosproject.net.flow.DefaultTrafficTreatment;
32import org.onosproject.net.flow.FlowRule;
Ray Milkey71ade562015-02-18 15:08:07 -080033import org.onosproject.net.flow.FlowRuleOperation;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.flow.FlowRuleService;
35import org.onosproject.net.flow.TrafficSelector;
36import org.onosproject.net.flow.TrafficTreatment;
37import org.onosproject.net.intent.IntentExtensionService;
38import org.onosproject.net.intent.IntentInstaller;
39import org.onosproject.net.intent.OpticalPathIntent;
40import org.onosproject.net.resource.DefaultLinkResourceRequest;
41import org.onosproject.net.resource.Lambda;
42import org.onosproject.net.resource.LambdaResourceAllocation;
43import org.onosproject.net.resource.LinkResourceAllocations;
44import org.onosproject.net.resource.LinkResourceRequest;
45import org.onosproject.net.resource.LinkResourceService;
46import org.onosproject.net.resource.ResourceAllocation;
47import org.onosproject.net.resource.ResourceType;
48import org.onosproject.net.topology.TopologyService;
weibitf32383b2014-10-22 10:17:31 -070049import org.slf4j.Logger;
50
Brian O'Connor64a0369d2015-02-20 22:02:59 -080051import java.util.Collection;
52import java.util.List;
Thomas Vachuska425a2d72014-10-29 11:28:28 -070053
Brian O'Connorabafb502014-12-02 22:26:20 -080054import static org.onosproject.net.flow.DefaultTrafficTreatment.builder;
Thomas Vachuska425a2d72014-10-29 11:28:28 -070055import static org.slf4j.LoggerFactory.getLogger;
weibitf32383b2014-10-22 10:17:31 -070056
57/**
Brian O'Connorabafb502014-12-02 22:26:20 -080058 * Installer for {@link org.onosproject.net.intent.OpticalPathIntent optical path connectivity intents}.
weibitf32383b2014-10-22 10:17:31 -070059 */
weibitf32383b2014-10-22 10:17:31 -070060@Component(immediate = true)
61public class OpticalPathIntentInstaller implements IntentInstaller<OpticalPathIntent> {
weibitf32383b2014-10-22 10:17:31 -070062 private final Logger log = getLogger(getClass());
63
64 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 protected IntentExtensionService intentManager;
66
67 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
68 protected FlowRuleService flowRuleService;
69
70 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
71 protected CoreService coreService;
72
weibit7e583462014-10-23 10:14:05 -070073 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
74 protected TopologyService topologyService;
75
76 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
77 protected LinkResourceService resourceService;
78
weibitf32383b2014-10-22 10:17:31 -070079 private ApplicationId appId;
80
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -080081 static final short SIGNAL_TYPE = (short) 1;
weibit7e583462014-10-23 10:14:05 -070082
weibitf32383b2014-10-22 10:17:31 -070083 @Activate
84 public void activate() {
Brian O'Connorabafb502014-12-02 22:26:20 -080085 appId = coreService.registerApplication("org.onosproject.net.intent");
weibitf32383b2014-10-22 10:17:31 -070086 intentManager.registerInstaller(OpticalPathIntent.class, this);
87 }
88
89 @Deactivate
90 public void deactivate() {
91 intentManager.unregisterInstaller(OpticalPathIntent.class);
92 }
93
94 @Override
Brian O'Connor64a0369d2015-02-20 22:02:59 -080095 public List<Collection<FlowRuleOperation>> install(OpticalPathIntent intent) {
weibit9e622ac2014-10-23 13:45:44 -070096 LinkResourceAllocations allocations = assignWavelength(intent);
Ray Milkey71ade562015-02-18 15:08:07 -080097 return generateRules(intent, allocations, FlowRuleOperation.Type.ADD);
Brian O'Connor41718fc2014-10-30 16:57:21 -070098 }
weibitf32383b2014-10-22 10:17:31 -070099
Brian O'Connor41718fc2014-10-30 16:57:21 -0700100 @Override
Brian O'Connor64a0369d2015-02-20 22:02:59 -0800101 public List<Collection<FlowRuleOperation>> uninstall(OpticalPathIntent intent) {
Brian O'Connor41718fc2014-10-30 16:57:21 -0700102 LinkResourceAllocations allocations = resourceService.getAllocations(intent.id());
Brian O'Connor64a0369d2015-02-20 22:02:59 -0800103 List<Collection<FlowRuleOperation>> rules = generateRules(intent, allocations, FlowRuleOperation.Type.REMOVE);
Brian O'Connor772852a2014-11-17 15:51:19 -0800104 log.info("uninstall rules: {}", rules);
105 return rules;
Brian O'Connor41718fc2014-10-30 16:57:21 -0700106 }
107
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700108 @Override
Brian O'Connor64a0369d2015-02-20 22:02:59 -0800109 public List<Collection<FlowRuleOperation>> replace(OpticalPathIntent oldIntent,
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700110 OpticalPathIntent newIntent) {
111 // FIXME: implement this
Brian O'Connor64a0369d2015-02-20 22:02:59 -0800112 List<Collection<FlowRuleOperation>> batches = Lists.newArrayList();
Brian O'Connor772852a2014-11-17 15:51:19 -0800113 batches.addAll(uninstall(oldIntent));
114 batches.addAll(install(newIntent));
115 return batches;
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700116 }
117
Brian O'Connor41718fc2014-10-30 16:57:21 -0700118 private LinkResourceAllocations assignWavelength(OpticalPathIntent intent) {
119 LinkResourceRequest.Builder request = DefaultLinkResourceRequest.builder(intent.id(),
120 intent.path().links())
121 .addLambdaRequest();
122 LinkResourceAllocations retLambda = resourceService.requestResources(request.build());
123 return retLambda;
124 }
125
Brian O'Connor64a0369d2015-02-20 22:02:59 -0800126 private List<Collection<FlowRuleOperation>> generateRules(OpticalPathIntent intent,
Ray Milkey71ade562015-02-18 15:08:07 -0800127 LinkResourceAllocations allocations,
128 FlowRuleOperation.Type operation) {
weibit9e622ac2014-10-23 13:45:44 -0700129 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800130 selectorBuilder.matchInPort(intent.src().port());
weibitf32383b2014-10-22 10:17:31 -0700131
Brian O'Connor64a0369d2015-02-20 22:02:59 -0800132 List<FlowRuleOperation> rules = Lists.newLinkedList();
Brian O'Connor086724e2014-10-23 15:47:32 -0700133 ConnectPoint prev = intent.src();
weibitf32383b2014-10-22 10:17:31 -0700134
Brian O'Connor8b6b1722014-12-08 01:28:50 -0800135 //FIXME check for null allocations
weibit9e622ac2014-10-23 13:45:44 -0700136 //TODO throw exception if the lambda was not assigned successfully
137 for (Link link : intent.path().links()) {
138 Lambda la = null;
139 for (ResourceAllocation allocation : allocations.getResourceAllocation(link)) {
140 if (allocation.type() == ResourceType.LAMBDA) {
141 la = ((LambdaResourceAllocation) allocation).lambda();
142 break;
143 }
144 }
145
146 if (la == null) {
147 log.info("Lambda was not assigned successfully");
148 return null;
149 }
150
weibit253c8652014-10-23 16:30:03 -0700151 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
weibit2543d5a2014-10-23 14:13:35 -0700152 treatmentBuilder.setLambda((short) la.toInt());
Marc De Leenheerad48f882015-03-17 17:45:11 -0700153 treatmentBuilder.setOutput(link.src().port());
weibit9e622ac2014-10-23 13:45:44 -0700154
155 FlowRule rule = new DefaultFlowRule(prev.deviceId(),
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700156 selectorBuilder.build(),
157 treatmentBuilder.build(),
158 100,
159 appId,
160 100,
161 true);
weibitaca14602014-10-24 10:26:26 -0700162
Ray Milkey71ade562015-02-18 15:08:07 -0800163 rules.add(new FlowRuleOperation(rule, operation));
weibit9e622ac2014-10-23 13:45:44 -0700164
weibit7e583462014-10-23 10:14:05 -0700165 prev = link.dst();
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800166 selectorBuilder.matchInPort(link.dst().port());
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800167 selectorBuilder.matchOpticalSignalType(SIGNAL_TYPE); //todo
weibit2543d5a2014-10-23 14:13:35 -0700168 selectorBuilder.matchLambda((short) la.toInt());
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800169
weibit7e583462014-10-23 10:14:05 -0700170 }
weibit9e622ac2014-10-23 13:45:44 -0700171
172 // build the last T port rule
Brian O'Connor41718fc2014-10-30 16:57:21 -0700173 TrafficTreatment.Builder treatmentLast = builder();
174 treatmentLast.setOutput(intent.dst().port());
Brian O'Connor086724e2014-10-23 15:47:32 -0700175 FlowRule rule = new DefaultFlowRule(intent.dst().deviceId(),
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700176 selectorBuilder.build(),
Brian O'Connor41718fc2014-10-30 16:57:21 -0700177 treatmentLast.build(),
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700178 100,
179 appId,
180 100,
181 true);
Ray Milkey71ade562015-02-18 15:08:07 -0800182 rules.add(new FlowRuleOperation(rule, operation));
weibit9e622ac2014-10-23 13:45:44 -0700183
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800184 //FIXME change to new api
Ray Milkey71ade562015-02-18 15:08:07 -0800185 return Lists.newArrayList(ImmutableSet.of(rules));
weibit7e583462014-10-23 10:14:05 -0700186 }
weibitf32383b2014-10-22 10:17:31 -0700187}