Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent.impl; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 17 | |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame^] | 18 | import java.util.List; |
| 19 | import java.util.Set; |
| 20 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.CoreService; |
| 28 | import org.onosproject.net.ConnectPoint; |
| 29 | import org.onosproject.net.Link; |
| 30 | import org.onosproject.net.flow.DefaultFlowRule; |
| 31 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 32 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 33 | import org.onosproject.net.flow.FlowRule; |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame^] | 34 | import org.onosproject.net.flow.FlowRuleOperation; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.net.flow.FlowRuleService; |
| 36 | import org.onosproject.net.flow.TrafficSelector; |
| 37 | import org.onosproject.net.flow.TrafficTreatment; |
| 38 | import org.onosproject.net.intent.IntentExtensionService; |
| 39 | import org.onosproject.net.intent.IntentInstaller; |
| 40 | import org.onosproject.net.intent.OpticalPathIntent; |
| 41 | import org.onosproject.net.resource.DefaultLinkResourceRequest; |
| 42 | import org.onosproject.net.resource.Lambda; |
| 43 | import org.onosproject.net.resource.LambdaResourceAllocation; |
| 44 | import org.onosproject.net.resource.LinkResourceAllocations; |
| 45 | import org.onosproject.net.resource.LinkResourceRequest; |
| 46 | import org.onosproject.net.resource.LinkResourceService; |
| 47 | import org.onosproject.net.resource.ResourceAllocation; |
| 48 | import org.onosproject.net.resource.ResourceType; |
| 49 | import org.onosproject.net.topology.TopologyService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 50 | import org.slf4j.Logger; |
| 51 | |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame^] | 52 | import com.google.common.collect.ImmutableSet; |
| 53 | import com.google.common.collect.Lists; |
| 54 | import com.google.common.collect.Sets; |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 55 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 56 | import static org.onosproject.net.flow.DefaultTrafficTreatment.builder; |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 57 | import static org.slf4j.LoggerFactory.getLogger; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 58 | |
| 59 | /** |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 60 | * Installer for {@link org.onosproject.net.intent.OpticalPathIntent optical path connectivity intents}. |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 61 | */ |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 62 | @Component(immediate = true) |
| 63 | public class OpticalPathIntentInstaller implements IntentInstaller<OpticalPathIntent> { |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 64 | 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 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 76 | protected TopologyService topologyService; |
| 77 | |
| 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 79 | protected LinkResourceService resourceService; |
| 80 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 81 | private ApplicationId appId; |
| 82 | |
Praseed Balakrishnan | 2dd5abd | 2014-11-03 14:56:28 -0800 | [diff] [blame] | 83 | static final short SIGNAL_TYPE = (short) 1; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 84 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 85 | @Activate |
| 86 | public void activate() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 87 | appId = coreService.registerApplication("org.onosproject.net.intent"); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 88 | intentManager.registerInstaller(OpticalPathIntent.class, this); |
| 89 | } |
| 90 | |
| 91 | @Deactivate |
| 92 | public void deactivate() { |
| 93 | intentManager.unregisterInstaller(OpticalPathIntent.class); |
| 94 | } |
| 95 | |
| 96 | @Override |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame^] | 97 | public List<Set<FlowRuleOperation>> install(OpticalPathIntent intent) { |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 98 | LinkResourceAllocations allocations = assignWavelength(intent); |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame^] | 99 | return generateRules(intent, allocations, FlowRuleOperation.Type.ADD); |
Brian O'Connor | 41718fc | 2014-10-30 16:57:21 -0700 | [diff] [blame] | 100 | } |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 101 | |
Brian O'Connor | 41718fc | 2014-10-30 16:57:21 -0700 | [diff] [blame] | 102 | @Override |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame^] | 103 | public List<Set<FlowRuleOperation>> uninstall(OpticalPathIntent intent) { |
Brian O'Connor | 41718fc | 2014-10-30 16:57:21 -0700 | [diff] [blame] | 104 | LinkResourceAllocations allocations = resourceService.getAllocations(intent.id()); |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame^] | 105 | List<Set<FlowRuleOperation>> rules = generateRules(intent, allocations, FlowRuleOperation.Type.REMOVE); |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 106 | log.info("uninstall rules: {}", rules); |
| 107 | return rules; |
Brian O'Connor | 41718fc | 2014-10-30 16:57:21 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 110 | @Override |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame^] | 111 | public List<Set<FlowRuleOperation>> replace(OpticalPathIntent oldIntent, |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 112 | OpticalPathIntent newIntent) { |
| 113 | // FIXME: implement this |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame^] | 114 | List<Set<FlowRuleOperation>> batches = Lists.newArrayList(); |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 115 | batches.addAll(uninstall(oldIntent)); |
| 116 | batches.addAll(install(newIntent)); |
| 117 | return batches; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Brian O'Connor | 41718fc | 2014-10-30 16:57:21 -0700 | [diff] [blame] | 120 | 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 Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame^] | 128 | private List<Set<FlowRuleOperation>> generateRules(OpticalPathIntent intent, |
| 129 | LinkResourceAllocations allocations, |
| 130 | FlowRuleOperation.Type operation) { |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 131 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 132 | selectorBuilder.matchInPort(intent.src().port()); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 133 | |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame^] | 134 | Set<FlowRuleOperation> rules = Sets.newHashSet(); |
Brian O'Connor | 086724e | 2014-10-23 15:47:32 -0700 | [diff] [blame] | 135 | ConnectPoint prev = intent.src(); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 136 | |
Brian O'Connor | 8b6b172 | 2014-12-08 01:28:50 -0800 | [diff] [blame] | 137 | //FIXME check for null allocations |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 138 | //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 | |
weibit | 253c865 | 2014-10-23 16:30:03 -0700 | [diff] [blame] | 153 | TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder(); |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 154 | treatmentBuilder.setOutput(link.src().port()); |
weibit | 2543d5a | 2014-10-23 14:13:35 -0700 | [diff] [blame] | 155 | treatmentBuilder.setLambda((short) la.toInt()); |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 156 | |
| 157 | FlowRule rule = new DefaultFlowRule(prev.deviceId(), |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 158 | selectorBuilder.build(), |
| 159 | treatmentBuilder.build(), |
| 160 | 100, |
| 161 | appId, |
| 162 | 100, |
| 163 | true); |
weibit | aca1460 | 2014-10-24 10:26:26 -0700 | [diff] [blame] | 164 | |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame^] | 165 | rules.add(new FlowRuleOperation(rule, operation)); |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 166 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 167 | prev = link.dst(); |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 168 | selectorBuilder.matchInPort(link.dst().port()); |
Praseed Balakrishnan | 2dd5abd | 2014-11-03 14:56:28 -0800 | [diff] [blame] | 169 | selectorBuilder.matchOpticalSignalType(SIGNAL_TYPE); //todo |
weibit | 2543d5a | 2014-10-23 14:13:35 -0700 | [diff] [blame] | 170 | selectorBuilder.matchLambda((short) la.toInt()); |
Praseed Balakrishnan | 2dd5abd | 2014-11-03 14:56:28 -0800 | [diff] [blame] | 171 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 172 | } |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 173 | |
| 174 | // build the last T port rule |
Brian O'Connor | 41718fc | 2014-10-30 16:57:21 -0700 | [diff] [blame] | 175 | TrafficTreatment.Builder treatmentLast = builder(); |
| 176 | treatmentLast.setOutput(intent.dst().port()); |
Brian O'Connor | 086724e | 2014-10-23 15:47:32 -0700 | [diff] [blame] | 177 | FlowRule rule = new DefaultFlowRule(intent.dst().deviceId(), |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 178 | selectorBuilder.build(), |
Brian O'Connor | 41718fc | 2014-10-30 16:57:21 -0700 | [diff] [blame] | 179 | treatmentLast.build(), |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 180 | 100, |
| 181 | appId, |
| 182 | 100, |
| 183 | true); |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame^] | 184 | rules.add(new FlowRuleOperation(rule, operation)); |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 185 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 186 | //FIXME change to new api |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame^] | 187 | return Lists.newArrayList(ImmutableSet.of(rules)); |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 188 | } |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 189 | } |