blob: 1196540733e8b6f209afe455346aaf43adf16443 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
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 Hart938008a2015-05-14 20:08:47 -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;
29import org.onosproject.net.intent.Intent;
30import org.onosproject.net.intent.IntentCompiler;
31import org.onosproject.net.intent.IntentExtensionService;
32import org.onosproject.net.intent.LinkCollectionIntent;
33import org.onosproject.net.intent.MultiPointToSinglePointIntent;
34import org.onosproject.net.intent.PointToPointIntent;
Sho SHIMIZU6c28f832015-02-20 16:12:19 -080035import org.onosproject.net.intent.impl.PathNotFoundException;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.resource.LinkResourceAllocations;
37import org.onosproject.net.topology.PathService;
Ray Milkey0742ec92014-10-13 08:39:55 -070038
Jonathan Hart938008a2015-05-14 20:08:47 -070039import java.util.Arrays;
40import java.util.Collections;
41import java.util.HashMap;
42import java.util.List;
43import java.util.Map;
44import java.util.Set;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070045
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
52 implements IntentCompiler<MultiPointToSinglePointIntent> {
53
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected IntentExtensionService intentManager;
56
57 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58 protected PathService pathService;
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() {
67 intentManager.unregisterCompiler(PointToPointIntent.class);
68 }
69
70 @Override
Brian O'Connorfa81eae2014-10-30 13:20:05 -070071 public List<Intent> compile(MultiPointToSinglePointIntent intent, List<Intent> installable,
72 Set<LinkResourceAllocations> resources) {
Jonathan Hartf5e35802014-12-01 20:45:18 -080073 Map<DeviceId, Link> links = new HashMap<>();
Ray Milkey0742ec92014-10-13 08:39:55 -070074
75 for (ConnectPoint ingressPoint : intent.ingressPoints()) {
Jonathan Hart938008a2015-05-14 20:08:47 -070076 if (ingressPoint.deviceId().equals(intent.egressPoint().deviceId())) {
77 continue;
78 }
Ray Milkey0742ec92014-10-13 08:39:55 -070079 Path path = getPath(ingressPoint, intent.egressPoint());
Jonathan Hartf5e35802014-12-01 20:45:18 -080080 for (Link link : path.links()) {
81 if (links.containsKey(link.src().deviceId())) {
82 // We've already reached the existing tree with the first
Pavlin Radoslavov2811c402015-02-25 14:30:17 -080083 // part of this path. Add the merging point with different
84 // incoming port, but don't add the remainder of the path
Jonathan Hartf5e35802014-12-01 20:45:18 -080085 // in case it differs from the path we already have.
Pavlin Radoslavov2811c402015-02-25 14:30:17 -080086 links.put(link.src().deviceId(), link);
Jonathan Hartf5e35802014-12-01 20:45:18 -080087 break;
88 }
89
90 links.put(link.src().deviceId(), link);
91 }
Ray Milkey0742ec92014-10-13 08:39:55 -070092 }
93
Ray Milkeyc24cde32015-03-10 18:20:18 -070094 Set<ConnectPoint> egress = ImmutableSet.of(intent.egressPoint());
Thomas Vachuskab97cf282014-10-20 23:31:12 -070095 Intent result = new LinkCollectionIntent(intent.appId(),
96 intent.selector(), intent.treatment(),
Pavlin Radoslavov2811c402015-02-25 14:30:17 -080097 Sets.newHashSet(links.values()),
98 intent.ingressPoints(),
99 ImmutableSet.of(intent.egressPoint()),
Ray Milkeyc24cde32015-03-10 18:20:18 -0700100 Collections.emptyList(),
101 intent.priority());
Ray Milkey0742ec92014-10-13 08:39:55 -0700102 return Arrays.asList(result);
103 }
104
105 /**
106 * Computes a path between two ConnectPoints.
107 *
108 * @param one start of the path
109 * @param two end of the path
110 * @return Path between the two
Brian O'Connorabafb502014-12-02 22:26:20 -0800111 * @throws org.onosproject.net.intent.impl.PathNotFoundException if a path cannot be found
Ray Milkey0742ec92014-10-13 08:39:55 -0700112 */
113 private Path getPath(ConnectPoint one, ConnectPoint two) {
114 Set<Path> paths = pathService.getPaths(one.deviceId(), two.deviceId());
115 if (paths.isEmpty()) {
Sho SHIMIZU877ec2c2015-02-09 12:50:36 -0800116 throw new PathNotFoundException(one.elementId(), two.elementId());
Ray Milkey0742ec92014-10-13 08:39:55 -0700117 }
118 // TODO: let's be more intelligent about this eventually
119 return paths.iterator().next();
120 }
121}