blob: 8ea04333e6ca7714d6ee073279a39443a6404534 [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.
Yi Tsenga64f0c82017-02-03 11:17:15 -080055 * @deprecated 1.10 Kingfisher
Ray Milkeydb74ec72016-03-01 10:35:24 -080056 */
Yi Tsenga64f0c82017-02-03 11:17:15 -080057@Deprecated
Ray Milkeydb74ec72016-03-01 10:35:24 -080058@Component(immediate = true)
Pier Ventred48320e2016-08-17 16:25:47 -070059public class LinkCollectionIntentFlowObjectiveCompiler
60 extends LinkCollectionCompiler<Objective>
61 implements IntentCompiler<LinkCollectionIntent> {
Ray Milkeydb74ec72016-03-01 10:35:24 -080062
63 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080064 protected IntentConfigurableRegistrator registrator;
Ray Milkeydb74ec72016-03-01 10:35:24 -080065
66 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected CoreService coreService;
68
Michele Santuarid2c8b152016-03-30 17:57:56 -070069 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 protected FlowObjectiveService flowObjectiveService;
71
Pier Ventre766995d2016-10-05 22:15:56 -070072 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
73 protected ResourceService resourceService;
74
Ray Milkeydb74ec72016-03-01 10:35:24 -080075 private ApplicationId appId;
76
77 @Activate
78 public void activate() {
79 appId = coreService.registerApplication("org.onosproject.net.intent");
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080080 registrator.registerCompiler(LinkCollectionIntent.class, this, true);
Pier Ventre766995d2016-10-05 22:15:56 -070081 if (labelAllocator == null) {
82 labelAllocator = new LabelAllocator(resourceService);
83 }
84
Ray Milkeydb74ec72016-03-01 10:35:24 -080085 }
86
87 @Deactivate
88 public void deactivate() {
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080089 registrator.unregisterCompiler(LinkCollectionIntent.class, true);
Ray Milkeydb74ec72016-03-01 10:35:24 -080090 }
91
92 @Override
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080093 public List<Intent> compile(LinkCollectionIntent intent, List<Intent> installable) {
Pier Ventred48320e2016-08-17 16:25:47 -070094
Ray Milkeydb74ec72016-03-01 10:35:24 -080095 SetMultimap<DeviceId, PortNumber> inputPorts = HashMultimap.create();
96 SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create();
Pier Ventre766995d2016-10-05 22:15:56 -070097 Map<ConnectPoint, Identifier<?>> labels = ImmutableMap.of();
98
99 Optional<EncapsulationConstraint> encapConstraint = this.getIntentEncapConstraint(intent);
Ray Milkeydb74ec72016-03-01 10:35:24 -0800100
Pier Ventred48320e2016-08-17 16:25:47 -0700101 computePorts(intent, inputPorts, outputPorts);
Ray Milkeydb74ec72016-03-01 10:35:24 -0800102
Pier Ventre766995d2016-10-05 22:15:56 -0700103 if (encapConstraint.isPresent()) {
104 labels = labelAllocator.assignLabelToPorts(intent.links(),
105 intent.id(),
106 encapConstraint.get().encapType());
107 }
108
Ray Milkeydb74ec72016-03-01 10:35:24 -0800109 List<Objective> objectives = new ArrayList<>();
110 List<DeviceId> devices = new ArrayList<>();
111 for (DeviceId deviceId: outputPorts.keys()) {
112 List<Objective> deviceObjectives =
113 createRules(intent,
114 deviceId,
115 inputPorts.get(deviceId),
Pier Ventre766995d2016-10-05 22:15:56 -0700116 outputPorts.get(deviceId),
117 labels);
Ray Milkeydb74ec72016-03-01 10:35:24 -0800118 deviceObjectives.forEach(objective -> {
119 objectives.add(objective);
120 devices.add(deviceId);
121 });
122 }
123 return Collections.singletonList(
Luca Prete670ac5d2017-02-03 15:55:43 -0800124 new FlowObjectiveIntent(appId, intent.key(), devices,
125 objectives,
126 intent.resources(),
127 intent.resourceGroup()));
Ray Milkeydb74ec72016-03-01 10:35:24 -0800128 }
129
Pier Ventred48320e2016-08-17 16:25:47 -0700130 @Override
Yi Tseng84c5a3d2017-04-14 16:42:59 -0700131 boolean optimizeTreatments() {
132 return false;
133 }
134
135 @Override
Pier Ventre766995d2016-10-05 22:15:56 -0700136 protected List<Objective> createRules(LinkCollectionIntent intent,
137 DeviceId deviceId,
138 Set<PortNumber> inPorts,
139 Set<PortNumber> outPorts,
140 Map<ConnectPoint, Identifier<?>> labels) {
Ray Milkeydb74ec72016-03-01 10:35:24 -0800141
142 List<Objective> objectives = new ArrayList<>(inPorts.size());
Pier Ventre766995d2016-10-05 22:15:56 -0700143
144 /*
145 * Looking for the encapsulation constraint
146 */
147 Optional<EncapsulationConstraint> encapConstraint = this.getIntentEncapConstraint(intent);
Pier Ventred48320e2016-08-17 16:25:47 -0700148
149 inPorts.forEach(inport -> {
Pier Ventre766995d2016-10-05 22:15:56 -0700150
151 ForwardingInstructions instructions = this.createForwardingInstruction(
152 encapConstraint,
153 intent,
154 inport,
155 outPorts,
156 deviceId,
157 labels
158 );
Ray Milkeydb74ec72016-03-01 10:35:24 -0800159
Michele Santuarid2c8b152016-03-30 17:57:56 -0700160 NextObjective nextObjective = DefaultNextObjective.builder()
161 .withId(flowObjectiveService.allocateNextId())
Pier Ventred48320e2016-08-17 16:25:47 -0700162 .addTreatment(instructions.treatment())
Michele Santuarid2c8b152016-03-30 17:57:56 -0700163 .withType(NextObjective.Type.SIMPLE)
164 .fromApp(appId)
165 .makePermanent().add();
166 objectives.add(nextObjective);
167
168 objectives.add(DefaultForwardingObjective.builder()
Pier Ventred48320e2016-08-17 16:25:47 -0700169 .withSelector(instructions.selector())
Michele Santuarid2c8b152016-03-30 17:57:56 -0700170 .nextStep(nextObjective.id())
Ray Milkeydb74ec72016-03-01 10:35:24 -0800171 .withPriority(intent.priority())
172 .fromApp(appId)
173 .makePermanent()
Thomas Vachuska8378ccf2016-03-02 18:02:17 -0800174 .withFlag(ForwardingObjective.Flag.SPECIFIC)
Michele Santuarid2c8b152016-03-30 17:57:56 -0700175 .add());
Pier Ventred48320e2016-08-17 16:25:47 -0700176 }
177 );
Ray Milkeydb74ec72016-03-01 10:35:24 -0800178
179 return objectives;
180 }
Pier Ventred48320e2016-08-17 16:25:47 -0700181
Ray Milkeydb74ec72016-03-01 10:35:24 -0800182}