blob: e1bed1549ca9e03ba1fd51f46fbdcdd3d013803b [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 com.google.common.collect.HashMultimap;
19import com.google.common.collect.SetMultimap;
20import 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;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080027import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.DeviceId;
29import org.onosproject.net.Link;
30import org.onosproject.net.PortNumber;
31import org.onosproject.net.flow.DefaultFlowRule;
32import org.onosproject.net.flow.DefaultTrafficSelector;
33import org.onosproject.net.flow.DefaultTrafficTreatment;
34import org.onosproject.net.flow.FlowRule;
35import org.onosproject.net.flow.TrafficSelector;
36import org.onosproject.net.flow.TrafficTreatment;
37import org.onosproject.net.intent.FlowRuleIntent;
38import org.onosproject.net.intent.Intent;
39import org.onosproject.net.intent.IntentCompiler;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080040import org.onosproject.net.intent.LinkCollectionIntent;
Brian O'Connor6de2e202015-05-21 14:30:41 -070041import org.onosproject.net.resource.link.LinkResourceAllocations;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080042
43import java.util.ArrayList;
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070044import java.util.Collections;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080045import java.util.List;
46import java.util.Set;
47import java.util.stream.Collectors;
48
49@Component(immediate = true)
50public class LinkCollectionIntentCompiler implements IntentCompiler<LinkCollectionIntent> {
51
52 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080053 protected IntentConfigurableRegistrator registrator;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080054
55 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
56 protected CoreService coreService;
57
58 private ApplicationId appId;
59
60 @Activate
61 public void activate() {
62 appId = coreService.registerApplication("org.onosproject.net.intent");
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080063 registrator.registerCompiler(LinkCollectionIntent.class, this, false);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080064 }
65
66 @Deactivate
67 public void deactivate() {
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080068 registrator.unregisterCompiler(LinkCollectionIntent.class, false);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080069 }
70
71 @Override
72 public List<Intent> compile(LinkCollectionIntent intent, List<Intent> installable,
73 Set<LinkResourceAllocations> resources) {
74 SetMultimap<DeviceId, PortNumber> inputPorts = HashMultimap.create();
75 SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create();
76
77 for (Link link : intent.links()) {
Jonathan Hart066244c2015-06-23 09:46:19 -070078 inputPorts.put(link.dst().deviceId(), link.dst().port());
79 outputPorts.put(link.src().deviceId(), link.src().port());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080080 }
81
82 for (ConnectPoint ingressPoint : intent.ingressPoints()) {
83 inputPorts.put(ingressPoint.deviceId(), ingressPoint.port());
84 }
85
86 for (ConnectPoint egressPoint : intent.egressPoints()) {
87 outputPorts.put(egressPoint.deviceId(), egressPoint.port());
88 }
89
90 List<FlowRule> rules = new ArrayList<>();
91 for (DeviceId deviceId: outputPorts.keys()) {
92 rules.addAll(createRules(intent, deviceId, inputPorts.get(deviceId), outputPorts.get(deviceId)));
93 }
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070094 return Collections.singletonList(new FlowRuleIntent(appId, rules, intent.resources()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080095 }
96
97 private List<FlowRule> createRules(LinkCollectionIntent intent, DeviceId deviceId,
98 Set<PortNumber> inPorts, Set<PortNumber> outPorts) {
99 Set<PortNumber> ingressPorts = intent.ingressPoints().stream()
100 .filter(point -> point.deviceId().equals(deviceId))
101 .map(ConnectPoint::port)
102 .collect(Collectors.toSet());
103
104 TrafficTreatment.Builder defaultTreatmentBuilder = DefaultTrafficTreatment.builder();
105 outPorts.stream()
106 .forEach(defaultTreatmentBuilder::setOutput);
107 TrafficTreatment defaultTreatment = defaultTreatmentBuilder.build();
108
109 TrafficTreatment.Builder ingressTreatmentBuilder = DefaultTrafficTreatment.builder(intent.treatment());
110 outPorts.stream()
111 .forEach(ingressTreatmentBuilder::setOutput);
112 TrafficTreatment ingressTreatment = ingressTreatmentBuilder.build();
113
114 List<FlowRule> rules = new ArrayList<>(inPorts.size());
115 for (PortNumber inPort: inPorts) {
116 TrafficSelector selector = DefaultTrafficSelector.builder(intent.selector()).matchInPort(inPort).build();
117 TrafficTreatment treatment;
118 if (ingressPorts.contains(inPort)) {
119 treatment = ingressTreatment;
120 } else {
121 treatment = defaultTreatment;
122 }
123
Brian O'Connor81134662015-06-25 17:23:33 -0400124 FlowRule rule = DefaultFlowRule.builder()
125 .forDevice(deviceId)
126 .withSelector(selector)
127 .withTreatment(treatment)
128 .withPriority(intent.priority())
129 .fromApp(appId)
130 .makePermanent()
131 .build();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800132 rules.add(rule);
133 }
134
135 return rules;
136 }
137}