blob: 0d6ad0ac3a448b58747348da06d35208e8ede495 [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;
Pier Ventred48320e2016-08-17 16:25:47 -070019import com.google.common.collect.ImmutableSet;
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080020import com.google.common.collect.SetMultimap;
Pier Ventred48320e2016-08-17 16:25:47 -070021import com.google.common.collect.Sets;
Ray Milkeydb74ec72016-03-01 10:35:24 -080022import org.apache.felix.scr.annotations.Activate;
23import org.apache.felix.scr.annotations.Component;
24import org.apache.felix.scr.annotations.Deactivate;
25import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
27import org.onosproject.core.ApplicationId;
28import org.onosproject.core.CoreService;
Ray Milkeydb74ec72016-03-01 10:35:24 -080029import org.onosproject.net.DeviceId;
Ray Milkeydb74ec72016-03-01 10:35:24 -080030import org.onosproject.net.PortNumber;
Ray Milkeydb74ec72016-03-01 10:35:24 -080031import org.onosproject.net.flowobjective.DefaultForwardingObjective;
Michele Santuarid2c8b152016-03-30 17:57:56 -070032import org.onosproject.net.flowobjective.DefaultNextObjective;
33import org.onosproject.net.flowobjective.FlowObjectiveService;
Ray Milkeydb74ec72016-03-01 10:35:24 -080034import org.onosproject.net.flowobjective.ForwardingObjective;
Michele Santuarid2c8b152016-03-30 17:57:56 -070035import org.onosproject.net.flowobjective.NextObjective;
Ray Milkeydb74ec72016-03-01 10:35:24 -080036import org.onosproject.net.flowobjective.Objective;
37import org.onosproject.net.intent.FlowObjectiveIntent;
38import org.onosproject.net.intent.Intent;
39import org.onosproject.net.intent.IntentCompiler;
Ray Milkeydb74ec72016-03-01 10:35:24 -080040import org.onosproject.net.intent.LinkCollectionIntent;
Ray Milkeydb74ec72016-03-01 10:35:24 -080041
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080042import java.util.ArrayList;
43import java.util.Collections;
44import java.util.List;
45import java.util.Set;
Ray Milkeydb74ec72016-03-01 10:35:24 -080046
47/**
48 * Compiler to produce flow objectives from link collections.
49 */
50@Component(immediate = true)
Pier Ventred48320e2016-08-17 16:25:47 -070051public class LinkCollectionIntentFlowObjectiveCompiler
52 extends LinkCollectionCompiler<Objective>
53 implements IntentCompiler<LinkCollectionIntent> {
Ray Milkeydb74ec72016-03-01 10:35:24 -080054
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
Michele Santuarid2c8b152016-03-30 17:57:56 -070061 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
62 protected FlowObjectiveService flowObjectiveService;
63
Ray Milkeydb74ec72016-03-01 10:35:24 -080064 private ApplicationId appId;
65
66 @Activate
67 public void activate() {
68 appId = coreService.registerApplication("org.onosproject.net.intent");
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080069 registrator.registerCompiler(LinkCollectionIntent.class, this, true);
Ray Milkeydb74ec72016-03-01 10:35:24 -080070 }
71
72 @Deactivate
73 public void deactivate() {
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080074 registrator.unregisterCompiler(LinkCollectionIntent.class, true);
Ray Milkeydb74ec72016-03-01 10:35:24 -080075 }
76
77 @Override
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080078 public List<Intent> compile(LinkCollectionIntent intent, List<Intent> installable) {
Pier Ventred48320e2016-08-17 16:25:47 -070079
Ray Milkeydb74ec72016-03-01 10:35:24 -080080 SetMultimap<DeviceId, PortNumber> inputPorts = HashMultimap.create();
81 SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create();
82
Pier Ventred48320e2016-08-17 16:25:47 -070083 computePorts(intent, inputPorts, outputPorts);
Ray Milkeydb74ec72016-03-01 10:35:24 -080084
85 List<Objective> objectives = new ArrayList<>();
86 List<DeviceId> devices = new ArrayList<>();
87 for (DeviceId deviceId: outputPorts.keys()) {
88 List<Objective> deviceObjectives =
89 createRules(intent,
90 deviceId,
91 inputPorts.get(deviceId),
92 outputPorts.get(deviceId));
93 deviceObjectives.forEach(objective -> {
94 objectives.add(objective);
95 devices.add(deviceId);
96 });
97 }
98 return Collections.singletonList(
99 new FlowObjectiveIntent(appId, devices, objectives, intent.resources()));
100 }
101
Pier Ventred48320e2016-08-17 16:25:47 -0700102 @Override
103 protected List<Objective> createRules(LinkCollectionIntent intent, DeviceId deviceId,
Ray Milkeydb74ec72016-03-01 10:35:24 -0800104 Set<PortNumber> inPorts, Set<PortNumber> outPorts) {
Ray Milkeydb74ec72016-03-01 10:35:24 -0800105
Pier Ventred48320e2016-08-17 16:25:47 -0700106 Set<PortNumber> ingressPorts = Sets.newHashSet();
107 Set<PortNumber> egressPorts = Sets.newHashSet();
Ray Milkeydb74ec72016-03-01 10:35:24 -0800108
Pier Ventred48320e2016-08-17 16:25:47 -0700109 computePorts(intent, deviceId, ingressPorts, egressPorts);
Ray Milkeydb74ec72016-03-01 10:35:24 -0800110
111 List<Objective> objectives = new ArrayList<>(inPorts.size());
Pier Ventred48320e2016-08-17 16:25:47 -0700112 Set<PortNumber> copyIngressPorts = ImmutableSet.copyOf(ingressPorts);
113 Set<PortNumber> copyEgressPorts = ImmutableSet.copyOf(egressPorts);
114
115 inPorts.forEach(inport -> {
116 ForwardingInstructions instructions = this.createForwardingInstructions(intent,
117 inport,
Pier Ventre647138f2016-08-26 17:32:44 -0700118 deviceId,
Pier Ventred48320e2016-08-17 16:25:47 -0700119 outPorts,
120 copyIngressPorts,
121 copyEgressPorts);
Ray Milkeydb74ec72016-03-01 10:35:24 -0800122
Michele Santuarid2c8b152016-03-30 17:57:56 -0700123 NextObjective nextObjective = DefaultNextObjective.builder()
124 .withId(flowObjectiveService.allocateNextId())
Pier Ventred48320e2016-08-17 16:25:47 -0700125 .addTreatment(instructions.treatment())
Michele Santuarid2c8b152016-03-30 17:57:56 -0700126 .withType(NextObjective.Type.SIMPLE)
127 .fromApp(appId)
128 .makePermanent().add();
129 objectives.add(nextObjective);
130
131 objectives.add(DefaultForwardingObjective.builder()
Pier Ventred48320e2016-08-17 16:25:47 -0700132 .withSelector(instructions.selector())
Michele Santuarid2c8b152016-03-30 17:57:56 -0700133 .nextStep(nextObjective.id())
Ray Milkeydb74ec72016-03-01 10:35:24 -0800134 .withPriority(intent.priority())
135 .fromApp(appId)
136 .makePermanent()
Thomas Vachuska8378ccf2016-03-02 18:02:17 -0800137 .withFlag(ForwardingObjective.Flag.SPECIFIC)
Michele Santuarid2c8b152016-03-30 17:57:56 -0700138 .add());
Pier Ventred48320e2016-08-17 16:25:47 -0700139 }
140 );
Ray Milkeydb74ec72016-03-01 10:35:24 -0800141
142 return objectives;
143 }
Pier Ventred48320e2016-08-17 16:25:47 -0700144
Ray Milkeydb74ec72016-03-01 10:35:24 -0800145}