blob: 9085f08e5dd6a8965da88cbba862681fec0e6132 [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
18import com.google.common.collect.HashMultimap;
19import com.google.common.collect.SetMultimap;
20import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
27import org.onosproject.core.DefaultGroupId;
28import org.onosproject.net.ConnectPoint;
29import org.onosproject.net.DeviceId;
Ray Milkey6e0fb302015-04-16 14:44:12 -070030import org.onosproject.net.EdgeLink;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080031import org.onosproject.net.Link;
32import org.onosproject.net.PortNumber;
33import org.onosproject.net.flow.DefaultFlowRule;
34import org.onosproject.net.flow.DefaultTrafficSelector;
35import org.onosproject.net.flow.DefaultTrafficTreatment;
36import org.onosproject.net.flow.FlowRule;
37import org.onosproject.net.flow.TrafficSelector;
38import org.onosproject.net.flow.TrafficTreatment;
39import org.onosproject.net.intent.FlowRuleIntent;
40import org.onosproject.net.intent.Intent;
41import org.onosproject.net.intent.IntentCompiler;
42import org.onosproject.net.intent.IntentExtensionService;
43import org.onosproject.net.intent.LinkCollectionIntent;
Brian O'Connor6de2e202015-05-21 14:30:41 -070044import org.onosproject.net.resource.link.LinkResourceAllocations;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080045
46import java.util.ArrayList;
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070047import java.util.Collections;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080048import java.util.List;
49import java.util.Set;
50import java.util.stream.Collectors;
51
52@Component(immediate = true)
53public class LinkCollectionIntentCompiler implements IntentCompiler<LinkCollectionIntent> {
54
55 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
56 protected IntentExtensionService intentManager;
57
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
59 protected CoreService coreService;
60
61 private ApplicationId appId;
62
63 @Activate
64 public void activate() {
65 appId = coreService.registerApplication("org.onosproject.net.intent");
66 intentManager.registerCompiler(LinkCollectionIntent.class, this);
67 }
68
69 @Deactivate
70 public void deactivate() {
71 intentManager.unregisterCompiler(LinkCollectionIntent.class);
72 }
73
74 @Override
75 public List<Intent> compile(LinkCollectionIntent intent, List<Intent> installable,
76 Set<LinkResourceAllocations> resources) {
77 SetMultimap<DeviceId, PortNumber> inputPorts = HashMultimap.create();
78 SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create();
79
80 for (Link link : intent.links()) {
Ray Milkey6e0fb302015-04-16 14:44:12 -070081 DeviceId srcDeviceId;
82 DeviceId dstDeviceId;
83
84 if (link instanceof EdgeLink) {
85 EdgeLink edgeLink = (EdgeLink) link;
86 dstDeviceId = edgeLink.hostLocation().deviceId();
87 srcDeviceId = dstDeviceId;
88 } else {
89 inputPorts.put(link.dst().deviceId(), link.dst().port());
90 srcDeviceId = link.src().deviceId();
91 }
92 outputPorts.put(srcDeviceId, link.src().port());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080093 }
94
95 for (ConnectPoint ingressPoint : intent.ingressPoints()) {
96 inputPorts.put(ingressPoint.deviceId(), ingressPoint.port());
97 }
98
99 for (ConnectPoint egressPoint : intent.egressPoints()) {
100 outputPorts.put(egressPoint.deviceId(), egressPoint.port());
101 }
102
103 List<FlowRule> rules = new ArrayList<>();
104 for (DeviceId deviceId: outputPorts.keys()) {
105 rules.addAll(createRules(intent, deviceId, inputPorts.get(deviceId), outputPorts.get(deviceId)));
106 }
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700107 return Collections.singletonList(new FlowRuleIntent(appId, rules, intent.resources()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800108 }
109
110 private List<FlowRule> createRules(LinkCollectionIntent intent, DeviceId deviceId,
111 Set<PortNumber> inPorts, Set<PortNumber> outPorts) {
112 Set<PortNumber> ingressPorts = intent.ingressPoints().stream()
113 .filter(point -> point.deviceId().equals(deviceId))
114 .map(ConnectPoint::port)
115 .collect(Collectors.toSet());
116
117 TrafficTreatment.Builder defaultTreatmentBuilder = DefaultTrafficTreatment.builder();
118 outPorts.stream()
119 .forEach(defaultTreatmentBuilder::setOutput);
120 TrafficTreatment defaultTreatment = defaultTreatmentBuilder.build();
121
122 TrafficTreatment.Builder ingressTreatmentBuilder = DefaultTrafficTreatment.builder(intent.treatment());
123 outPorts.stream()
124 .forEach(ingressTreatmentBuilder::setOutput);
125 TrafficTreatment ingressTreatment = ingressTreatmentBuilder.build();
126
127 List<FlowRule> rules = new ArrayList<>(inPorts.size());
128 for (PortNumber inPort: inPorts) {
129 TrafficSelector selector = DefaultTrafficSelector.builder(intent.selector()).matchInPort(inPort).build();
130 TrafficTreatment treatment;
131 if (ingressPorts.contains(inPort)) {
132 treatment = ingressTreatment;
133 } else {
134 treatment = defaultTreatment;
135 }
136
137 DefaultFlowRule rule = new DefaultFlowRule(deviceId, selector, treatment, 123, appId,
138 new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)), 0, true);
139
140 rules.add(rule);
141 }
142
143 return rules;
144 }
145}