blob: ddc7f211e9737d66cd55a9354da141ef9baeeb70 [file] [log] [blame]
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Sho SHIMIZUee2aa652015-02-25 18:56:43 -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 Santuari6096acd2016-02-09 17:00:37 +010018import com.google.common.collect.ImmutableList;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -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;
Ray Milkey661c38c2016-02-26 17:12:17 -080027import org.onosproject.net.DeviceId;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080028import org.onosproject.net.flow.DefaultFlowRule;
29import org.onosproject.net.flow.DefaultTrafficSelector;
30import org.onosproject.net.flow.DefaultTrafficTreatment;
31import org.onosproject.net.flow.FlowRule;
32import org.onosproject.net.flow.TrafficSelector;
33import org.onosproject.net.flow.TrafficTreatment;
34import org.onosproject.net.intent.FlowRuleIntent;
35import org.onosproject.net.intent.Intent;
36import org.onosproject.net.intent.IntentCompiler;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080037import org.onosproject.net.intent.PathIntent;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080038import org.onosproject.net.resource.ResourceService;
Pier Ventref8543d82016-09-28 19:49:33 -070039import org.onosproject.net.resource.impl.LabelAllocator;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010040import org.slf4j.Logger;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080041
Michele Santuari6096acd2016-02-09 17:00:37 +010042import java.util.LinkedList;
43import java.util.List;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010044
Michele Santuari69fc2ff2015-12-03 17:05:35 +010045import static org.slf4j.LoggerFactory.getLogger;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080046
Michele Santuari6096acd2016-02-09 17:00:37 +010047
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080048@Component(immediate = true)
Ray Milkey661c38c2016-02-26 17:12:17 -080049public class PathIntentCompiler
50 extends PathCompiler<FlowRule>
51 implements IntentCompiler<PathIntent>,
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080052 PathCompiler.PathCompilerCreateFlow<FlowRule> {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080053
Michele Santuari69fc2ff2015-12-03 17:05:35 +010054 private final Logger log = getLogger(getClass());
55
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080056 @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;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080061
Michele Santuari69fc2ff2015-12-03 17:05:35 +010062 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected ResourceService resourceService;
64
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080065 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, false);
Pier Ventref8543d82016-09-28 19:49:33 -070071 labelAllocator = new LabelAllocator(resourceService);
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
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080080 public List<Intent> compile(PathIntent intent, List<Intent> installable) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080081
Ray Milkey661c38c2016-02-26 17:12:17 -080082 List<FlowRule> rules = new LinkedList<>();
83 List<DeviceId> devices = new LinkedList<>();
84 compile(this, intent, rules, devices);
Sho SHIMIZUe8f656c2016-02-24 14:05:17 -080085
Michele Santuari6096acd2016-02-09 17:00:37 +010086
helenyrwu2a674902016-07-20 09:48:04 -070087 return ImmutableList.of(new FlowRuleIntent(appId, null, rules, intent.resources(), intent.type()));
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
Michele Santuari6096acd2016-02-09 17:00:37 +0100100
Ray Milkey661c38c2016-02-26 17:12:17 -0800101 @Override
102 public void createFlow(TrafficSelector originalSelector, TrafficTreatment originalTreatment,
103 ConnectPoint ingress, ConnectPoint egress,
104 int priority, boolean applyTreatment,
105 List<FlowRule> rules,
106 List<DeviceId> devices) {
Michele Santuari6096acd2016-02-09 17:00:37 +0100107
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800108 TrafficSelector selector = DefaultTrafficSelector.builder(originalSelector)
109 .matchInPort(ingress.port())
110 .build();
111
112 TrafficTreatment.Builder treatmentBuilder;
Michele Santuari69fc2ff2015-12-03 17:05:35 +0100113 if (applyTreatment) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800114 treatmentBuilder = DefaultTrafficTreatment.builder(originalTreatment);
115 } else {
116 treatmentBuilder = DefaultTrafficTreatment.builder();
117 }
118 TrafficTreatment treatment = treatmentBuilder.setOutput(egress.port()).build();
119
Ray Milkey661c38c2016-02-26 17:12:17 -0800120 rules.add(DefaultFlowRule.builder()
Ray Milkeyd13a37b2015-06-12 11:55:17 -0700121 .forDevice(ingress.deviceId())
122 .withSelector(selector)
123 .withTreatment(treatment)
Brian O'Connor81134662015-06-25 17:23:33 -0400124 .withPriority(priority)
Ray Milkeyd13a37b2015-06-12 11:55:17 -0700125 .fromApp(appId)
126 .makePermanent()
Ray Milkey661c38c2016-02-26 17:12:17 -0800127 .build());
Michele Santuari6096acd2016-02-09 17:00:37 +0100128
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800129 }
130}