blob: 0aa4e87a61bb8d3d0093f73f772fc189f979d3b5 [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 Ventre766995d2016-10-05 22:15:56 -070019import com.google.common.collect.ImmutableMap;
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080020import com.google.common.collect.SetMultimap;
Ray Milkeydb74ec72016-03-01 10:35:24 -080021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
Pier Ventre766995d2016-10-05 22:15:56 -070026import org.onlab.util.Identifier;
Ray Milkeydb74ec72016-03-01 10:35:24 -080027import org.onosproject.core.ApplicationId;
28import org.onosproject.core.CoreService;
Pier Ventre766995d2016-10-05 22:15:56 -070029import org.onosproject.net.ConnectPoint;
Ray Milkeydb74ec72016-03-01 10:35:24 -080030import org.onosproject.net.DeviceId;
Ray Milkeydb74ec72016-03-01 10:35:24 -080031import org.onosproject.net.PortNumber;
Ray Milkeydb74ec72016-03-01 10:35:24 -080032import org.onosproject.net.flowobjective.DefaultForwardingObjective;
Michele Santuarid2c8b152016-03-30 17:57:56 -070033import org.onosproject.net.flowobjective.DefaultNextObjective;
34import org.onosproject.net.flowobjective.FlowObjectiveService;
Ray Milkeydb74ec72016-03-01 10:35:24 -080035import org.onosproject.net.flowobjective.ForwardingObjective;
Michele Santuarid2c8b152016-03-30 17:57:56 -070036import org.onosproject.net.flowobjective.NextObjective;
Ray Milkeydb74ec72016-03-01 10:35:24 -080037import 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;
Pier Ventre766995d2016-10-05 22:15:56 -070042import org.onosproject.net.intent.constraint.EncapsulationConstraint;
43import org.onosproject.net.resource.ResourceService;
44import org.onosproject.net.resource.impl.LabelAllocator;
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;
Pier Ventre766995d2016-10-05 22:15:56 -070049import java.util.Map;
50import java.util.Optional;
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080051import java.util.Set;
Ray Milkeydb74ec72016-03-01 10:35:24 -080052
53/**
54 * Compiler to produce flow objectives from link collections.
55 */
56@Component(immediate = true)
Pier Ventred48320e2016-08-17 16:25:47 -070057public class LinkCollectionIntentFlowObjectiveCompiler
58 extends LinkCollectionCompiler<Objective>
59 implements IntentCompiler<LinkCollectionIntent> {
Ray Milkeydb74ec72016-03-01 10:35:24 -080060
61 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080062 protected IntentConfigurableRegistrator registrator;
Ray Milkeydb74ec72016-03-01 10:35:24 -080063
64 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 protected CoreService coreService;
66
Michele Santuarid2c8b152016-03-30 17:57:56 -070067 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
68 protected FlowObjectiveService flowObjectiveService;
69
Pier Ventre766995d2016-10-05 22:15:56 -070070 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
71 protected ResourceService resourceService;
72
Ray Milkeydb74ec72016-03-01 10:35:24 -080073 private ApplicationId appId;
74
75 @Activate
76 public void activate() {
77 appId = coreService.registerApplication("org.onosproject.net.intent");
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080078 registrator.registerCompiler(LinkCollectionIntent.class, this, true);
Pier Ventre766995d2016-10-05 22:15:56 -070079 if (labelAllocator == null) {
80 labelAllocator = new LabelAllocator(resourceService);
81 }
82
Ray Milkeydb74ec72016-03-01 10:35:24 -080083 }
84
85 @Deactivate
86 public void deactivate() {
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080087 registrator.unregisterCompiler(LinkCollectionIntent.class, true);
Ray Milkeydb74ec72016-03-01 10:35:24 -080088 }
89
90 @Override
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080091 public List<Intent> compile(LinkCollectionIntent intent, List<Intent> installable) {
Pier Ventred48320e2016-08-17 16:25:47 -070092
Ray Milkeydb74ec72016-03-01 10:35:24 -080093 SetMultimap<DeviceId, PortNumber> inputPorts = HashMultimap.create();
94 SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create();
Pier Ventre766995d2016-10-05 22:15:56 -070095 Map<ConnectPoint, Identifier<?>> labels = ImmutableMap.of();
96
97 Optional<EncapsulationConstraint> encapConstraint = this.getIntentEncapConstraint(intent);
Ray Milkeydb74ec72016-03-01 10:35:24 -080098
Pier Ventred48320e2016-08-17 16:25:47 -070099 computePorts(intent, inputPorts, outputPorts);
Ray Milkeydb74ec72016-03-01 10:35:24 -0800100
Pier Ventre766995d2016-10-05 22:15:56 -0700101 if (encapConstraint.isPresent()) {
102 labels = labelAllocator.assignLabelToPorts(intent.links(),
103 intent.id(),
104 encapConstraint.get().encapType());
105 }
106
Ray Milkeydb74ec72016-03-01 10:35:24 -0800107 List<Objective> objectives = new ArrayList<>();
108 List<DeviceId> devices = new ArrayList<>();
109 for (DeviceId deviceId: outputPorts.keys()) {
110 List<Objective> deviceObjectives =
111 createRules(intent,
112 deviceId,
113 inputPorts.get(deviceId),
Pier Ventre766995d2016-10-05 22:15:56 -0700114 outputPorts.get(deviceId),
115 labels);
Ray Milkeydb74ec72016-03-01 10:35:24 -0800116 deviceObjectives.forEach(objective -> {
117 objectives.add(objective);
118 devices.add(deviceId);
119 });
120 }
121 return Collections.singletonList(
Luca Prete670ac5d2017-02-03 15:55:43 -0800122 new FlowObjectiveIntent(appId, intent.key(), devices,
123 objectives,
124 intent.resources(),
125 intent.resourceGroup()));
Ray Milkeydb74ec72016-03-01 10:35:24 -0800126 }
127
Pier Ventred48320e2016-08-17 16:25:47 -0700128 @Override
Pier Ventre766995d2016-10-05 22:15:56 -0700129 protected List<Objective> createRules(LinkCollectionIntent intent,
130 DeviceId deviceId,
131 Set<PortNumber> inPorts,
132 Set<PortNumber> outPorts,
133 Map<ConnectPoint, Identifier<?>> labels) {
Ray Milkeydb74ec72016-03-01 10:35:24 -0800134
135 List<Objective> objectives = new ArrayList<>(inPorts.size());
Pier Ventre766995d2016-10-05 22:15:56 -0700136
137 /*
138 * Looking for the encapsulation constraint
139 */
140 Optional<EncapsulationConstraint> encapConstraint = this.getIntentEncapConstraint(intent);
Pier Ventred48320e2016-08-17 16:25:47 -0700141
142 inPorts.forEach(inport -> {
Pier Ventre766995d2016-10-05 22:15:56 -0700143
144 ForwardingInstructions instructions = this.createForwardingInstruction(
145 encapConstraint,
146 intent,
147 inport,
148 outPorts,
149 deviceId,
150 labels
151 );
Ray Milkeydb74ec72016-03-01 10:35:24 -0800152
Michele Santuarid2c8b152016-03-30 17:57:56 -0700153 NextObjective nextObjective = DefaultNextObjective.builder()
154 .withId(flowObjectiveService.allocateNextId())
Pier Ventred48320e2016-08-17 16:25:47 -0700155 .addTreatment(instructions.treatment())
Michele Santuarid2c8b152016-03-30 17:57:56 -0700156 .withType(NextObjective.Type.SIMPLE)
157 .fromApp(appId)
158 .makePermanent().add();
159 objectives.add(nextObjective);
160
161 objectives.add(DefaultForwardingObjective.builder()
Pier Ventred48320e2016-08-17 16:25:47 -0700162 .withSelector(instructions.selector())
Michele Santuarid2c8b152016-03-30 17:57:56 -0700163 .nextStep(nextObjective.id())
Ray Milkeydb74ec72016-03-01 10:35:24 -0800164 .withPriority(intent.priority())
165 .fromApp(appId)
166 .makePermanent()
Thomas Vachuska8378ccf2016-03-02 18:02:17 -0800167 .withFlag(ForwardingObjective.Flag.SPECIFIC)
Michele Santuarid2c8b152016-03-30 17:57:56 -0700168 .add());
Pier Ventred48320e2016-08-17 16:25:47 -0700169 }
170 );
Ray Milkeydb74ec72016-03-01 10:35:24 -0800171
172 return objectives;
173 }
Pier Ventred48320e2016-08-17 16:25:47 -0700174
Ray Milkeydb74ec72016-03-01 10:35:24 -0800175}