blob: 710f6e191fac47af5093cc6366cc6c6cda9537c7 [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;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070020import org.osgi.service.component.annotations.Activate;
21import org.osgi.service.component.annotations.Component;
22import org.osgi.service.component.annotations.Deactivate;
23import org.osgi.service.component.annotations.Reference;
24import org.osgi.service.component.annotations.ReferenceCardinality;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.ConnectPoint;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.Link;
28import org.onosproject.net.Path;
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070029import org.onosproject.net.device.DeviceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.intent.Intent;
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070031import org.onosproject.net.intent.IntentException;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.intent.IntentExtensionService;
33import org.onosproject.net.intent.LinkCollectionIntent;
34import org.onosproject.net.intent.MultiPointToSinglePointIntent;
Ray Milkey0742ec92014-10-13 08:39:55 -070035
Jonathan Hart066244c2015-06-23 09:46:19 -070036import java.util.Collections;
37import java.util.HashMap;
38import java.util.List;
39import java.util.Map;
Luca Pretede10c782017-01-05 17:23:08 -080040import java.util.stream.Collectors;
41import java.util.stream.Stream;
Ray Milkey6e0fb302015-04-16 14:44:12 -070042
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070043import static org.onosproject.net.intent.constraint.PartialFailureConstraint.intentAllowsPartialFailure;
44
45
Ray Milkey0742ec92014-10-13 08:39:55 -070046/**
47 * An intent compiler for
Brian O'Connorabafb502014-12-02 22:26:20 -080048 * {@link org.onosproject.net.intent.MultiPointToSinglePointIntent}.
Ray Milkey0742ec92014-10-13 08:39:55 -070049 */
50@Component(immediate = true)
51public class MultiPointToSinglePointIntentCompiler
Luca Preted26ea652017-01-03 15:59:30 -080052 extends ConnectivityIntentCompiler<MultiPointToSinglePointIntent> {
Ray Milkey0742ec92014-10-13 08:39:55 -070053
Ray Milkey0742ec92014-10-13 08:39:55 -070054 @Activate
55 public void activate() {
Ray Milkey0742ec92014-10-13 08:39:55 -070056 intentManager.registerCompiler(MultiPointToSinglePointIntent.class, this);
57 }
58
59 @Deactivate
60 public void deactivate() {
wu76a3d672017-04-11 14:06:44 +080061 intentManager.unregisterCompiler(MultiPointToSinglePointIntent.class);
Ray Milkey0742ec92014-10-13 08:39:55 -070062 }
63
64 @Override
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080065 public List<Intent> compile(MultiPointToSinglePointIntent intent, List<Intent> installable) {
Jonathan Hartf5e35802014-12-01 20:45:18 -080066 Map<DeviceId, Link> links = new HashMap<>();
Ray Milkey6e0fb302015-04-16 14:44:12 -070067 ConnectPoint egressPoint = intent.egressPoint();
Ray Milkey0742ec92014-10-13 08:39:55 -070068
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070069 final boolean allowMissingPaths = intentAllowsPartialFailure(intent);
Luca Preted26ea652017-01-03 15:59:30 -080070 boolean hasPaths = false;
71 boolean missingSomePaths = false;
72
Ray Milkey0742ec92014-10-13 08:39:55 -070073 for (ConnectPoint ingressPoint : intent.ingressPoints()) {
Ray Milkey6e0fb302015-04-16 14:44:12 -070074 if (ingressPoint.deviceId().equals(egressPoint.deviceId())) {
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070075 if (deviceService.isAvailable(ingressPoint.deviceId())) {
Luca Preted26ea652017-01-03 15:59:30 -080076 hasPaths = true;
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070077 } else {
Luca Preted26ea652017-01-03 15:59:30 -080078 missingSomePaths = true;
Ray Milkey6e0fb302015-04-16 14:44:12 -070079 }
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070080 continue;
Jonathan Hartf5e35802014-12-01 20:45:18 -080081 }
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070082
Luca Pretede10c782017-01-05 17:23:08 -080083 Path path = getPath(intent, ingressPoint.deviceId(), egressPoint.deviceId());
Luca Preted26ea652017-01-03 15:59:30 -080084
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070085 if (path != null) {
Luca Preted26ea652017-01-03 15:59:30 -080086 hasPaths = true;
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070087
88 for (Link link : path.links()) {
Jonathan Hart4cb39882015-08-12 23:50:55 -040089 if (links.containsKey(link.dst().deviceId())) {
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070090 // We've already reached the existing tree with the first
91 // part of this path. Add the merging point with different
92 // incoming port, but don't add the remainder of the path
93 // in case it differs from the path we already have.
94 links.put(link.src().deviceId(), link);
95 break;
96 }
97 links.put(link.src().deviceId(), link);
98 }
99 } else {
Luca Preted26ea652017-01-03 15:59:30 -0800100 missingSomePaths = true;
Jonathan Hart96c5a4a2015-07-31 14:23:33 -0700101 }
102 }
103
Luca Pretede10c782017-01-05 17:23:08 -0800104 // Allocate bandwidth on existing paths if a bandwidth constraint is set
105 List<ConnectPoint> ingressCPs =
106 intent.filteredIngressPoints().stream()
107 .map(fcp -> fcp.connectPoint())
108 .collect(Collectors.toList());
109 ConnectPoint egressCP = intent.filteredEgressPoint().connectPoint();
110
111 List<ConnectPoint> pathCPs =
112 links.values().stream()
113 .flatMap(l -> Stream.of(l.src(), l.dst()))
114 .collect(Collectors.toList());
115
116 pathCPs.addAll(ingressCPs);
117 pathCPs.add(egressCP);
118
119 allocateBandwidth(intent, pathCPs);
120
Luca Preted26ea652017-01-03 15:59:30 -0800121 if (!hasPaths) {
122 throw new IntentException("Cannot find any path between ingress and egress points.");
123 } else if (!allowMissingPaths && missingSomePaths) {
124 throw new IntentException("Missing some paths between ingress and egress points.");
Ray Milkey0742ec92014-10-13 08:39:55 -0700125 }
126
Ray Milkeyebc5d222015-03-18 15:45:36 -0700127 Intent result = LinkCollectionIntent.builder()
128 .appId(intent.appId())
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700129 .key(intent.key())
Ray Milkeyebc5d222015-03-18 15:45:36 -0700130 .treatment(intent.treatment())
Pier Ventre973bb032016-10-11 08:57:39 -0700131 .selector(intent.selector())
Jonathan Hart066244c2015-06-23 09:46:19 -0700132 .links(Sets.newHashSet(links.values()))
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700133 .filteredIngressPoints(intent.filteredIngressPoints())
134 .filteredEgressPoints(ImmutableSet.of(intent.filteredEgressPoint()))
Ray Milkeyebc5d222015-03-18 15:45:36 -0700135 .priority(intent.priority())
Sho SHIMIZU1b46a972015-04-10 17:33:00 -0700136 .constraints(intent.constraints())
Luca Prete670ac5d2017-02-03 15:55:43 -0800137 .resourceGroup(intent.resourceGroup())
Ray Milkeyebc5d222015-03-18 15:45:36 -0700138 .build();
139
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700140 return Collections.singletonList(result);
Ray Milkey0742ec92014-10-13 08:39:55 -0700141 }
Ray Milkey0742ec92014-10-13 08:39:55 -0700142}