blob: 045b0eff3e40a62fa202b17b08598122e1c8d4d2 [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
Ray Milkey661c38c2016-02-26 17:12:17 -080018import java.util.LinkedList;
19import java.util.List;
20import java.util.Set;
21
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080022import org.apache.felix.scr.annotations.Activate;
23import org.apache.felix.scr.annotations.Component;
24import org.apache.felix.scr.annotations.Deactivate;
25import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
27import org.onosproject.core.ApplicationId;
28import org.onosproject.core.CoreService;
29import org.onosproject.net.ConnectPoint;
Ray Milkey661c38c2016-02-26 17:12:17 -080030import org.onosproject.net.DeviceId;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080031import org.onosproject.net.flow.DefaultFlowRule;
32import org.onosproject.net.flow.DefaultTrafficSelector;
33import org.onosproject.net.flow.DefaultTrafficTreatment;
34import org.onosproject.net.flow.FlowRule;
35import org.onosproject.net.flow.TrafficSelector;
36import org.onosproject.net.flow.TrafficTreatment;
37import org.onosproject.net.intent.FlowRuleIntent;
38import org.onosproject.net.intent.Intent;
39import org.onosproject.net.intent.IntentCompiler;
40import org.onosproject.net.intent.IntentExtensionService;
41import org.onosproject.net.intent.PathIntent;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010042import org.onosproject.net.newresource.ResourceService;
Brian O'Connor6de2e202015-05-21 14:30:41 -070043import org.onosproject.net.resource.link.LinkResourceAllocations;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010044import org.slf4j.Logger;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080045
Ray Milkey661c38c2016-02-26 17:12:17 -080046import com.google.common.collect.ImmutableList;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010047
Michele Santuari69fc2ff2015-12-03 17:05:35 +010048import static org.slf4j.LoggerFactory.getLogger;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080049
50@Component(immediate = true)
Ray Milkey661c38c2016-02-26 17:12:17 -080051public class PathIntentCompiler
52 extends PathCompiler<FlowRule>
53 implements IntentCompiler<PathIntent>,
54 PathCompiler.PathCompilerCreateFlow<FlowRule> {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080055
Michele Santuari69fc2ff2015-12-03 17:05:35 +010056 private final Logger log = getLogger(getClass());
57
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080058 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
59 protected CoreService coreService;
60
61 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
62 protected IntentExtensionService intentManager;
63
Michele Santuari69fc2ff2015-12-03 17:05:35 +010064 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 protected ResourceService resourceService;
66
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080067 private ApplicationId appId;
68
69 @Activate
70 public void activate() {
71 appId = coreService.registerApplication("org.onosproject.net.intent");
72 intentManager.registerCompiler(PathIntent.class, this);
73 }
74
75 @Deactivate
76 public void deactivate() {
77 intentManager.unregisterCompiler(PathIntent.class);
78 }
79
80 @Override
81 public List<Intent> compile(PathIntent intent, List<Intent> installable,
82 Set<LinkResourceAllocations> resources) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080083
Ray Milkey661c38c2016-02-26 17:12:17 -080084 List<FlowRule> rules = new LinkedList<>();
85 List<DeviceId> devices = new LinkedList<>();
86 compile(this, intent, rules, devices);
Sho SHIMIZUe8f656c2016-02-24 14:05:17 -080087
88 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
101 @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) {
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());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800127 }
128}