blob: 25cae1876fd79d68e11d2dc22fc27828edb5df27 [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;
41import org.onosproject.net.intent.IntentExtensionService;
42import org.onosproject.net.intent.PathIntent;
43import org.onosproject.net.newresource.ResourceService;
44import org.onosproject.net.resource.link.LinkResourceAllocations;
45import org.slf4j.Logger;
46
47import com.google.common.collect.ImmutableList;
48
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)
63 protected IntentExtensionService intentManager;
64
65 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected ResourceService resourceService;
67
68 private ApplicationId appId;
69
70 @Activate
71 public void activate() {
72 appId = coreService.registerApplication("org.onosproject.net.intent");
73 //intentManager.registerCompiler(PathIntent.class, this);
74 }
75
76 @Deactivate
77 public void deactivate() {
78 //intentManager.unregisterCompiler(PathIntent.class);
79 }
80
81 @Override
82 public List<Intent> compile(PathIntent intent, List<Intent> installable,
83 Set<LinkResourceAllocations> resources) {
84
85 List<Objective> objectives = new LinkedList<>();
86 List<DeviceId> devices = new LinkedList<>();
87 compile(this, intent, objectives, devices);
88
89 return ImmutableList.of(new FlowObjectiveIntent(appId, devices, objectives, intent.resources()));
90 }
91
92 @Override
93 public Logger log() {
94 return log;
95 }
96
97 @Override
98 public ResourceService resourceService() {
99 return resourceService;
100 }
101
102 @Override
103 public void createFlow(TrafficSelector originalSelector, TrafficTreatment originalTreatment,
104 ConnectPoint ingress, ConnectPoint egress,
105 int priority, boolean applyTreatment,
106 List<Objective> objectives,
107 List<DeviceId> devices) {
108 TrafficSelector selector = DefaultTrafficSelector.builder(originalSelector)
109 .matchInPort(ingress.port())
110 .build();
111
112 TrafficTreatment.Builder treatmentBuilder;
113 if (applyTreatment) {
114 treatmentBuilder = DefaultTrafficTreatment.builder(originalTreatment);
115 } else {
116 treatmentBuilder = DefaultTrafficTreatment.builder();
117 }
118 TrafficTreatment treatment = treatmentBuilder.setOutput(egress.port()).build();
119
120 objectives.add(DefaultForwardingObjective.builder()
121 .withSelector(selector)
122 .withTreatment(treatment)
123 .withPriority(priority)
124 .fromApp(appId)
125 .makePermanent()
126 .withFlag(ForwardingObjective.Flag.SPECIFIC)
127 .add());
128 devices.add(ingress.deviceId());
129 }
130}