blob: d66cd80b1d6138a60338f75823243a9692cbc99b [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;
Ray Milkeydb74ec72016-03-01 10:35:24 -080042
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080043import java.util.ArrayList;
44import java.util.Collections;
45import java.util.List;
46import java.util.Set;
47import java.util.stream.Collectors;
Ray Milkeydb74ec72016-03-01 10:35:24 -080048
49/**
50 * Compiler to produce flow objectives from link collections.
51 */
52@Component(immediate = true)
53public class LinkCollectionIntentFlowObjectivesCompiler implements IntentCompiler<LinkCollectionIntent> {
54
55 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080056 protected IntentConfigurableRegistrator registrator;
Ray Milkeydb74ec72016-03-01 10:35:24 -080057
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
59 protected CoreService coreService;
60
61 private ApplicationId appId;
62
63 @Activate
64 public void activate() {
65 appId = coreService.registerApplication("org.onosproject.net.intent");
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080066 registrator.registerCompiler(LinkCollectionIntent.class, this, true);
Ray Milkeydb74ec72016-03-01 10:35:24 -080067 }
68
69 @Deactivate
70 public void deactivate() {
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080071 registrator.unregisterCompiler(LinkCollectionIntent.class, true);
Ray Milkeydb74ec72016-03-01 10:35:24 -080072 }
73
74 @Override
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080075 public List<Intent> compile(LinkCollectionIntent intent, List<Intent> installable) {
Ray Milkeydb74ec72016-03-01 10:35:24 -080076 SetMultimap<DeviceId, PortNumber> inputPorts = HashMultimap.create();
77 SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create();
78
79 for (Link link : intent.links()) {
80 inputPorts.put(link.dst().deviceId(), link.dst().port());
81 outputPorts.put(link.src().deviceId(), link.src().port());
82 }
83
84 for (ConnectPoint ingressPoint : intent.ingressPoints()) {
85 inputPorts.put(ingressPoint.deviceId(), ingressPoint.port());
86 }
87
88 for (ConnectPoint egressPoint : intent.egressPoints()) {
89 outputPorts.put(egressPoint.deviceId(), egressPoint.port());
90 }
91
92 List<Objective> objectives = new ArrayList<>();
93 List<DeviceId> devices = new ArrayList<>();
94 for (DeviceId deviceId: outputPorts.keys()) {
95 List<Objective> deviceObjectives =
96 createRules(intent,
97 deviceId,
98 inputPorts.get(deviceId),
99 outputPorts.get(deviceId));
100 deviceObjectives.forEach(objective -> {
101 objectives.add(objective);
102 devices.add(deviceId);
103 });
104 }
105 return Collections.singletonList(
106 new FlowObjectiveIntent(appId, devices, objectives, intent.resources()));
107 }
108
109 private List<Objective> createRules(LinkCollectionIntent intent, DeviceId deviceId,
110 Set<PortNumber> inPorts, Set<PortNumber> outPorts) {
111 Set<PortNumber> ingressPorts = intent.ingressPoints().stream()
112 .filter(point -> point.deviceId().equals(deviceId))
113 .map(ConnectPoint::port)
114 .collect(Collectors.toSet());
115
116 TrafficTreatment.Builder defaultTreatmentBuilder = DefaultTrafficTreatment.builder();
117 outPorts.stream()
118 .forEach(defaultTreatmentBuilder::setOutput);
119 TrafficTreatment defaultTreatment = defaultTreatmentBuilder.build();
120
121 TrafficTreatment.Builder ingressTreatmentBuilder = DefaultTrafficTreatment.builder(intent.treatment());
122 outPorts.stream()
123 .forEach(ingressTreatmentBuilder::setOutput);
124 TrafficTreatment ingressTreatment = ingressTreatmentBuilder.build();
125
126 List<Objective> objectives = new ArrayList<>(inPorts.size());
127 for (PortNumber inPort: inPorts) {
128 TrafficSelector selector = DefaultTrafficSelector.builder(intent.selector()).matchInPort(inPort).build();
129 TrafficTreatment treatment;
130 if (ingressPorts.contains(inPort)) {
131 treatment = ingressTreatment;
132 } else {
133 treatment = defaultTreatment;
134 }
135
136 Objective objective = DefaultForwardingObjective.builder()
137 .withSelector(selector)
138 .withTreatment(treatment)
139 .withPriority(intent.priority())
140 .fromApp(appId)
141 .makePermanent()
Thomas Vachuska8378ccf2016-03-02 18:02:17 -0800142 .withFlag(ForwardingObjective.Flag.SPECIFIC)
Ray Milkeydb74ec72016-03-01 10:35:24 -0800143 .add();
144
145 objectives.add(objective);
146 }
147
148 return objectives;
149 }
150}