blob: 1ef3ee2d4c7db738fefa0d3912c4a3c14ed6d546 [file] [log] [blame]
Ray Milkeydb74ec72016-03-01 10:35:24 -08001/*
2 * Copyright 2016 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
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080018import com.google.common.collect.HashMultimap;
19import com.google.common.collect.SetMultimap;
Ray Milkeydb74ec72016-03-01 10:35:24 -080020import 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;
27import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.DeviceId;
29import org.onosproject.net.Link;
30import org.onosproject.net.PortNumber;
31import org.onosproject.net.flow.DefaultTrafficSelector;
32import org.onosproject.net.flow.DefaultTrafficTreatment;
33import org.onosproject.net.flow.TrafficSelector;
34import org.onosproject.net.flow.TrafficTreatment;
35import org.onosproject.net.flowobjective.DefaultForwardingObjective;
36import org.onosproject.net.flowobjective.ForwardingObjective;
37import org.onosproject.net.flowobjective.Objective;
38import org.onosproject.net.intent.FlowObjectiveIntent;
39import org.onosproject.net.intent.Intent;
40import org.onosproject.net.intent.IntentCompiler;
Ray Milkeydb74ec72016-03-01 10:35:24 -080041import org.onosproject.net.intent.LinkCollectionIntent;
42import org.onosproject.net.resource.link.LinkResourceAllocations;
43
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080044import java.util.ArrayList;
45import java.util.Collections;
46import java.util.List;
47import java.util.Set;
48import java.util.stream.Collectors;
Ray Milkeydb74ec72016-03-01 10:35:24 -080049
50/**
51 * Compiler to produce flow objectives from link collections.
52 */
53@Component(immediate = true)
54public class LinkCollectionIntentFlowObjectivesCompiler implements IntentCompiler<LinkCollectionIntent> {
55
56 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080057 protected IntentConfigurableRegistrator registrator;
Ray Milkeydb74ec72016-03-01 10:35:24 -080058
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected CoreService coreService;
61
62 private ApplicationId appId;
63
64 @Activate
65 public void activate() {
66 appId = coreService.registerApplication("org.onosproject.net.intent");
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080067 registrator.registerCompiler(LinkCollectionIntent.class, this, true);
Ray Milkeydb74ec72016-03-01 10:35:24 -080068 }
69
70 @Deactivate
71 public void deactivate() {
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080072 registrator.unregisterCompiler(LinkCollectionIntent.class, true);
Ray Milkeydb74ec72016-03-01 10:35:24 -080073 }
74
75 @Override
76 public List<Intent> compile(LinkCollectionIntent intent, List<Intent> installable,
77 Set<LinkResourceAllocations> resources) {
78 SetMultimap<DeviceId, PortNumber> inputPorts = HashMultimap.create();
79 SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create();
80
81 for (Link link : intent.links()) {
82 inputPorts.put(link.dst().deviceId(), link.dst().port());
83 outputPorts.put(link.src().deviceId(), link.src().port());
84 }
85
86 for (ConnectPoint ingressPoint : intent.ingressPoints()) {
87 inputPorts.put(ingressPoint.deviceId(), ingressPoint.port());
88 }
89
90 for (ConnectPoint egressPoint : intent.egressPoints()) {
91 outputPorts.put(egressPoint.deviceId(), egressPoint.port());
92 }
93
94 List<Objective> objectives = new ArrayList<>();
95 List<DeviceId> devices = new ArrayList<>();
96 for (DeviceId deviceId: outputPorts.keys()) {
97 List<Objective> deviceObjectives =
98 createRules(intent,
99 deviceId,
100 inputPorts.get(deviceId),
101 outputPorts.get(deviceId));
102 deviceObjectives.forEach(objective -> {
103 objectives.add(objective);
104 devices.add(deviceId);
105 });
106 }
107 return Collections.singletonList(
108 new FlowObjectiveIntent(appId, devices, objectives, intent.resources()));
109 }
110
111 private List<Objective> createRules(LinkCollectionIntent intent, DeviceId deviceId,
112 Set<PortNumber> inPorts, Set<PortNumber> outPorts) {
113 Set<PortNumber> ingressPorts = intent.ingressPoints().stream()
114 .filter(point -> point.deviceId().equals(deviceId))
115 .map(ConnectPoint::port)
116 .collect(Collectors.toSet());
117
118 TrafficTreatment.Builder defaultTreatmentBuilder = DefaultTrafficTreatment.builder();
119 outPorts.stream()
120 .forEach(defaultTreatmentBuilder::setOutput);
121 TrafficTreatment defaultTreatment = defaultTreatmentBuilder.build();
122
123 TrafficTreatment.Builder ingressTreatmentBuilder = DefaultTrafficTreatment.builder(intent.treatment());
124 outPorts.stream()
125 .forEach(ingressTreatmentBuilder::setOutput);
126 TrafficTreatment ingressTreatment = ingressTreatmentBuilder.build();
127
128 List<Objective> objectives = new ArrayList<>(inPorts.size());
129 for (PortNumber inPort: inPorts) {
130 TrafficSelector selector = DefaultTrafficSelector.builder(intent.selector()).matchInPort(inPort).build();
131 TrafficTreatment treatment;
132 if (ingressPorts.contains(inPort)) {
133 treatment = ingressTreatment;
134 } else {
135 treatment = defaultTreatment;
136 }
137
138 Objective objective = DefaultForwardingObjective.builder()
139 .withSelector(selector)
140 .withTreatment(treatment)
141 .withPriority(intent.priority())
142 .fromApp(appId)
143 .makePermanent()
144 .withFlag(ForwardingObjective.Flag.VERSATILE)
145 .add();
146
147 objectives.add(objective);
148 }
149
150 return objectives;
151 }
152}