blob: f719a009f6d5df9f8a4c9f19ddcce1d08aabbf16 [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
helenyrwu2a674902016-07-20 09:48:04 -070086 return ImmutableList.of(new FlowRuleIntent(appId, null, rules, intent.resources(), intent.type()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080087 }
88
Ray Milkey661c38c2016-02-26 17:12:17 -080089 @Override
90 public Logger log() {
91 return log;
92 }
93
94 @Override
95 public ResourceService resourceService() {
96 return resourceService;
97 }
98
Michele Santuari6096acd2016-02-09 17:00:37 +010099
Ray Milkey661c38c2016-02-26 17:12:17 -0800100 @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) {
Michele Santuari6096acd2016-02-09 17:00:37 +0100106
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800107 TrafficSelector selector = DefaultTrafficSelector.builder(originalSelector)
108 .matchInPort(ingress.port())
109 .build();
110
111 TrafficTreatment.Builder treatmentBuilder;
Michele Santuari69fc2ff2015-12-03 17:05:35 +0100112 if (applyTreatment) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800113 treatmentBuilder = DefaultTrafficTreatment.builder(originalTreatment);
114 } else {
115 treatmentBuilder = DefaultTrafficTreatment.builder();
116 }
117 TrafficTreatment treatment = treatmentBuilder.setOutput(egress.port()).build();
118
Ray Milkey661c38c2016-02-26 17:12:17 -0800119 rules.add(DefaultFlowRule.builder()
Ray Milkeyd13a37b2015-06-12 11:55:17 -0700120 .forDevice(ingress.deviceId())
121 .withSelector(selector)
122 .withTreatment(treatment)
Brian O'Connor81134662015-06-25 17:23:33 -0400123 .withPriority(priority)
Ray Milkeyd13a37b2015-06-12 11:55:17 -0700124 .fromApp(appId)
125 .makePermanent()
Ray Milkey661c38c2016-02-26 17:12:17 -0800126 .build());
Michele Santuari6096acd2016-02-09 17:00:37 +0100127
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800128 }
129}