blob: cdde0511f39029dcd645f447ef931d70441f9031 [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
18import java.util.LinkedList;
19import java.util.List;
Ray Milkey661c38c2016-02-26 17:12:17 -080020
21import 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;
26import org.onosproject.core.ApplicationId;
27import org.onosproject.core.CoreService;
28import org.onosproject.net.ConnectPoint;
29import org.onosproject.net.DeviceId;
30import org.onosproject.net.flow.DefaultTrafficSelector;
31import org.onosproject.net.flow.DefaultTrafficTreatment;
32import org.onosproject.net.flow.TrafficSelector;
33import org.onosproject.net.flow.TrafficTreatment;
34import org.onosproject.net.flowobjective.DefaultForwardingObjective;
35import org.onosproject.net.flowobjective.ForwardingObjective;
36import org.onosproject.net.flowobjective.Objective;
37import org.onosproject.net.intent.FlowObjectiveIntent;
38import org.onosproject.net.intent.Intent;
39import org.onosproject.net.intent.IntentCompiler;
Ray Milkey661c38c2016-02-26 17:12:17 -080040import org.onosproject.net.intent.PathIntent;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080041import org.onosproject.net.resource.ResourceService;
Ray Milkey661c38c2016-02-26 17:12:17 -080042import org.slf4j.Logger;
43
44import com.google.common.collect.ImmutableList;
45
46import static org.slf4j.LoggerFactory.getLogger;
47
48@Component(immediate = true)
49public class PathIntentFlowObjectiveCompiler
50 extends PathCompiler<Objective>
51 implements IntentCompiler<PathIntent>,
52 PathCompiler.PathCompilerCreateFlow<Objective> {
53
54 private final Logger log = getLogger(getClass());
55
56 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
57 protected CoreService coreService;
58
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080060 protected IntentConfigurableRegistrator registrator;
Ray Milkey661c38c2016-02-26 17:12:17 -080061
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected ResourceService resourceService;
64
65 private ApplicationId appId;
66
67 @Activate
68 public void activate() {
69 appId = coreService.registerApplication("org.onosproject.net.intent");
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080070 registrator.registerCompiler(PathIntent.class, this, true);
Ray Milkey661c38c2016-02-26 17:12:17 -080071 }
72
73 @Deactivate
74 public void deactivate() {
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080075 registrator.unregisterCompiler(PathIntent.class, true);
Ray Milkey661c38c2016-02-26 17:12:17 -080076 }
77
78 @Override
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080079 public List<Intent> compile(PathIntent intent, List<Intent> installable) {
Ray Milkey661c38c2016-02-26 17:12:17 -080080
81 List<Objective> objectives = new LinkedList<>();
82 List<DeviceId> devices = new LinkedList<>();
83 compile(this, intent, objectives, devices);
84
85 return ImmutableList.of(new FlowObjectiveIntent(appId, devices, objectives, intent.resources()));
86 }
87
88 @Override
89 public Logger log() {
90 return log;
91 }
92
93 @Override
94 public ResourceService resourceService() {
95 return resourceService;
96 }
97
98 @Override
99 public void createFlow(TrafficSelector originalSelector, TrafficTreatment originalTreatment,
100 ConnectPoint ingress, ConnectPoint egress,
101 int priority, boolean applyTreatment,
102 List<Objective> objectives,
103 List<DeviceId> devices) {
104 TrafficSelector selector = DefaultTrafficSelector.builder(originalSelector)
105 .matchInPort(ingress.port())
106 .build();
107
108 TrafficTreatment.Builder treatmentBuilder;
109 if (applyTreatment) {
110 treatmentBuilder = DefaultTrafficTreatment.builder(originalTreatment);
111 } else {
112 treatmentBuilder = DefaultTrafficTreatment.builder();
113 }
114 TrafficTreatment treatment = treatmentBuilder.setOutput(egress.port()).build();
115
116 objectives.add(DefaultForwardingObjective.builder()
117 .withSelector(selector)
118 .withTreatment(treatment)
119 .withPriority(priority)
120 .fromApp(appId)
121 .makePermanent()
Thomas Vachuska8378ccf2016-03-02 18:02:17 -0800122 .withFlag(ForwardingObjective.Flag.SPECIFIC)
Ray Milkey661c38c2016-02-26 17:12:17 -0800123 .add());
124 devices.add(ingress.deviceId());
125 }
126}