blob: fc5c970b4fd9cba32b573599585eac33d7389805 [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 Milkey0742ec92014-10-13 08:39:55 -070020import 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;
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
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected IntentExtensionService intentManager;
56
57 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070058 protected DeviceService deviceService;
59
Ray Milkey0742ec92014-10-13 08:39:55 -070060 @Activate
61 public void activate() {
Ray Milkey0742ec92014-10-13 08:39:55 -070062 intentManager.registerCompiler(MultiPointToSinglePointIntent.class, this);
63 }
64
65 @Deactivate
66 public void deactivate() {
wu76a3d672017-04-11 14:06:44 +080067 intentManager.unregisterCompiler(MultiPointToSinglePointIntent.class);
Ray Milkey0742ec92014-10-13 08:39:55 -070068 }
69
70 @Override
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080071 public List<Intent> compile(MultiPointToSinglePointIntent intent, List<Intent> installable) {
Jonathan Hartf5e35802014-12-01 20:45:18 -080072 Map<DeviceId, Link> links = new HashMap<>();
Ray Milkey6e0fb302015-04-16 14:44:12 -070073 ConnectPoint egressPoint = intent.egressPoint();
Ray Milkey0742ec92014-10-13 08:39:55 -070074
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070075 final boolean allowMissingPaths = intentAllowsPartialFailure(intent);
Luca Preted26ea652017-01-03 15:59:30 -080076 boolean hasPaths = false;
77 boolean missingSomePaths = false;
78
Ray Milkey0742ec92014-10-13 08:39:55 -070079 for (ConnectPoint ingressPoint : intent.ingressPoints()) {
Ray Milkey6e0fb302015-04-16 14:44:12 -070080 if (ingressPoint.deviceId().equals(egressPoint.deviceId())) {
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070081 if (deviceService.isAvailable(ingressPoint.deviceId())) {
Luca Preted26ea652017-01-03 15:59:30 -080082 hasPaths = true;
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070083 } else {
Luca Preted26ea652017-01-03 15:59:30 -080084 missingSomePaths = true;
Ray Milkey6e0fb302015-04-16 14:44:12 -070085 }
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070086 continue;
Jonathan Hartf5e35802014-12-01 20:45:18 -080087 }
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070088
Luca Pretede10c782017-01-05 17:23:08 -080089 Path path = getPath(intent, ingressPoint.deviceId(), egressPoint.deviceId());
Luca Preted26ea652017-01-03 15:59:30 -080090
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070091 if (path != null) {
Luca Preted26ea652017-01-03 15:59:30 -080092 hasPaths = true;
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070093
94 for (Link link : path.links()) {
Jonathan Hart4cb39882015-08-12 23:50:55 -040095 if (links.containsKey(link.dst().deviceId())) {
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070096 // We've already reached the existing tree with the first
97 // part of this path. Add the merging point with different
98 // incoming port, but don't add the remainder of the path
99 // in case it differs from the path we already have.
100 links.put(link.src().deviceId(), link);
101 break;
102 }
103 links.put(link.src().deviceId(), link);
104 }
105 } else {
Luca Preted26ea652017-01-03 15:59:30 -0800106 missingSomePaths = true;
Jonathan Hart96c5a4a2015-07-31 14:23:33 -0700107 }
108 }
109
Luca Pretede10c782017-01-05 17:23:08 -0800110 // Allocate bandwidth on existing paths if a bandwidth constraint is set
111 List<ConnectPoint> ingressCPs =
112 intent.filteredIngressPoints().stream()
113 .map(fcp -> fcp.connectPoint())
114 .collect(Collectors.toList());
115 ConnectPoint egressCP = intent.filteredEgressPoint().connectPoint();
116
117 List<ConnectPoint> pathCPs =
118 links.values().stream()
119 .flatMap(l -> Stream.of(l.src(), l.dst()))
120 .collect(Collectors.toList());
121
122 pathCPs.addAll(ingressCPs);
123 pathCPs.add(egressCP);
124
125 allocateBandwidth(intent, pathCPs);
126
Luca Preted26ea652017-01-03 15:59:30 -0800127 if (!hasPaths) {
128 throw new IntentException("Cannot find any path between ingress and egress points.");
129 } else if (!allowMissingPaths && missingSomePaths) {
130 throw new IntentException("Missing some paths between ingress and egress points.");
Ray Milkey0742ec92014-10-13 08:39:55 -0700131 }
132
Ray Milkeyebc5d222015-03-18 15:45:36 -0700133 Intent result = LinkCollectionIntent.builder()
134 .appId(intent.appId())
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700135 .key(intent.key())
Ray Milkeyebc5d222015-03-18 15:45:36 -0700136 .treatment(intent.treatment())
Pier Ventre973bb032016-10-11 08:57:39 -0700137 .selector(intent.selector())
Jonathan Hart066244c2015-06-23 09:46:19 -0700138 .links(Sets.newHashSet(links.values()))
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700139 .filteredIngressPoints(intent.filteredIngressPoints())
140 .filteredEgressPoints(ImmutableSet.of(intent.filteredEgressPoint()))
Ray Milkeyebc5d222015-03-18 15:45:36 -0700141 .priority(intent.priority())
Sho SHIMIZU1b46a972015-04-10 17:33:00 -0700142 .constraints(intent.constraints())
Luca Prete670ac5d2017-02-03 15:55:43 -0800143 .resourceGroup(intent.resourceGroup())
Ray Milkeyebc5d222015-03-18 15:45:36 -0700144 .build();
145
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700146 return Collections.singletonList(result);
Ray Milkey0742ec92014-10-13 08:39:55 -0700147 }
Ray Milkey0742ec92014-10-13 08:39:55 -0700148}