blob: ac1c051c84811cd01e56baaa088c7eff75ec4ecf [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;
Ray Milkey661c38c2016-02-26 17:12:17 -080043import org.slf4j.Logger;
44
Michele Santuarid2c8b152016-03-30 17:57:56 -070045import java.util.LinkedList;
46import java.util.List;
Ray Milkey661c38c2016-02-26 17:12:17 -080047
48import static org.slf4j.LoggerFactory.getLogger;
49
50@Component(immediate = true)
51public class PathIntentFlowObjectiveCompiler
52 extends PathCompiler<Objective>
53 implements IntentCompiler<PathIntent>,
54 PathCompiler.PathCompilerCreateFlow<Objective> {
55
56 private final Logger log = getLogger(getClass());
57
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
59 protected CoreService coreService;
60
61 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080062 protected IntentConfigurableRegistrator registrator;
Ray Milkey661c38c2016-02-26 17:12:17 -080063
64 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 protected ResourceService resourceService;
66
Michele Santuarid2c8b152016-03-30 17:57:56 -070067 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
68 protected FlowObjectiveService flowObjectiveService;
69
Ray Milkey661c38c2016-02-26 17:12:17 -080070 private ApplicationId appId;
71
72 @Activate
73 public void activate() {
74 appId = coreService.registerApplication("org.onosproject.net.intent");
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080075 registrator.registerCompiler(PathIntent.class, this, true);
Ray Milkey661c38c2016-02-26 17:12:17 -080076 }
77
78 @Deactivate
79 public void deactivate() {
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080080 registrator.unregisterCompiler(PathIntent.class, true);
Ray Milkey661c38c2016-02-26 17:12:17 -080081 }
82
83 @Override
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080084 public List<Intent> compile(PathIntent intent, List<Intent> installable) {
Ray Milkey661c38c2016-02-26 17:12:17 -080085
86 List<Objective> objectives = new LinkedList<>();
87 List<DeviceId> devices = new LinkedList<>();
88 compile(this, intent, objectives, devices);
89
90 return ImmutableList.of(new FlowObjectiveIntent(appId, devices, objectives, intent.resources()));
91 }
92
93 @Override
94 public Logger log() {
95 return log;
96 }
97
98 @Override
99 public ResourceService resourceService() {
100 return resourceService;
101 }
102
103 @Override
104 public void createFlow(TrafficSelector originalSelector, TrafficTreatment originalTreatment,
105 ConnectPoint ingress, ConnectPoint egress,
106 int priority, boolean applyTreatment,
107 List<Objective> objectives,
108 List<DeviceId> devices) {
109 TrafficSelector selector = DefaultTrafficSelector.builder(originalSelector)
110 .matchInPort(ingress.port())
111 .build();
112
113 TrafficTreatment.Builder treatmentBuilder;
114 if (applyTreatment) {
115 treatmentBuilder = DefaultTrafficTreatment.builder(originalTreatment);
116 } else {
117 treatmentBuilder = DefaultTrafficTreatment.builder();
118 }
Michele Santuarid2c8b152016-03-30 17:57:56 -0700119
Ray Milkey661c38c2016-02-26 17:12:17 -0800120 TrafficTreatment treatment = treatmentBuilder.setOutput(egress.port()).build();
121
Michele Santuarid2c8b152016-03-30 17:57:56 -0700122 NextObjective nextObjective = DefaultNextObjective.builder()
123 .withId(flowObjectiveService.allocateNextId())
124 .addTreatment(treatment)
125 .withType(NextObjective.Type.SIMPLE)
126 .fromApp(appId)
127 .makePermanent().add();
128 objectives.add(nextObjective);
129 devices.add(ingress.deviceId());
130
Ray Milkey661c38c2016-02-26 17:12:17 -0800131 objectives.add(DefaultForwardingObjective.builder()
Michele Santuarid2c8b152016-03-30 17:57:56 -0700132 .withSelector(selector)
133 .nextStep(nextObjective.id())
134 .withPriority(priority)
135 .fromApp(appId)
136 .makePermanent()
137 .withFlag(ForwardingObjective.Flag.SPECIFIC)
138 .add());
Ray Milkey661c38c2016-02-26 17:12:17 -0800139 devices.add(ingress.deviceId());
140 }
141}