blob: 75734b9971936232b266f2021dd8b8e940f14a7e [file] [log] [blame]
Ray Milkey661c38c2016-02-26 17:12:17 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Ray Milkey661c38c2016-02-26 17:12:17 -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
Michele Santuarid2c8b152016-03-30 17:57:56 -070018import com.google.common.collect.ImmutableList;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import org.osgi.service.component.annotations.Activate;
20import org.osgi.service.component.annotations.Component;
21import org.osgi.service.component.annotations.Deactivate;
22import org.osgi.service.component.annotations.Reference;
23import org.osgi.service.component.annotations.ReferenceCardinality;
Ray Milkey661c38c2016-02-26 17:12:17 -080024import org.onosproject.core.ApplicationId;
25import org.onosproject.core.CoreService;
26import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.flow.DefaultTrafficSelector;
29import org.onosproject.net.flow.DefaultTrafficTreatment;
30import org.onosproject.net.flow.TrafficSelector;
31import org.onosproject.net.flow.TrafficTreatment;
32import 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 Milkey661c38c2016-02-26 17:12:17 -080035import org.onosproject.net.flowobjective.ForwardingObjective;
Michele Santuarid2c8b152016-03-30 17:57:56 -070036import org.onosproject.net.flowobjective.NextObjective;
Ray Milkey661c38c2016-02-26 17:12:17 -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 Milkey661c38c2016-02-26 17:12:17 -080041import org.onosproject.net.intent.PathIntent;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080042import org.onosproject.net.resource.ResourceService;
Pier Ventref8543d82016-09-28 19:49:33 -070043import org.onosproject.net.resource.impl.LabelAllocator;
Ray Milkey661c38c2016-02-26 17:12:17 -080044import org.slf4j.Logger;
45
Michele Santuarid2c8b152016-03-30 17:57:56 -070046import java.util.LinkedList;
47import java.util.List;
Ray Milkey661c38c2016-02-26 17:12:17 -080048
49import static org.slf4j.LoggerFactory.getLogger;
50
51@Component(immediate = true)
52public class PathIntentFlowObjectiveCompiler
53 extends PathCompiler<Objective>
54 implements IntentCompiler<PathIntent>,
55 PathCompiler.PathCompilerCreateFlow<Objective> {
56
57 private final Logger log = getLogger(getClass());
58
Ray Milkeyd84f89b2018-08-17 14:54:17 -070059 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Ray Milkey661c38c2016-02-26 17:12:17 -080060 protected CoreService coreService;
61
Ray Milkeyd84f89b2018-08-17 14:54:17 -070062 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080063 protected IntentConfigurableRegistrator registrator;
Ray Milkey661c38c2016-02-26 17:12:17 -080064
Ray Milkeyd84f89b2018-08-17 14:54:17 -070065 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Ray Milkey661c38c2016-02-26 17:12:17 -080066 protected ResourceService resourceService;
67
Ray Milkeyd84f89b2018-08-17 14:54:17 -070068 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Michele Santuarid2c8b152016-03-30 17:57:56 -070069 protected FlowObjectiveService flowObjectiveService;
70
Ray Milkey661c38c2016-02-26 17:12:17 -080071 private ApplicationId appId;
72
73 @Activate
74 public void activate() {
75 appId = coreService.registerApplication("org.onosproject.net.intent");
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080076 registrator.registerCompiler(PathIntent.class, this, true);
Pier Ventref8543d82016-09-28 19:49:33 -070077 labelAllocator = new LabelAllocator(resourceService);
Ray Milkey661c38c2016-02-26 17:12:17 -080078 }
79
80 @Deactivate
81 public void deactivate() {
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080082 registrator.unregisterCompiler(PathIntent.class, true);
Ray Milkey661c38c2016-02-26 17:12:17 -080083 }
84
85 @Override
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080086 public List<Intent> compile(PathIntent intent, List<Intent> installable) {
Ray Milkey661c38c2016-02-26 17:12:17 -080087
88 List<Objective> objectives = new LinkedList<>();
89 List<DeviceId> devices = new LinkedList<>();
90 compile(this, intent, objectives, devices);
91
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -070092 return ImmutableList.of(new FlowObjectiveIntent(appId,
93 intent.key(),
94 devices,
95 objectives,
Luca Prete670ac5d2017-02-03 15:55:43 -080096 intent.resources(),
97 intent.resourceGroup()
98 ));
Ray Milkey661c38c2016-02-26 17:12:17 -080099 }
100
101 @Override
102 public Logger log() {
103 return log;
104 }
105
106 @Override
107 public ResourceService resourceService() {
108 return resourceService;
109 }
110
111 @Override
112 public void createFlow(TrafficSelector originalSelector, TrafficTreatment originalTreatment,
113 ConnectPoint ingress, ConnectPoint egress,
114 int priority, boolean applyTreatment,
115 List<Objective> objectives,
116 List<DeviceId> devices) {
117 TrafficSelector selector = DefaultTrafficSelector.builder(originalSelector)
118 .matchInPort(ingress.port())
119 .build();
120
121 TrafficTreatment.Builder treatmentBuilder;
122 if (applyTreatment) {
123 treatmentBuilder = DefaultTrafficTreatment.builder(originalTreatment);
124 } else {
125 treatmentBuilder = DefaultTrafficTreatment.builder();
126 }
Michele Santuarid2c8b152016-03-30 17:57:56 -0700127
Ray Milkey661c38c2016-02-26 17:12:17 -0800128 TrafficTreatment treatment = treatmentBuilder.setOutput(egress.port()).build();
129
Michele Santuarid2c8b152016-03-30 17:57:56 -0700130 NextObjective nextObjective = DefaultNextObjective.builder()
131 .withId(flowObjectiveService.allocateNextId())
132 .addTreatment(treatment)
133 .withType(NextObjective.Type.SIMPLE)
134 .fromApp(appId)
135 .makePermanent().add();
136 objectives.add(nextObjective);
137 devices.add(ingress.deviceId());
138
Ray Milkey661c38c2016-02-26 17:12:17 -0800139 objectives.add(DefaultForwardingObjective.builder()
Michele Santuarid2c8b152016-03-30 17:57:56 -0700140 .withSelector(selector)
141 .nextStep(nextObjective.id())
142 .withPriority(priority)
143 .fromApp(appId)
144 .makePermanent()
145 .withFlag(ForwardingObjective.Flag.SPECIFIC)
146 .add());
Ray Milkey661c38c2016-02-26 17:12:17 -0800147 devices.add(ingress.deviceId());
148 }
149}