blob: e464e99cef43dced19c284a676044a0d54891c35 [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 Ventre766995d2016-10-05 22:15:56 -070019import com.google.common.collect.ImmutableMap;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080020import com.google.common.collect.SetMultimap;
21import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
Pier Ventre766995d2016-10-05 22:15:56 -070026import org.onlab.util.Identifier;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080027import org.onosproject.core.ApplicationId;
28import org.onosproject.core.CoreService;
Pier Ventre766995d2016-10-05 22:15:56 -070029import org.onosproject.net.ConnectPoint;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080030import org.onosproject.net.DeviceId;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080031import org.onosproject.net.PortNumber;
32import org.onosproject.net.flow.DefaultFlowRule;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080033import org.onosproject.net.flow.FlowRule;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080034import 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.LinkCollectionIntent;
Pier Ventre766995d2016-10-05 22:15:56 -070038import org.onosproject.net.intent.constraint.EncapsulationConstraint;
39import org.onosproject.net.resource.ResourceService;
40import org.onosproject.net.resource.impl.LabelAllocator;
Pier Ventre27d42572016-08-29 17:37:08 -070041import org.slf4j.Logger;
42import org.slf4j.LoggerFactory;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080043
44import java.util.ArrayList;
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070045import java.util.Collections;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080046import java.util.List;
Pier Ventre766995d2016-10-05 22:15:56 -070047import java.util.Map;
48import java.util.Optional;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080049import java.util.Set;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080050
Pier Ventred48320e2016-08-17 16:25:47 -070051/**
52 * Compiler to produce flow rules from link collections.
53 */
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080054@Component(immediate = true)
Pier Ventred48320e2016-08-17 16:25:47 -070055public class LinkCollectionIntentCompiler
56 extends LinkCollectionCompiler<FlowRule>
57 implements IntentCompiler<LinkCollectionIntent> {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080058
Pier Ventre27d42572016-08-29 17:37:08 -070059 private static Logger log = LoggerFactory.getLogger(LinkCollectionIntentCompiler.class);
60
61
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080062 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080063 protected IntentConfigurableRegistrator registrator;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080064
65 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected CoreService coreService;
67
Pier Ventre766995d2016-10-05 22:15:56 -070068 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
69 protected ResourceService resourceService;
70
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080071 private ApplicationId appId;
72
73 @Activate
74 public void activate() {
75 appId = coreService.registerApplication("org.onosproject.net.intent");
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080076 registrator.registerCompiler(LinkCollectionIntent.class, this, false);
Pier Ventre766995d2016-10-05 22:15:56 -070077 if (labelAllocator == null) {
78 labelAllocator = new LabelAllocator(resourceService);
79 }
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080080 }
81
82 @Deactivate
83 public void deactivate() {
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080084 registrator.unregisterCompiler(LinkCollectionIntent.class, false);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080085 }
86
87 @Override
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080088 public List<Intent> compile(LinkCollectionIntent intent, List<Intent> installable) {
Pier Ventred48320e2016-08-17 16:25:47 -070089
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080090 SetMultimap<DeviceId, PortNumber> inputPorts = HashMultimap.create();
91 SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create();
Pier Ventre766995d2016-10-05 22:15:56 -070092 Map<ConnectPoint, Identifier<?>> labels = ImmutableMap.of();
93
94 Optional<EncapsulationConstraint> encapConstraint = this.getIntentEncapConstraint(intent);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080095
Pier Ventred48320e2016-08-17 16:25:47 -070096 computePorts(intent, inputPorts, outputPorts);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080097
Pier Ventre766995d2016-10-05 22:15:56 -070098 if (encapConstraint.isPresent()) {
99 labels = labelAllocator.assignLabelToPorts(intent.links(),
100 intent.id(),
101 encapConstraint.get().encapType());
102 }
103
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800104 List<FlowRule> rules = new ArrayList<>();
Yi Tseng155370e2016-09-20 11:08:32 -0700105 for (DeviceId deviceId: outputPorts.keySet()) {
Pier Ventre766995d2016-10-05 22:15:56 -0700106 rules.addAll(createRules(
107 intent,
108 deviceId,
109 inputPorts.get(deviceId),
110 outputPorts.get(deviceId),
111 labels)
112 );
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800113 }
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700114 return Collections.singletonList(new FlowRuleIntent(appId, rules, intent.resources()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800115 }
116
Pier Ventred48320e2016-08-17 16:25:47 -0700117 @Override
Pier Ventre766995d2016-10-05 22:15:56 -0700118 protected List<FlowRule> createRules(LinkCollectionIntent intent,
119 DeviceId deviceId,
120 Set<PortNumber> inPorts,
121 Set<PortNumber> outPorts,
122 Map<ConnectPoint, Identifier<?>> labels) {
Brian O'Connor406e2642016-04-18 11:45:35 -0700123
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800124 List<FlowRule> rules = new ArrayList<>(inPorts.size());
Pier Ventre766995d2016-10-05 22:15:56 -0700125 /*
126 * Looking for the encapsulation constraint
127 */
128 Optional<EncapsulationConstraint> encapConstraint = this.getIntentEncapConstraint(intent);
Nicholas Dean126b8af2016-07-18 14:43:13 -0700129
Pier Ventred48320e2016-08-17 16:25:47 -0700130 inPorts.forEach(inport -> {
Pier Ventre766995d2016-10-05 22:15:56 -0700131
132 ForwardingInstructions instructions = this.createForwardingInstruction(
133 encapConstraint,
134 intent,
135 inport,
136 outPorts,
137 deviceId,
138 labels
139 );
140
Pier Ventred48320e2016-08-17 16:25:47 -0700141 FlowRule rule = DefaultFlowRule.builder()
142 .forDevice(deviceId)
143 .withSelector(instructions.selector())
144 .withTreatment(instructions.treatment())
145 .withPriority(intent.priority())
146 .fromApp(appId)
147 .makePermanent()
148 .build();
149 rules.add(rule);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800150 }
Pier Ventred48320e2016-08-17 16:25:47 -0700151 );
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800152
153 return rules;
154 }
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700155}