blob: dfd2e0a7269f3c49cd5d2dd6d6c7492cac680595 [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Brian O'Connorabafb502014-12-02 22:26:20 -080019import org.onosproject.net.ConnectPoint;
20import org.onosproject.net.Link;
21import org.onosproject.net.Path;
22import org.onosproject.net.intent.Intent;
Luca Preted26ea652017-01-03 15:59:30 -080023import org.onosproject.net.intent.IntentException;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.intent.LinkCollectionIntent;
25import org.onosproject.net.intent.SinglePointToMultiPointIntent;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070026import org.osgi.service.component.annotations.Activate;
27import org.osgi.service.component.annotations.Component;
28import org.osgi.service.component.annotations.Deactivate;
Michele Santuari4a338072014-11-05 18:38:55 +010029
Jonathan Hart066244c2015-06-23 09:46:19 -070030import java.util.Collections;
31import java.util.HashSet;
32import java.util.List;
33import java.util.Set;
Luca Pretede10c782017-01-05 17:23:08 -080034import java.util.stream.Collectors;
35import java.util.stream.Stream;
Pavlin Radoslavov2811c402015-02-25 14:30:17 -080036
Luca Preted26ea652017-01-03 15:59:30 -080037import static org.onosproject.net.intent.constraint.PartialFailureConstraint.intentAllowsPartialFailure;
38
Michele Santuari4a338072014-11-05 18:38:55 +010039@Component(immediate = true)
40public class SinglePointToMultiPointIntentCompiler
41 extends ConnectivityIntentCompiler<SinglePointToMultiPointIntent> {
42
Michele Santuari4a338072014-11-05 18:38:55 +010043 @Activate
44 public void activate() {
Luca Preted26ea652017-01-03 15:59:30 -080045 intentManager.registerCompiler(SinglePointToMultiPointIntent.class, this);
Michele Santuari4a338072014-11-05 18:38:55 +010046 }
47
48 @Deactivate
49 public void deactivate() {
50 intentManager.unregisterCompiler(SinglePointToMultiPointIntent.class);
51 }
52
Michele Santuari4a338072014-11-05 18:38:55 +010053 @Override
54 public List<Intent> compile(SinglePointToMultiPointIntent intent,
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080055 List<Intent> installable) {
Michele Santuari4a338072014-11-05 18:38:55 +010056 Set<Link> links = new HashSet<>();
Jonathan Hart066244c2015-06-23 09:46:19 -070057
Luca Preted26ea652017-01-03 15:59:30 -080058 final boolean allowMissingPaths = intentAllowsPartialFailure(intent);
59 boolean hasPaths = false;
60 boolean missingSomePaths = false;
61
Michele Santuari4a338072014-11-05 18:38:55 +010062 for (ConnectPoint egressPoint : intent.egressPoints()) {
Jonathan Hart066244c2015-06-23 09:46:19 -070063 if (egressPoint.deviceId().equals(intent.ingressPoint().deviceId())) {
Luca Preted26ea652017-01-03 15:59:30 -080064 // Do not need to look for paths, since ingress and egress
65 // devices are the same.
66 if (deviceService.isAvailable(egressPoint.deviceId())) {
67 hasPaths = true;
68 } else {
69 missingSomePaths = true;
70 }
Jonathan Hart066244c2015-06-23 09:46:19 -070071 continue;
72 }
73
Michele Santuari4a338072014-11-05 18:38:55 +010074 Path path = getPath(intent, intent.ingressPoint().deviceId(), egressPoint.deviceId());
Luca Pretede10c782017-01-05 17:23:08 -080075
Luca Preted26ea652017-01-03 15:59:30 -080076 if (path != null) {
77 hasPaths = true;
78 links.addAll(path.links());
79 } else {
80 missingSomePaths = true;
81 }
82 }
83
Luca Pretede10c782017-01-05 17:23:08 -080084 // Allocate bandwidth if a bandwidth constraint is set
85 ConnectPoint ingressCP = intent.filteredIngressPoint().connectPoint();
86 List<ConnectPoint> egressCPs =
87 intent.filteredEgressPoints().stream()
88 .map(fcp -> fcp.connectPoint())
89 .collect(Collectors.toList());
90
91 List<ConnectPoint> pathCPs =
92 links.stream()
93 .flatMap(l -> Stream.of(l.src(), l.dst()))
94 .collect(Collectors.toList());
95
96 pathCPs.add(ingressCP);
97 pathCPs.addAll(egressCPs);
98
99 allocateBandwidth(intent, pathCPs);
100
Luca Preted26ea652017-01-03 15:59:30 -0800101 if (!hasPaths) {
102 throw new IntentException("Cannot find any path between ingress and egress points.");
103 } else if (!allowMissingPaths && missingSomePaths) {
104 throw new IntentException("Missing some paths between ingress and egress points.");
Michele Santuari4a338072014-11-05 18:38:55 +0100105 }
106
Ray Milkeyebc5d222015-03-18 15:45:36 -0700107 Intent result = LinkCollectionIntent.builder()
108 .appId(intent.appId())
109 .key(intent.key())
Pier Ventre973bb032016-10-11 08:57:39 -0700110 .selector(intent.selector())
Ray Milkeyebc5d222015-03-18 15:45:36 -0700111 .treatment(intent.treatment())
112 .links(links)
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700113 .filteredIngressPoints(ImmutableSet.of(intent.filteredIngressPoint()))
114 .filteredEgressPoints(intent.filteredEgressPoints())
Ray Milkeyebc5d222015-03-18 15:45:36 -0700115 .priority(intent.priority())
Nicholas Dean126b8af2016-07-18 14:43:13 -0700116 .applyTreatmentOnEgress(true)
Sho SHIMIZU1b46a972015-04-10 17:33:00 -0700117 .constraints(intent.constraints())
Luca Prete670ac5d2017-02-03 15:55:43 -0800118 .resourceGroup(intent.resourceGroup())
Ray Milkeyebc5d222015-03-18 15:45:36 -0700119 .build();
Michele Santuari4a338072014-11-05 18:38:55 +0100120
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700121 return Collections.singletonList(result);
Michele Santuari4a338072014-11-05 18:38:55 +0100122 }
Nicholas Dean126b8af2016-07-18 14:43:13 -0700123}