blob: 83547e22a3f26bb0422401e12a0852a0219761c2 [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
18import com.google.common.collect.HashMultimap;
Pier Ventred48320e2016-08-17 16:25:47 -070019import com.google.common.collect.ImmutableSet;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080020import com.google.common.collect.SetMultimap;
Pier Ventred48320e2016-08-17 16:25:47 -070021import com.google.common.collect.Sets;
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;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080029import org.onosproject.net.DeviceId;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080030import org.onosproject.net.PortNumber;
31import org.onosproject.net.flow.DefaultFlowRule;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080032import org.onosproject.net.flow.FlowRule;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080033import org.onosproject.net.intent.FlowRuleIntent;
34import org.onosproject.net.intent.Intent;
35import org.onosproject.net.intent.IntentCompiler;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080036import org.onosproject.net.intent.LinkCollectionIntent;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080037
38import java.util.ArrayList;
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070039import java.util.Collections;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080040import java.util.List;
41import java.util.Set;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080042
Pier Ventred48320e2016-08-17 16:25:47 -070043/**
44 * Compiler to produce flow rules from link collections.
45 */
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080046@Component(immediate = true)
Pier Ventred48320e2016-08-17 16:25:47 -070047public class LinkCollectionIntentCompiler
48 extends LinkCollectionCompiler<FlowRule>
49 implements IntentCompiler<LinkCollectionIntent> {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080050
51 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080052 protected IntentConfigurableRegistrator registrator;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080053
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected CoreService coreService;
56
57 private ApplicationId appId;
58
59 @Activate
60 public void activate() {
61 appId = coreService.registerApplication("org.onosproject.net.intent");
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080062 registrator.registerCompiler(LinkCollectionIntent.class, this, false);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080063 }
64
65 @Deactivate
66 public void deactivate() {
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080067 registrator.unregisterCompiler(LinkCollectionIntent.class, false);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080068 }
69
70 @Override
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080071 public List<Intent> compile(LinkCollectionIntent intent, List<Intent> installable) {
Pier Ventred48320e2016-08-17 16:25:47 -070072
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080073 SetMultimap<DeviceId, PortNumber> inputPorts = HashMultimap.create();
74 SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create();
75
Pier Ventred48320e2016-08-17 16:25:47 -070076 computePorts(intent, inputPorts, outputPorts);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080077
78 List<FlowRule> rules = new ArrayList<>();
79 for (DeviceId deviceId: outputPorts.keys()) {
80 rules.addAll(createRules(intent, deviceId, inputPorts.get(deviceId), outputPorts.get(deviceId)));
81 }
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070082 return Collections.singletonList(new FlowRuleIntent(appId, rules, intent.resources()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080083 }
84
Pier Ventred48320e2016-08-17 16:25:47 -070085 @Override
86 protected List<FlowRule> createRules(LinkCollectionIntent intent, DeviceId deviceId,
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080087 Set<PortNumber> inPorts, Set<PortNumber> outPorts) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080088
Pier Ventred48320e2016-08-17 16:25:47 -070089 Set<PortNumber> ingressPorts = Sets.newHashSet();
90 Set<PortNumber> egressPorts = Sets.newHashSet();
91
92 computePorts(intent, deviceId, ingressPorts, egressPorts);
Brian O'Connor406e2642016-04-18 11:45:35 -070093
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080094 List<FlowRule> rules = new ArrayList<>(inPorts.size());
Pier Ventred48320e2016-08-17 16:25:47 -070095 Set<PortNumber> copyIngressPorts = ImmutableSet.copyOf(ingressPorts);
96 Set<PortNumber> copyEgressPorts = ImmutableSet.copyOf(egressPorts);
Nicholas Dean126b8af2016-07-18 14:43:13 -070097
Pier Ventred48320e2016-08-17 16:25:47 -070098 inPorts.forEach(inport -> {
99 ForwardingInstructions instructions = this.createForwardingInstructions(intent,
100 inport,
Pier Ventre647138f2016-08-26 17:32:44 -0700101 deviceId,
Pier Ventred48320e2016-08-17 16:25:47 -0700102 outPorts,
103 copyIngressPorts,
104 copyEgressPorts);
105 FlowRule rule = DefaultFlowRule.builder()
106 .forDevice(deviceId)
107 .withSelector(instructions.selector())
108 .withTreatment(instructions.treatment())
109 .withPriority(intent.priority())
110 .fromApp(appId)
111 .makePermanent()
112 .build();
113 rules.add(rule);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800114 }
Pier Ventred48320e2016-08-17 16:25:47 -0700115 );
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800116
117 return rules;
118 }
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700119}