blob: 1f2242913f03f04d593458c3caf530c75c9dc2b0 [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;
Pier Ventre27d42572016-08-29 17:37:08 -070037import org.slf4j.Logger;
38import org.slf4j.LoggerFactory;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080039
40import java.util.ArrayList;
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070041import java.util.Collections;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080042import java.util.List;
43import java.util.Set;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080044
Pier Ventred48320e2016-08-17 16:25:47 -070045/**
46 * Compiler to produce flow rules from link collections.
47 */
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080048@Component(immediate = true)
Pier Ventred48320e2016-08-17 16:25:47 -070049public class LinkCollectionIntentCompiler
50 extends LinkCollectionCompiler<FlowRule>
51 implements IntentCompiler<LinkCollectionIntent> {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080052
Pier Ventre27d42572016-08-29 17:37:08 -070053 private static Logger log = LoggerFactory.getLogger(LinkCollectionIntentCompiler.class);
54
55
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080056 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080057 protected IntentConfigurableRegistrator registrator;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080058
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected CoreService coreService;
61
62 private ApplicationId appId;
63
64 @Activate
65 public void activate() {
66 appId = coreService.registerApplication("org.onosproject.net.intent");
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080067 registrator.registerCompiler(LinkCollectionIntent.class, this, false);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080068 }
69
70 @Deactivate
71 public void deactivate() {
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080072 registrator.unregisterCompiler(LinkCollectionIntent.class, false);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080073 }
74
75 @Override
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080076 public List<Intent> compile(LinkCollectionIntent intent, List<Intent> installable) {
Pier Ventred48320e2016-08-17 16:25:47 -070077
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080078 SetMultimap<DeviceId, PortNumber> inputPorts = HashMultimap.create();
79 SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create();
80
Pier Ventred48320e2016-08-17 16:25:47 -070081 computePorts(intent, inputPorts, outputPorts);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080082
83 List<FlowRule> rules = new ArrayList<>();
Yi Tseng155370e2016-09-20 11:08:32 -070084 for (DeviceId deviceId: outputPorts.keySet()) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080085 rules.addAll(createRules(intent, deviceId, inputPorts.get(deviceId), outputPorts.get(deviceId)));
86 }
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070087 return Collections.singletonList(new FlowRuleIntent(appId, rules, intent.resources()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080088 }
89
Pier Ventred48320e2016-08-17 16:25:47 -070090 @Override
91 protected List<FlowRule> createRules(LinkCollectionIntent intent, DeviceId deviceId,
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080092 Set<PortNumber> inPorts, Set<PortNumber> outPorts) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080093
Pier Ventred48320e2016-08-17 16:25:47 -070094 Set<PortNumber> ingressPorts = Sets.newHashSet();
95 Set<PortNumber> egressPorts = Sets.newHashSet();
96
97 computePorts(intent, deviceId, ingressPorts, egressPorts);
Brian O'Connor406e2642016-04-18 11:45:35 -070098
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080099 List<FlowRule> rules = new ArrayList<>(inPorts.size());
Pier Ventred48320e2016-08-17 16:25:47 -0700100 Set<PortNumber> copyIngressPorts = ImmutableSet.copyOf(ingressPorts);
101 Set<PortNumber> copyEgressPorts = ImmutableSet.copyOf(egressPorts);
Nicholas Dean126b8af2016-07-18 14:43:13 -0700102
Pier Ventred48320e2016-08-17 16:25:47 -0700103 inPorts.forEach(inport -> {
104 ForwardingInstructions instructions = this.createForwardingInstructions(intent,
105 inport,
Pier Ventre647138f2016-08-26 17:32:44 -0700106 deviceId,
Pier Ventred48320e2016-08-17 16:25:47 -0700107 outPorts,
108 copyIngressPorts,
109 copyEgressPorts);
110 FlowRule rule = DefaultFlowRule.builder()
111 .forDevice(deviceId)
112 .withSelector(instructions.selector())
113 .withTreatment(instructions.treatment())
114 .withPriority(intent.priority())
115 .fromApp(appId)
116 .makePermanent()
117 .build();
118 rules.add(rule);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800119 }
Pier Ventred48320e2016-08-17 16:25:47 -0700120 );
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800121
122 return rules;
123 }
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700124}