blob: 521d6fc6e8baeef28d5ca8b4ad73370915be7e92 [file] [log] [blame]
Ray Milkey661c38c2016-02-26 17:12:17 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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 Milkey661c38c2016-02-26 17:12:17 -080019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import 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
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected CoreService coreService;
61
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080063 protected IntentConfigurableRegistrator registrator;
Ray Milkey661c38c2016-02-26 17:12:17 -080064
65 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected ResourceService resourceService;
67
Michele Santuarid2c8b152016-03-30 17:57:56 -070068 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
69 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,
96 intent.resources()));
Ray Milkey661c38c2016-02-26 17:12:17 -080097 }
98
99 @Override
100 public Logger log() {
101 return log;
102 }
103
104 @Override
105 public ResourceService resourceService() {
106 return resourceService;
107 }
108
109 @Override
110 public void createFlow(TrafficSelector originalSelector, TrafficTreatment originalTreatment,
111 ConnectPoint ingress, ConnectPoint egress,
112 int priority, boolean applyTreatment,
113 List<Objective> objectives,
114 List<DeviceId> devices) {
115 TrafficSelector selector = DefaultTrafficSelector.builder(originalSelector)
116 .matchInPort(ingress.port())
117 .build();
118
119 TrafficTreatment.Builder treatmentBuilder;
120 if (applyTreatment) {
121 treatmentBuilder = DefaultTrafficTreatment.builder(originalTreatment);
122 } else {
123 treatmentBuilder = DefaultTrafficTreatment.builder();
124 }
Michele Santuarid2c8b152016-03-30 17:57:56 -0700125
Ray Milkey661c38c2016-02-26 17:12:17 -0800126 TrafficTreatment treatment = treatmentBuilder.setOutput(egress.port()).build();
127
Michele Santuarid2c8b152016-03-30 17:57:56 -0700128 NextObjective nextObjective = DefaultNextObjective.builder()
129 .withId(flowObjectiveService.allocateNextId())
130 .addTreatment(treatment)
131 .withType(NextObjective.Type.SIMPLE)
132 .fromApp(appId)
133 .makePermanent().add();
134 objectives.add(nextObjective);
135 devices.add(ingress.deviceId());
136
Ray Milkey661c38c2016-02-26 17:12:17 -0800137 objectives.add(DefaultForwardingObjective.builder()
Michele Santuarid2c8b152016-03-30 17:57:56 -0700138 .withSelector(selector)
139 .nextStep(nextObjective.id())
140 .withPriority(priority)
141 .fromApp(appId)
142 .makePermanent()
143 .withFlag(ForwardingObjective.Flag.SPECIFIC)
144 .add());
Ray Milkey661c38c2016-02-26 17:12:17 -0800145 devices.add(ingress.deviceId());
146 }
147}