blob: 04e740dd00bf01790937c54249f7d38b2004b8c8 [file] [log] [blame]
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08003 *
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;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080041
42import java.util.ArrayList;
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070043import java.util.Collections;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080044import java.util.List;
45import java.util.Set;
46import java.util.stream.Collectors;
47
48@Component(immediate = true)
49public class LinkCollectionIntentCompiler implements IntentCompiler<LinkCollectionIntent> {
50
51 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080052 protected IntentConfigurableRegistrator registrator;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080053
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected CoreService coreService;
56
57 private ApplicationId appId;
58
59 @Activate
60 public void activate() {
61 appId = coreService.registerApplication("org.onosproject.net.intent");
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080062 registrator.registerCompiler(LinkCollectionIntent.class, this, false);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080063 }
64
65 @Deactivate
66 public void deactivate() {
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080067 registrator.unregisterCompiler(LinkCollectionIntent.class, false);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080068 }
69
70 @Override
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080071 public List<Intent> compile(LinkCollectionIntent intent, List<Intent> installable) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080072 SetMultimap<DeviceId, PortNumber> inputPorts = HashMultimap.create();
73 SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create();
74
75 for (Link link : intent.links()) {
Jonathan Hart066244c2015-06-23 09:46:19 -070076 inputPorts.put(link.dst().deviceId(), link.dst().port());
77 outputPorts.put(link.src().deviceId(), link.src().port());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080078 }
79
80 for (ConnectPoint ingressPoint : intent.ingressPoints()) {
81 inputPorts.put(ingressPoint.deviceId(), ingressPoint.port());
82 }
83
84 for (ConnectPoint egressPoint : intent.egressPoints()) {
85 outputPorts.put(egressPoint.deviceId(), egressPoint.port());
86 }
87
88 List<FlowRule> rules = new ArrayList<>();
89 for (DeviceId deviceId: outputPorts.keys()) {
90 rules.addAll(createRules(intent, deviceId, inputPorts.get(deviceId), outputPorts.get(deviceId)));
91 }
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070092 return Collections.singletonList(new FlowRuleIntent(appId, rules, intent.resources()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080093 }
94
95 private List<FlowRule> createRules(LinkCollectionIntent intent, DeviceId deviceId,
96 Set<PortNumber> inPorts, Set<PortNumber> outPorts) {
97 Set<PortNumber> ingressPorts = intent.ingressPoints().stream()
98 .filter(point -> point.deviceId().equals(deviceId))
99 .map(ConnectPoint::port)
100 .collect(Collectors.toSet());
101
102 TrafficTreatment.Builder defaultTreatmentBuilder = DefaultTrafficTreatment.builder();
103 outPorts.stream()
104 .forEach(defaultTreatmentBuilder::setOutput);
105 TrafficTreatment defaultTreatment = defaultTreatmentBuilder.build();
106
107 TrafficTreatment.Builder ingressTreatmentBuilder = DefaultTrafficTreatment.builder(intent.treatment());
108 outPorts.stream()
109 .forEach(ingressTreatmentBuilder::setOutput);
110 TrafficTreatment ingressTreatment = ingressTreatmentBuilder.build();
111
112 List<FlowRule> rules = new ArrayList<>(inPorts.size());
113 for (PortNumber inPort: inPorts) {
114 TrafficSelector selector = DefaultTrafficSelector.builder(intent.selector()).matchInPort(inPort).build();
115 TrafficTreatment treatment;
116 if (ingressPorts.contains(inPort)) {
117 treatment = ingressTreatment;
118 } else {
119 treatment = defaultTreatment;
120 }
121
Brian O'Connor81134662015-06-25 17:23:33 -0400122 FlowRule rule = DefaultFlowRule.builder()
123 .forDevice(deviceId)
124 .withSelector(selector)
125 .withTreatment(treatment)
126 .withPriority(intent.priority())
127 .fromApp(appId)
128 .makePermanent()
129 .build();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800130 rules.add(rule);
131 }
132
133 return rules;
134 }
135}