blob: fdcdd00cb85525d00e9ab6aa3ca4b1da70b60bd7 [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;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010039import org.slf4j.Logger;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080040
Michele Santuari6096acd2016-02-09 17:00:37 +010041import java.util.LinkedList;
42import java.util.List;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010043
Michele Santuari69fc2ff2015-12-03 17:05:35 +010044import static org.slf4j.LoggerFactory.getLogger;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080045
Michele Santuari6096acd2016-02-09 17:00:37 +010046
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080047@Component(immediate = true)
Ray Milkey661c38c2016-02-26 17:12:17 -080048public class PathIntentCompiler
49 extends PathCompiler<FlowRule>
50 implements IntentCompiler<PathIntent>,
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080051 PathCompiler.PathCompilerCreateFlow<FlowRule> {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080052
Michele Santuari69fc2ff2015-12-03 17:05:35 +010053 private final Logger log = getLogger(getClass());
54
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080055 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
56 protected CoreService coreService;
57
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080059 protected IntentConfigurableRegistrator registrator;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080060
Michele Santuari69fc2ff2015-12-03 17:05:35 +010061 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
62 protected ResourceService resourceService;
63
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080064 private ApplicationId appId;
65
66 @Activate
67 public void activate() {
68 appId = coreService.registerApplication("org.onosproject.net.intent");
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080069 registrator.registerCompiler(PathIntent.class, this, false);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080070 }
71
72 @Deactivate
73 public void deactivate() {
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080074 registrator.unregisterCompiler(PathIntent.class, false);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080075 }
76
77 @Override
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080078 public List<Intent> compile(PathIntent intent, List<Intent> installable) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080079
Ray Milkey661c38c2016-02-26 17:12:17 -080080 List<FlowRule> rules = new LinkedList<>();
81 List<DeviceId> devices = new LinkedList<>();
82 compile(this, intent, rules, devices);
Sho SHIMIZUe8f656c2016-02-24 14:05:17 -080083
Michele Santuari6096acd2016-02-09 17:00:37 +010084
helenyrwu2a674902016-07-20 09:48:04 -070085 return ImmutableList.of(new FlowRuleIntent(appId, null, rules, intent.resources(), intent.type()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080086 }
87
Ray Milkey661c38c2016-02-26 17:12:17 -080088 @Override
89 public Logger log() {
90 return log;
91 }
92
93 @Override
94 public ResourceService resourceService() {
95 return resourceService;
96 }
97
Michele Santuari6096acd2016-02-09 17:00:37 +010098
Ray Milkey661c38c2016-02-26 17:12:17 -080099 @Override
100 public void createFlow(TrafficSelector originalSelector, TrafficTreatment originalTreatment,
101 ConnectPoint ingress, ConnectPoint egress,
102 int priority, boolean applyTreatment,
103 List<FlowRule> rules,
104 List<DeviceId> devices) {
Michele Santuari6096acd2016-02-09 17:00:37 +0100105
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());
Michele Santuari6096acd2016-02-09 17:00:37 +0100126
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800127 }
128}