blob: 7e6cf3dc60ce5ca624d8cbd771ecd34a88431db3 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
35import org.onosproject.net.intent.PointToPointIntent;
Ray Milkey0742ec92014-10-13 08:39:55 -070036
Jonathan Hart066244c2015-06-23 09:46:19 -070037import java.util.Collections;
38import java.util.HashMap;
39import java.util.List;
40import java.util.Map;
Luca Pretede10c782017-01-05 17:23:08 -080041import java.util.stream.Collectors;
42import java.util.stream.Stream;
Ray Milkey6e0fb302015-04-16 14:44:12 -070043
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070044import static org.onosproject.net.intent.constraint.PartialFailureConstraint.intentAllowsPartialFailure;
45
46
Ray Milkey0742ec92014-10-13 08:39:55 -070047/**
48 * An intent compiler for
Brian O'Connorabafb502014-12-02 22:26:20 -080049 * {@link org.onosproject.net.intent.MultiPointToSinglePointIntent}.
Ray Milkey0742ec92014-10-13 08:39:55 -070050 */
51@Component(immediate = true)
52public class MultiPointToSinglePointIntentCompiler
Luca Preted26ea652017-01-03 15:59:30 -080053 extends ConnectivityIntentCompiler<MultiPointToSinglePointIntent> {
Ray Milkey0742ec92014-10-13 08:39:55 -070054
55 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
56 protected IntentExtensionService intentManager;
57
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070059 protected DeviceService deviceService;
60
Ray Milkey0742ec92014-10-13 08:39:55 -070061 @Activate
62 public void activate() {
Ray Milkey0742ec92014-10-13 08:39:55 -070063 intentManager.registerCompiler(MultiPointToSinglePointIntent.class, this);
64 }
65
66 @Deactivate
67 public void deactivate() {
68 intentManager.unregisterCompiler(PointToPointIntent.class);
69 }
70
71 @Override
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080072 public List<Intent> compile(MultiPointToSinglePointIntent intent, List<Intent> installable) {
Jonathan Hartf5e35802014-12-01 20:45:18 -080073 Map<DeviceId, Link> links = new HashMap<>();
Ray Milkey6e0fb302015-04-16 14:44:12 -070074 ConnectPoint egressPoint = intent.egressPoint();
Ray Milkey0742ec92014-10-13 08:39:55 -070075
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070076 final boolean allowMissingPaths = intentAllowsPartialFailure(intent);
Luca Preted26ea652017-01-03 15:59:30 -080077 boolean hasPaths = false;
78 boolean missingSomePaths = false;
79
Ray Milkey0742ec92014-10-13 08:39:55 -070080 for (ConnectPoint ingressPoint : intent.ingressPoints()) {
Ray Milkey6e0fb302015-04-16 14:44:12 -070081 if (ingressPoint.deviceId().equals(egressPoint.deviceId())) {
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070082 if (deviceService.isAvailable(ingressPoint.deviceId())) {
Luca Preted26ea652017-01-03 15:59:30 -080083 hasPaths = true;
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070084 } else {
Luca Preted26ea652017-01-03 15:59:30 -080085 missingSomePaths = true;
Ray Milkey6e0fb302015-04-16 14:44:12 -070086 }
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070087 continue;
Jonathan Hartf5e35802014-12-01 20:45:18 -080088 }
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070089
Luca Pretede10c782017-01-05 17:23:08 -080090 Path path = getPath(intent, ingressPoint.deviceId(), egressPoint.deviceId());
Luca Preted26ea652017-01-03 15:59:30 -080091
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070092 if (path != null) {
Luca Preted26ea652017-01-03 15:59:30 -080093 hasPaths = true;
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070094
95 for (Link link : path.links()) {
Jonathan Hart4cb39882015-08-12 23:50:55 -040096 if (links.containsKey(link.dst().deviceId())) {
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070097 // We've already reached the existing tree with the first
98 // part of this path. Add the merging point with different
99 // incoming port, but don't add the remainder of the path
100 // in case it differs from the path we already have.
101 links.put(link.src().deviceId(), link);
102 break;
103 }
104 links.put(link.src().deviceId(), link);
105 }
106 } else {
Luca Preted26ea652017-01-03 15:59:30 -0800107 missingSomePaths = true;
Jonathan Hart96c5a4a2015-07-31 14:23:33 -0700108 }
109 }
110
Luca Pretede10c782017-01-05 17:23:08 -0800111 // Allocate bandwidth on existing paths if a bandwidth constraint is set
112 List<ConnectPoint> ingressCPs =
113 intent.filteredIngressPoints().stream()
114 .map(fcp -> fcp.connectPoint())
115 .collect(Collectors.toList());
116 ConnectPoint egressCP = intent.filteredEgressPoint().connectPoint();
117
118 List<ConnectPoint> pathCPs =
119 links.values().stream()
120 .flatMap(l -> Stream.of(l.src(), l.dst()))
121 .collect(Collectors.toList());
122
123 pathCPs.addAll(ingressCPs);
124 pathCPs.add(egressCP);
125
126 allocateBandwidth(intent, pathCPs);
127
Luca Preted26ea652017-01-03 15:59:30 -0800128 if (!hasPaths) {
129 throw new IntentException("Cannot find any path between ingress and egress points.");
130 } else if (!allowMissingPaths && missingSomePaths) {
131 throw new IntentException("Missing some paths between ingress and egress points.");
Ray Milkey0742ec92014-10-13 08:39:55 -0700132 }
133
Ray Milkeyebc5d222015-03-18 15:45:36 -0700134 Intent result = LinkCollectionIntent.builder()
135 .appId(intent.appId())
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700136 .key(intent.key())
Ray Milkeyebc5d222015-03-18 15:45:36 -0700137 .treatment(intent.treatment())
Pier Ventre973bb032016-10-11 08:57:39 -0700138 .selector(intent.selector())
Jonathan Hart066244c2015-06-23 09:46:19 -0700139 .links(Sets.newHashSet(links.values()))
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700140 .filteredIngressPoints(intent.filteredIngressPoints())
141 .filteredEgressPoints(ImmutableSet.of(intent.filteredEgressPoint()))
Ray Milkeyebc5d222015-03-18 15:45:36 -0700142 .priority(intent.priority())
Sho SHIMIZU1b46a972015-04-10 17:33:00 -0700143 .constraints(intent.constraints())
Luca Prete670ac5d2017-02-03 15:55:43 -0800144 .resourceGroup(intent.resourceGroup())
Ray Milkeyebc5d222015-03-18 15:45:36 -0700145 .build();
146
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700147 return Collections.singletonList(result);
Ray Milkey0742ec92014-10-13 08:39:55 -0700148 }
Ray Milkey0742ec92014-10-13 08:39:55 -0700149}