blob: 7caa4e13e07b23da1cb210533a48a10c79f35d62 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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;
Ray Milkey0742ec92014-10-13 08:39:55 -070017
Jonathan Hart066244c2015-06-23 09:46:19 -070018import com.google.common.collect.ImmutableSet;
19import com.google.common.collect.Sets;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.net.ConnectPoint;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.Link;
23import org.onosproject.net.Path;
24import org.onosproject.net.intent.Intent;
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070025import org.onosproject.net.intent.IntentException;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.net.intent.LinkCollectionIntent;
27import org.onosproject.net.intent.MultiPointToSinglePointIntent;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070028import org.osgi.service.component.annotations.Activate;
29import org.osgi.service.component.annotations.Component;
30import org.osgi.service.component.annotations.Deactivate;
Ray Milkey0742ec92014-10-13 08:39:55 -070031
Jonathan Hart066244c2015-06-23 09:46:19 -070032import java.util.Collections;
33import java.util.HashMap;
34import java.util.List;
35import java.util.Map;
Luca Pretede10c782017-01-05 17:23:08 -080036import java.util.stream.Collectors;
37import java.util.stream.Stream;
Ray Milkey6e0fb302015-04-16 14:44:12 -070038
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070039import static org.onosproject.net.intent.constraint.PartialFailureConstraint.intentAllowsPartialFailure;
40
41
Ray Milkey0742ec92014-10-13 08:39:55 -070042/**
43 * An intent compiler for
Brian O'Connorabafb502014-12-02 22:26:20 -080044 * {@link org.onosproject.net.intent.MultiPointToSinglePointIntent}.
Ray Milkey0742ec92014-10-13 08:39:55 -070045 */
46@Component(immediate = true)
47public class MultiPointToSinglePointIntentCompiler
Luca Preted26ea652017-01-03 15:59:30 -080048 extends ConnectivityIntentCompiler<MultiPointToSinglePointIntent> {
Ray Milkey0742ec92014-10-13 08:39:55 -070049
Ray Milkey0742ec92014-10-13 08:39:55 -070050 @Activate
51 public void activate() {
Ray Milkey0742ec92014-10-13 08:39:55 -070052 intentManager.registerCompiler(MultiPointToSinglePointIntent.class, this);
53 }
54
55 @Deactivate
56 public void deactivate() {
wu76a3d672017-04-11 14:06:44 +080057 intentManager.unregisterCompiler(MultiPointToSinglePointIntent.class);
Ray Milkey0742ec92014-10-13 08:39:55 -070058 }
59
60 @Override
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080061 public List<Intent> compile(MultiPointToSinglePointIntent intent, List<Intent> installable) {
Jonathan Hartf5e35802014-12-01 20:45:18 -080062 Map<DeviceId, Link> links = new HashMap<>();
Ray Milkey6e0fb302015-04-16 14:44:12 -070063 ConnectPoint egressPoint = intent.egressPoint();
Ray Milkey0742ec92014-10-13 08:39:55 -070064
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070065 final boolean allowMissingPaths = intentAllowsPartialFailure(intent);
Luca Preted26ea652017-01-03 15:59:30 -080066 boolean hasPaths = false;
67 boolean missingSomePaths = false;
68
Ray Milkey0742ec92014-10-13 08:39:55 -070069 for (ConnectPoint ingressPoint : intent.ingressPoints()) {
Ray Milkey6e0fb302015-04-16 14:44:12 -070070 if (ingressPoint.deviceId().equals(egressPoint.deviceId())) {
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070071 if (deviceService.isAvailable(ingressPoint.deviceId())) {
Luca Preted26ea652017-01-03 15:59:30 -080072 hasPaths = true;
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070073 } else {
Luca Preted26ea652017-01-03 15:59:30 -080074 missingSomePaths = true;
Ray Milkey6e0fb302015-04-16 14:44:12 -070075 }
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070076 continue;
Jonathan Hartf5e35802014-12-01 20:45:18 -080077 }
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070078
Luca Pretede10c782017-01-05 17:23:08 -080079 Path path = getPath(intent, ingressPoint.deviceId(), egressPoint.deviceId());
Luca Preted26ea652017-01-03 15:59:30 -080080
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070081 if (path != null) {
Luca Preted26ea652017-01-03 15:59:30 -080082 hasPaths = true;
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070083
84 for (Link link : path.links()) {
Jonathan Hart4cb39882015-08-12 23:50:55 -040085 if (links.containsKey(link.dst().deviceId())) {
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070086 // We've already reached the existing tree with the first
87 // part of this path. Add the merging point with different
88 // incoming port, but don't add the remainder of the path
89 // in case it differs from the path we already have.
90 links.put(link.src().deviceId(), link);
91 break;
92 }
93 links.put(link.src().deviceId(), link);
94 }
95 } else {
Luca Preted26ea652017-01-03 15:59:30 -080096 missingSomePaths = true;
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070097 }
98 }
99
Luca Pretede10c782017-01-05 17:23:08 -0800100 // Allocate bandwidth on existing paths if a bandwidth constraint is set
101 List<ConnectPoint> ingressCPs =
102 intent.filteredIngressPoints().stream()
103 .map(fcp -> fcp.connectPoint())
104 .collect(Collectors.toList());
105 ConnectPoint egressCP = intent.filteredEgressPoint().connectPoint();
106
107 List<ConnectPoint> pathCPs =
108 links.values().stream()
109 .flatMap(l -> Stream.of(l.src(), l.dst()))
110 .collect(Collectors.toList());
111
112 pathCPs.addAll(ingressCPs);
113 pathCPs.add(egressCP);
114
115 allocateBandwidth(intent, pathCPs);
116
Luca Preted26ea652017-01-03 15:59:30 -0800117 if (!hasPaths) {
118 throw new IntentException("Cannot find any path between ingress and egress points.");
119 } else if (!allowMissingPaths && missingSomePaths) {
120 throw new IntentException("Missing some paths between ingress and egress points.");
Ray Milkey0742ec92014-10-13 08:39:55 -0700121 }
122
Ray Milkeyebc5d222015-03-18 15:45:36 -0700123 Intent result = LinkCollectionIntent.builder()
124 .appId(intent.appId())
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700125 .key(intent.key())
Ray Milkeyebc5d222015-03-18 15:45:36 -0700126 .treatment(intent.treatment())
Pier Ventre973bb032016-10-11 08:57:39 -0700127 .selector(intent.selector())
Jonathan Hart066244c2015-06-23 09:46:19 -0700128 .links(Sets.newHashSet(links.values()))
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700129 .filteredIngressPoints(intent.filteredIngressPoints())
130 .filteredEgressPoints(ImmutableSet.of(intent.filteredEgressPoint()))
Ray Milkeyebc5d222015-03-18 15:45:36 -0700131 .priority(intent.priority())
Sho SHIMIZU1b46a972015-04-10 17:33:00 -0700132 .constraints(intent.constraints())
Luca Prete670ac5d2017-02-03 15:55:43 -0800133 .resourceGroup(intent.resourceGroup())
Ray Milkeyebc5d222015-03-18 15:45:36 -0700134 .build();
135
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700136 return Collections.singletonList(result);
Ray Milkey0742ec92014-10-13 08:39:55 -0700137 }
Ray Milkey0742ec92014-10-13 08:39:55 -0700138}