blob: 039a1f5197192a9ffaad13efed3ca1cd0513abc7 [file] [log] [blame]
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08001/*
2 * Copyright 2015 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
Ray Milkey661c38c2016-02-26 17:12:17 -080018import java.util.LinkedList;
19import java.util.List;
20import java.util.Set;
21
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080022import 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;
Ray Milkey661c38c2016-02-26 17:12:17 -080030import org.onosproject.net.DeviceId;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080031import org.onosproject.net.flow.DefaultFlowRule;
32import org.onosproject.net.flow.DefaultTrafficSelector;
33import org.onosproject.net.flow.DefaultTrafficTreatment;
34import org.onosproject.net.flow.FlowRule;
35import org.onosproject.net.flow.TrafficSelector;
36import org.onosproject.net.flow.TrafficTreatment;
37import org.onosproject.net.intent.FlowRuleIntent;
38import org.onosproject.net.intent.Intent;
39import org.onosproject.net.intent.IntentCompiler;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080040import org.onosproject.net.intent.PathIntent;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010041import org.onosproject.net.newresource.ResourceService;
Brian O'Connor6de2e202015-05-21 14:30:41 -070042import org.onosproject.net.resource.link.LinkResourceAllocations;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010043import org.slf4j.Logger;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080044
Ray Milkey661c38c2016-02-26 17:12:17 -080045import com.google.common.collect.ImmutableList;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010046
Michele Santuari69fc2ff2015-12-03 17:05:35 +010047import static org.slf4j.LoggerFactory.getLogger;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080048
49@Component(immediate = true)
Ray Milkey661c38c2016-02-26 17:12:17 -080050public class PathIntentCompiler
51 extends PathCompiler<FlowRule>
52 implements IntentCompiler<PathIntent>,
53 PathCompiler.PathCompilerCreateFlow<FlowRule> {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080054
Michele Santuari69fc2ff2015-12-03 17:05:35 +010055 private final Logger log = getLogger(getClass());
56
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080057 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58 protected CoreService coreService;
59
60 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080061 protected IntentConfigurableRegistrator registrator;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080062
Michele Santuari69fc2ff2015-12-03 17:05:35 +010063 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 protected ResourceService resourceService;
65
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080066 private ApplicationId appId;
67
68 @Activate
69 public void activate() {
70 appId = coreService.registerApplication("org.onosproject.net.intent");
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080071 registrator.registerCompiler(PathIntent.class, this, false);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080072 }
73
74 @Deactivate
75 public void deactivate() {
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080076 registrator.unregisterCompiler(PathIntent.class, false);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080077 }
78
79 @Override
80 public List<Intent> compile(PathIntent intent, List<Intent> installable,
81 Set<LinkResourceAllocations> resources) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080082
Ray Milkey661c38c2016-02-26 17:12:17 -080083 List<FlowRule> rules = new LinkedList<>();
84 List<DeviceId> devices = new LinkedList<>();
85 compile(this, intent, rules, devices);
Sho SHIMIZUe8f656c2016-02-24 14:05:17 -080086
87 return ImmutableList.of(new FlowRuleIntent(appId, null, rules, intent.resources()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080088 }
89
Ray Milkey661c38c2016-02-26 17:12:17 -080090 @Override
91 public Logger log() {
92 return log;
93 }
94
95 @Override
96 public ResourceService resourceService() {
97 return resourceService;
98 }
99
100 @Override
101 public void createFlow(TrafficSelector originalSelector, TrafficTreatment originalTreatment,
102 ConnectPoint ingress, ConnectPoint egress,
103 int priority, boolean applyTreatment,
104 List<FlowRule> rules,
105 List<DeviceId> devices) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800106 TrafficSelector selector = DefaultTrafficSelector.builder(originalSelector)
107 .matchInPort(ingress.port())
108 .build();
109
110 TrafficTreatment.Builder treatmentBuilder;
Michele Santuari69fc2ff2015-12-03 17:05:35 +0100111 if (applyTreatment) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800112 treatmentBuilder = DefaultTrafficTreatment.builder(originalTreatment);
113 } else {
114 treatmentBuilder = DefaultTrafficTreatment.builder();
115 }
116 TrafficTreatment treatment = treatmentBuilder.setOutput(egress.port()).build();
117
Ray Milkey661c38c2016-02-26 17:12:17 -0800118 rules.add(DefaultFlowRule.builder()
Ray Milkeyd13a37b2015-06-12 11:55:17 -0700119 .forDevice(ingress.deviceId())
120 .withSelector(selector)
121 .withTreatment(treatment)
Brian O'Connor81134662015-06-25 17:23:33 -0400122 .withPriority(priority)
Ray Milkeyd13a37b2015-06-12 11:55:17 -0700123 .fromApp(appId)
124 .makePermanent()
Ray Milkey661c38c2016-02-26 17:12:17 -0800125 .build());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800126 }
127}