blob: 0b9bdd123bb7f82db9823f9bd8668f067e69409d [file] [log] [blame]
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import org.osgi.service.component.annotations.Activate;
20import org.osgi.service.component.annotations.Component;
21import org.osgi.service.component.annotations.Deactivate;
22import org.osgi.service.component.annotations.Reference;
23import org.osgi.service.component.annotations.ReferenceCardinality;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080024import 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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070056 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080057 protected CoreService coreService;
58
Ray Milkeyd84f89b2018-08-17 14:54:17 -070059 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080060 protected IntentConfigurableRegistrator registrator;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080061
Ray Milkeyd84f89b2018-08-17 14:54:17 -070062 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Michele Santuari69fc2ff2015-12-03 17:05:35 +010063 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
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -070086
87 return ImmutableList.of(new FlowRuleIntent(appId,
88 intent.key(),
89 rules,
90 intent.resources(),
Luca Prete670ac5d2017-02-03 15:55:43 -080091 intent.type(),
92 intent.resourceGroup()
93 ));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080094 }
95
Ray Milkey661c38c2016-02-26 17:12:17 -080096 @Override
97 public Logger log() {
98 return log;
99 }
100
101 @Override
102 public ResourceService resourceService() {
103 return resourceService;
104 }
105
Michele Santuari6096acd2016-02-09 17:00:37 +0100106
Ray Milkey661c38c2016-02-26 17:12:17 -0800107 @Override
108 public void createFlow(TrafficSelector originalSelector, TrafficTreatment originalTreatment,
109 ConnectPoint ingress, ConnectPoint egress,
110 int priority, boolean applyTreatment,
111 List<FlowRule> rules,
112 List<DeviceId> devices) {
Michele Santuari6096acd2016-02-09 17:00:37 +0100113
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800114 TrafficSelector selector = DefaultTrafficSelector.builder(originalSelector)
115 .matchInPort(ingress.port())
116 .build();
117
118 TrafficTreatment.Builder treatmentBuilder;
Michele Santuari69fc2ff2015-12-03 17:05:35 +0100119 if (applyTreatment) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800120 treatmentBuilder = DefaultTrafficTreatment.builder(originalTreatment);
121 } else {
122 treatmentBuilder = DefaultTrafficTreatment.builder();
123 }
124 TrafficTreatment treatment = treatmentBuilder.setOutput(egress.port()).build();
125
Ray Milkey661c38c2016-02-26 17:12:17 -0800126 rules.add(DefaultFlowRule.builder()
Ray Milkeyd13a37b2015-06-12 11:55:17 -0700127 .forDevice(ingress.deviceId())
128 .withSelector(selector)
129 .withTreatment(treatment)
Brian O'Connor81134662015-06-25 17:23:33 -0400130 .withPriority(priority)
Ray Milkeyd13a37b2015-06-12 11:55:17 -0700131 .fromApp(appId)
132 .makePermanent()
Ray Milkey661c38c2016-02-26 17:12:17 -0800133 .build());
Michele Santuari6096acd2016-02-09 17:00:37 +0100134
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800135 }
136}