blob: 369e52b55bf551e50c3faf16a66f47c77edf23e7 [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
alshabibab984662014-12-04 18:56:18 -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 */
Sho SHIMIZU6c28f832015-02-20 16:12:19 -080016package org.onosproject.net.intent.impl.compiler;
Michele Santuari4a338072014-11-05 18:38:55 +010017
Jonathan Hart066244c2015-06-23 09:46:19 -070018import com.google.common.collect.ImmutableSet;
Michele Santuari4a338072014-11-05 18:38:55 +010019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
Luca Preted26ea652017-01-03 15:59:30 -080022import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.Link;
26import org.onosproject.net.Path;
Luca Preted26ea652017-01-03 15:59:30 -080027import org.onosproject.net.device.DeviceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.intent.Intent;
Luca Preted26ea652017-01-03 15:59:30 -080029import org.onosproject.net.intent.IntentException;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.intent.LinkCollectionIntent;
31import org.onosproject.net.intent.SinglePointToMultiPointIntent;
Michele Santuari4a338072014-11-05 18:38:55 +010032
Jonathan Hart066244c2015-06-23 09:46:19 -070033import java.util.Collections;
34import java.util.HashSet;
35import java.util.List;
36import java.util.Set;
Luca Pretede10c782017-01-05 17:23:08 -080037import java.util.stream.Collectors;
38import java.util.stream.Stream;
Pavlin Radoslavov2811c402015-02-25 14:30:17 -080039
Luca Preted26ea652017-01-03 15:59:30 -080040import static org.onosproject.net.intent.constraint.PartialFailureConstraint.intentAllowsPartialFailure;
41
Michele Santuari4a338072014-11-05 18:38:55 +010042@Component(immediate = true)
43public class SinglePointToMultiPointIntentCompiler
44 extends ConnectivityIntentCompiler<SinglePointToMultiPointIntent> {
45
Luca Preted26ea652017-01-03 15:59:30 -080046 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
47 protected DeviceService deviceService;
Michele Santuari4a338072014-11-05 18:38:55 +010048
49 @Activate
50 public void activate() {
Luca Preted26ea652017-01-03 15:59:30 -080051 intentManager.registerCompiler(SinglePointToMultiPointIntent.class, this);
Michele Santuari4a338072014-11-05 18:38:55 +010052 }
53
54 @Deactivate
55 public void deactivate() {
56 intentManager.unregisterCompiler(SinglePointToMultiPointIntent.class);
57 }
58
Michele Santuari4a338072014-11-05 18:38:55 +010059 @Override
60 public List<Intent> compile(SinglePointToMultiPointIntent intent,
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080061 List<Intent> installable) {
Michele Santuari4a338072014-11-05 18:38:55 +010062 Set<Link> links = new HashSet<>();
Jonathan Hart066244c2015-06-23 09:46:19 -070063
Luca Preted26ea652017-01-03 15:59:30 -080064 final boolean allowMissingPaths = intentAllowsPartialFailure(intent);
65 boolean hasPaths = false;
66 boolean missingSomePaths = false;
67
Michele Santuari4a338072014-11-05 18:38:55 +010068 for (ConnectPoint egressPoint : intent.egressPoints()) {
Jonathan Hart066244c2015-06-23 09:46:19 -070069 if (egressPoint.deviceId().equals(intent.ingressPoint().deviceId())) {
Luca Preted26ea652017-01-03 15:59:30 -080070 // Do not need to look for paths, since ingress and egress
71 // devices are the same.
72 if (deviceService.isAvailable(egressPoint.deviceId())) {
73 hasPaths = true;
74 } else {
75 missingSomePaths = true;
76 }
Jonathan Hart066244c2015-06-23 09:46:19 -070077 continue;
78 }
79
Michele Santuari4a338072014-11-05 18:38:55 +010080 Path path = getPath(intent, intent.ingressPoint().deviceId(), egressPoint.deviceId());
Luca Pretede10c782017-01-05 17:23:08 -080081
Luca Preted26ea652017-01-03 15:59:30 -080082 if (path != null) {
83 hasPaths = true;
84 links.addAll(path.links());
85 } else {
86 missingSomePaths = true;
87 }
88 }
89
Luca Pretede10c782017-01-05 17:23:08 -080090 // Allocate bandwidth if a bandwidth constraint is set
91 ConnectPoint ingressCP = intent.filteredIngressPoint().connectPoint();
92 List<ConnectPoint> egressCPs =
93 intent.filteredEgressPoints().stream()
94 .map(fcp -> fcp.connectPoint())
95 .collect(Collectors.toList());
96
97 List<ConnectPoint> pathCPs =
98 links.stream()
99 .flatMap(l -> Stream.of(l.src(), l.dst()))
100 .collect(Collectors.toList());
101
102 pathCPs.add(ingressCP);
103 pathCPs.addAll(egressCPs);
104
105 allocateBandwidth(intent, pathCPs);
106
Luca Preted26ea652017-01-03 15:59:30 -0800107 if (!hasPaths) {
108 throw new IntentException("Cannot find any path between ingress and egress points.");
109 } else if (!allowMissingPaths && missingSomePaths) {
110 throw new IntentException("Missing some paths between ingress and egress points.");
Michele Santuari4a338072014-11-05 18:38:55 +0100111 }
112
Ray Milkeyebc5d222015-03-18 15:45:36 -0700113 Intent result = LinkCollectionIntent.builder()
114 .appId(intent.appId())
115 .key(intent.key())
Pier Ventre973bb032016-10-11 08:57:39 -0700116 .selector(intent.selector())
Ray Milkeyebc5d222015-03-18 15:45:36 -0700117 .treatment(intent.treatment())
118 .links(links)
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700119 .filteredIngressPoints(ImmutableSet.of(intent.filteredIngressPoint()))
120 .filteredEgressPoints(intent.filteredEgressPoints())
Ray Milkeyebc5d222015-03-18 15:45:36 -0700121 .priority(intent.priority())
Nicholas Dean126b8af2016-07-18 14:43:13 -0700122 .applyTreatmentOnEgress(true)
Sho SHIMIZU1b46a972015-04-10 17:33:00 -0700123 .constraints(intent.constraints())
Luca Prete670ac5d2017-02-03 15:55:43 -0800124 .resourceGroup(intent.resourceGroup())
Ray Milkeyebc5d222015-03-18 15:45:36 -0700125 .build();
Michele Santuari4a338072014-11-05 18:38:55 +0100126
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700127 return Collections.singletonList(result);
Michele Santuari4a338072014-11-05 18:38:55 +0100128 }
Nicholas Dean126b8af2016-07-18 14:43:13 -0700129}