blob: e9e319eda91ac6f877dda43a584ca4f3fe74acb9 [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;
30import org.onosproject.net.Link;
31import org.onosproject.net.PortNumber;
32import org.onosproject.net.flow.DefaultFlowRule;
33import org.onosproject.net.flow.DefaultTrafficSelector;
34import org.onosproject.net.flow.DefaultTrafficTreatment;
35import org.onosproject.net.flow.FlowRule;
36import org.onosproject.net.flow.TrafficSelector;
37import org.onosproject.net.flow.TrafficTreatment;
38import org.onosproject.net.intent.FlowRuleIntent;
39import org.onosproject.net.intent.Intent;
40import org.onosproject.net.intent.IntentCompiler;
41import org.onosproject.net.intent.IntentExtensionService;
42import org.onosproject.net.intent.LinkCollectionIntent;
43import org.onosproject.net.resource.LinkResourceAllocations;
44
45import java.util.ArrayList;
46import java.util.Arrays;
47import java.util.List;
48import java.util.Set;
49import java.util.stream.Collectors;
50
51@Component(immediate = true)
52public class LinkCollectionIntentCompiler implements IntentCompiler<LinkCollectionIntent> {
53
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected IntentExtensionService intentManager;
56
57 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58 protected CoreService coreService;
59
60 private ApplicationId appId;
61
62 @Activate
63 public void activate() {
64 appId = coreService.registerApplication("org.onosproject.net.intent");
65 intentManager.registerCompiler(LinkCollectionIntent.class, this);
66 }
67
68 @Deactivate
69 public void deactivate() {
70 intentManager.unregisterCompiler(LinkCollectionIntent.class);
71 }
72
73 @Override
74 public List<Intent> compile(LinkCollectionIntent intent, List<Intent> installable,
75 Set<LinkResourceAllocations> resources) {
76 SetMultimap<DeviceId, PortNumber> inputPorts = HashMultimap.create();
77 SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create();
78
79 for (Link link : intent.links()) {
80 inputPorts.put(link.dst().deviceId(), link.dst().port());
81 outputPorts.put(link.src().deviceId(), link.src().port());
82 }
83
84 for (ConnectPoint ingressPoint : intent.ingressPoints()) {
85 inputPorts.put(ingressPoint.deviceId(), ingressPoint.port());
86 }
87
88 for (ConnectPoint egressPoint : intent.egressPoints()) {
89 outputPorts.put(egressPoint.deviceId(), egressPoint.port());
90 }
91
92 List<FlowRule> rules = new ArrayList<>();
93 for (DeviceId deviceId: outputPorts.keys()) {
94 rules.addAll(createRules(intent, deviceId, inputPorts.get(deviceId), outputPorts.get(deviceId)));
95 }
96 return Arrays.asList(new FlowRuleIntent(appId, rules));
97 }
98
99 private List<FlowRule> createRules(LinkCollectionIntent intent, DeviceId deviceId,
100 Set<PortNumber> inPorts, Set<PortNumber> outPorts) {
101 Set<PortNumber> ingressPorts = intent.ingressPoints().stream()
102 .filter(point -> point.deviceId().equals(deviceId))
103 .map(ConnectPoint::port)
104 .collect(Collectors.toSet());
105
106 TrafficTreatment.Builder defaultTreatmentBuilder = DefaultTrafficTreatment.builder();
107 outPorts.stream()
108 .forEach(defaultTreatmentBuilder::setOutput);
109 TrafficTreatment defaultTreatment = defaultTreatmentBuilder.build();
110
111 TrafficTreatment.Builder ingressTreatmentBuilder = DefaultTrafficTreatment.builder(intent.treatment());
112 outPorts.stream()
113 .forEach(ingressTreatmentBuilder::setOutput);
114 TrafficTreatment ingressTreatment = ingressTreatmentBuilder.build();
115
116 List<FlowRule> rules = new ArrayList<>(inPorts.size());
117 for (PortNumber inPort: inPorts) {
118 TrafficSelector selector = DefaultTrafficSelector.builder(intent.selector()).matchInPort(inPort).build();
119 TrafficTreatment treatment;
120 if (ingressPorts.contains(inPort)) {
121 treatment = ingressTreatment;
122 } else {
123 treatment = defaultTreatment;
124 }
125
126 DefaultFlowRule rule = new DefaultFlowRule(deviceId, selector, treatment, 123, appId,
127 new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)), 0, true);
128
129 rules.add(rule);
130 }
131
132 return rules;
133 }
134}