blob: d1ab1cf842eb56ccebc8b47bfe00031b5efb9ef5 [file] [log] [blame]
Ray Milkey661c38c2016-02-26 17:12:17 -08001/*
2 * Copyright 2016 Open Networking Laboratory
3 *
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;
20import java.util.Set;
21
22import org.apache.felix.scr.annotations.Activate;
23import org.apache.felix.scr.annotations.Component;
24import org.apache.felix.scr.annotations.Deactivate;
25import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
27import org.onosproject.core.ApplicationId;
28import org.onosproject.core.CoreService;
29import org.onosproject.net.ConnectPoint;
30import org.onosproject.net.DeviceId;
31import org.onosproject.net.flow.DefaultTrafficSelector;
32import org.onosproject.net.flow.DefaultTrafficTreatment;
33import org.onosproject.net.flow.TrafficSelector;
34import org.onosproject.net.flow.TrafficTreatment;
35import org.onosproject.net.flowobjective.DefaultForwardingObjective;
36import org.onosproject.net.flowobjective.ForwardingObjective;
37import 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;
42import org.onosproject.net.newresource.ResourceService;
43import org.onosproject.net.resource.link.LinkResourceAllocations;
44import org.slf4j.Logger;
45
46import com.google.common.collect.ImmutableList;
47
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
67 private ApplicationId appId;
68
69 @Activate
70 public void activate() {
71 appId = coreService.registerApplication("org.onosproject.net.intent");
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080072 registrator.registerCompiler(PathIntent.class, this, true);
Ray Milkey661c38c2016-02-26 17:12:17 -080073 }
74
75 @Deactivate
76 public void deactivate() {
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080077 registrator.unregisterCompiler(PathIntent.class, true);
Ray Milkey661c38c2016-02-26 17:12:17 -080078 }
79
80 @Override
81 public List<Intent> compile(PathIntent intent, List<Intent> installable,
82 Set<LinkResourceAllocations> resources) {
83
84 List<Objective> objectives = new LinkedList<>();
85 List<DeviceId> devices = new LinkedList<>();
86 compile(this, intent, objectives, devices);
87
88 return ImmutableList.of(new FlowObjectiveIntent(appId, devices, objectives, intent.resources()));
89 }
90
91 @Override
92 public Logger log() {
93 return log;
94 }
95
96 @Override
97 public ResourceService resourceService() {
98 return resourceService;
99 }
100
101 @Override
102 public void createFlow(TrafficSelector originalSelector, TrafficTreatment originalTreatment,
103 ConnectPoint ingress, ConnectPoint egress,
104 int priority, boolean applyTreatment,
105 List<Objective> objectives,
106 List<DeviceId> devices) {
107 TrafficSelector selector = DefaultTrafficSelector.builder(originalSelector)
108 .matchInPort(ingress.port())
109 .build();
110
111 TrafficTreatment.Builder treatmentBuilder;
112 if (applyTreatment) {
113 treatmentBuilder = DefaultTrafficTreatment.builder(originalTreatment);
114 } else {
115 treatmentBuilder = DefaultTrafficTreatment.builder();
116 }
117 TrafficTreatment treatment = treatmentBuilder.setOutput(egress.port()).build();
118
119 objectives.add(DefaultForwardingObjective.builder()
120 .withSelector(selector)
121 .withTreatment(treatment)
122 .withPriority(priority)
123 .fromApp(appId)
124 .makePermanent()
125 .withFlag(ForwardingObjective.Flag.SPECIFIC)
126 .add());
127 devices.add(ingress.deviceId());
128 }
129}