blob: 09c7037ac6b1200f16962afa7c9a4f8a1a80a978 [file] [log] [blame]
Ray Milkeydb74ec72016-03-01 10:35:24 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Ray Milkeydb74ec72016-03-01 10:35:24 -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
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;
Michele Santuarid2c8b152016-03-30 17:57:56 -070036import org.onosproject.net.flowobjective.DefaultNextObjective;
37import org.onosproject.net.flowobjective.FlowObjectiveService;
Ray Milkeydb74ec72016-03-01 10:35:24 -080038import org.onosproject.net.flowobjective.ForwardingObjective;
Michele Santuarid2c8b152016-03-30 17:57:56 -070039import org.onosproject.net.flowobjective.NextObjective;
Ray Milkeydb74ec72016-03-01 10:35:24 -080040import org.onosproject.net.flowobjective.Objective;
41import org.onosproject.net.intent.FlowObjectiveIntent;
42import org.onosproject.net.intent.Intent;
43import org.onosproject.net.intent.IntentCompiler;
Ray Milkeydb74ec72016-03-01 10:35:24 -080044import org.onosproject.net.intent.LinkCollectionIntent;
Ray Milkeydb74ec72016-03-01 10:35:24 -080045
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080046import java.util.ArrayList;
47import java.util.Collections;
48import java.util.List;
49import java.util.Set;
50import java.util.stream.Collectors;
Ray Milkeydb74ec72016-03-01 10:35:24 -080051
52/**
53 * Compiler to produce flow objectives from link collections.
54 */
55@Component(immediate = true)
56public class LinkCollectionIntentFlowObjectivesCompiler implements IntentCompiler<LinkCollectionIntent> {
57
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080059 protected IntentConfigurableRegistrator registrator;
Ray Milkeydb74ec72016-03-01 10:35:24 -080060
61 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
62 protected CoreService coreService;
63
Michele Santuarid2c8b152016-03-30 17:57:56 -070064 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 protected FlowObjectiveService flowObjectiveService;
66
Ray Milkeydb74ec72016-03-01 10:35:24 -080067 private ApplicationId appId;
68
69 @Activate
70 public void activate() {
71 appId = coreService.registerApplication("org.onosproject.net.intent");
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080072 registrator.registerCompiler(LinkCollectionIntent.class, this, true);
Ray Milkeydb74ec72016-03-01 10:35:24 -080073 }
74
75 @Deactivate
76 public void deactivate() {
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080077 registrator.unregisterCompiler(LinkCollectionIntent.class, true);
Ray Milkeydb74ec72016-03-01 10:35:24 -080078 }
79
80 @Override
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080081 public List<Intent> compile(LinkCollectionIntent intent, List<Intent> installable) {
Ray Milkeydb74ec72016-03-01 10:35:24 -080082 SetMultimap<DeviceId, PortNumber> inputPorts = HashMultimap.create();
83 SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create();
84
85 for (Link link : intent.links()) {
86 inputPorts.put(link.dst().deviceId(), link.dst().port());
87 outputPorts.put(link.src().deviceId(), link.src().port());
88 }
89
90 for (ConnectPoint ingressPoint : intent.ingressPoints()) {
91 inputPorts.put(ingressPoint.deviceId(), ingressPoint.port());
92 }
93
94 for (ConnectPoint egressPoint : intent.egressPoints()) {
95 outputPorts.put(egressPoint.deviceId(), egressPoint.port());
96 }
97
98 List<Objective> objectives = new ArrayList<>();
99 List<DeviceId> devices = new ArrayList<>();
100 for (DeviceId deviceId: outputPorts.keys()) {
101 List<Objective> deviceObjectives =
102 createRules(intent,
103 deviceId,
104 inputPorts.get(deviceId),
105 outputPorts.get(deviceId));
106 deviceObjectives.forEach(objective -> {
107 objectives.add(objective);
108 devices.add(deviceId);
109 });
110 }
111 return Collections.singletonList(
112 new FlowObjectiveIntent(appId, devices, objectives, intent.resources()));
113 }
114
115 private List<Objective> createRules(LinkCollectionIntent intent, DeviceId deviceId,
116 Set<PortNumber> inPorts, Set<PortNumber> outPorts) {
117 Set<PortNumber> ingressPorts = intent.ingressPoints().stream()
118 .filter(point -> point.deviceId().equals(deviceId))
119 .map(ConnectPoint::port)
120 .collect(Collectors.toSet());
121
122 TrafficTreatment.Builder defaultTreatmentBuilder = DefaultTrafficTreatment.builder();
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700123 outPorts.forEach(defaultTreatmentBuilder::setOutput);
Ray Milkeydb74ec72016-03-01 10:35:24 -0800124 TrafficTreatment defaultTreatment = defaultTreatmentBuilder.build();
125
126 TrafficTreatment.Builder ingressTreatmentBuilder = DefaultTrafficTreatment.builder(intent.treatment());
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700127 outPorts.forEach(ingressTreatmentBuilder::setOutput);
Ray Milkeydb74ec72016-03-01 10:35:24 -0800128 TrafficTreatment ingressTreatment = ingressTreatmentBuilder.build();
129
130 List<Objective> objectives = new ArrayList<>(inPorts.size());
131 for (PortNumber inPort: inPorts) {
132 TrafficSelector selector = DefaultTrafficSelector.builder(intent.selector()).matchInPort(inPort).build();
133 TrafficTreatment treatment;
134 if (ingressPorts.contains(inPort)) {
135 treatment = ingressTreatment;
136 } else {
137 treatment = defaultTreatment;
138 }
139
Michele Santuarid2c8b152016-03-30 17:57:56 -0700140 NextObjective nextObjective = DefaultNextObjective.builder()
141 .withId(flowObjectiveService.allocateNextId())
142 .addTreatment(treatment)
143 .withType(NextObjective.Type.SIMPLE)
144 .fromApp(appId)
145 .makePermanent().add();
146 objectives.add(nextObjective);
147
148 objectives.add(DefaultForwardingObjective.builder()
Ray Milkeydb74ec72016-03-01 10:35:24 -0800149 .withSelector(selector)
Michele Santuarid2c8b152016-03-30 17:57:56 -0700150 .nextStep(nextObjective.id())
Ray Milkeydb74ec72016-03-01 10:35:24 -0800151 .withPriority(intent.priority())
152 .fromApp(appId)
153 .makePermanent()
Thomas Vachuska8378ccf2016-03-02 18:02:17 -0800154 .withFlag(ForwardingObjective.Flag.SPECIFIC)
Michele Santuarid2c8b152016-03-30 17:57:56 -0700155 .add());
Ray Milkeydb74ec72016-03-01 10:35:24 -0800156 }
157
158 return objectives;
159 }
160}