blob: 7222f0fe42dbc68afeaeca55acfd13b439f71eeb [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
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;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010038import org.onosproject.net.newresource.ResourceService;
Brian O'Connor6de2e202015-05-21 14:30:41 -070039import org.onosproject.net.resource.link.LinkResourceAllocations;
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;
44import java.util.Set;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010045
Michele Santuari69fc2ff2015-12-03 17:05:35 +010046import static org.slf4j.LoggerFactory.getLogger;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080047
Michele Santuari6096acd2016-02-09 17:00:37 +010048
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080049@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
Michele Santuari6096acd2016-02-09 17:00:37 +010087
Sho SHIMIZUe8f656c2016-02-24 14:05:17 -080088 return ImmutableList.of(new FlowRuleIntent(appId, null, rules, intent.resources()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080089 }
90
Ray Milkey661c38c2016-02-26 17:12:17 -080091 @Override
92 public Logger log() {
93 return log;
94 }
95
96 @Override
97 public ResourceService resourceService() {
98 return resourceService;
99 }
100
Michele Santuari6096acd2016-02-09 17:00:37 +0100101
Ray Milkey661c38c2016-02-26 17:12:17 -0800102 @Override
103 public void createFlow(TrafficSelector originalSelector, TrafficTreatment originalTreatment,
104 ConnectPoint ingress, ConnectPoint egress,
105 int priority, boolean applyTreatment,
106 List<FlowRule> rules,
107 List<DeviceId> devices) {
Michele Santuari6096acd2016-02-09 17:00:37 +0100108
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800109 TrafficSelector selector = DefaultTrafficSelector.builder(originalSelector)
110 .matchInPort(ingress.port())
111 .build();
112
113 TrafficTreatment.Builder treatmentBuilder;
Michele Santuari69fc2ff2015-12-03 17:05:35 +0100114 if (applyTreatment) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800115 treatmentBuilder = DefaultTrafficTreatment.builder(originalTreatment);
116 } else {
117 treatmentBuilder = DefaultTrafficTreatment.builder();
118 }
119 TrafficTreatment treatment = treatmentBuilder.setOutput(egress.port()).build();
120
Ray Milkey661c38c2016-02-26 17:12:17 -0800121 rules.add(DefaultFlowRule.builder()
Ray Milkeyd13a37b2015-06-12 11:55:17 -0700122 .forDevice(ingress.deviceId())
123 .withSelector(selector)
124 .withTreatment(treatment)
Brian O'Connor81134662015-06-25 17:23:33 -0400125 .withPriority(priority)
Ray Milkeyd13a37b2015-06-12 11:55:17 -0700126 .fromApp(appId)
127 .makePermanent()
Ray Milkey661c38c2016-02-26 17:12:17 -0800128 .build());
Michele Santuari6096acd2016-02-09 17:00:37 +0100129
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800130 }
131}