blob: b4889f47f79f28e156105097ab69961da60dca1f [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 Hartf5e35802014-12-01 20:45:18 -080018import java.util.Arrays;
Pavlin Radoslavov2811c402015-02-25 14:30:17 -080019import java.util.Collections;
Jonathan Hartf5e35802014-12-01 20:45:18 -080020import java.util.HashMap;
21import java.util.List;
22import java.util.Map;
23import java.util.Set;
24
Ray Milkey0742ec92014-10-13 08:39:55 -070025import org.apache.felix.scr.annotations.Activate;
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Deactivate;
28import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.ConnectPoint;
31import org.onosproject.net.DeviceId;
32import org.onosproject.net.Link;
33import org.onosproject.net.Path;
34import org.onosproject.net.intent.Intent;
35import org.onosproject.net.intent.IntentCompiler;
36import org.onosproject.net.intent.IntentExtensionService;
37import org.onosproject.net.intent.LinkCollectionIntent;
38import org.onosproject.net.intent.MultiPointToSinglePointIntent;
39import org.onosproject.net.intent.PointToPointIntent;
Sho SHIMIZU6c28f832015-02-20 16:12:19 -080040import org.onosproject.net.intent.impl.PathNotFoundException;
Brian O'Connorabafb502014-12-02 22:26:20 -080041import org.onosproject.net.resource.LinkResourceAllocations;
42import org.onosproject.net.topology.PathService;
Ray Milkey0742ec92014-10-13 08:39:55 -070043
Pavlin Radoslavov2811c402015-02-25 14:30:17 -080044import com.google.common.collect.ImmutableSet;
Jonathan Hartf5e35802014-12-01 20:45:18 -080045import com.google.common.collect.Sets;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070046
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
53 implements IntentCompiler<MultiPointToSinglePointIntent> {
54
55 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
56 protected IntentExtensionService intentManager;
57
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
59 protected PathService pathService;
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
Brian O'Connorfa81eae2014-10-30 13:20:05 -070072 public List<Intent> compile(MultiPointToSinglePointIntent intent, List<Intent> installable,
73 Set<LinkResourceAllocations> resources) {
Jonathan Hartf5e35802014-12-01 20:45:18 -080074 Map<DeviceId, Link> links = new HashMap<>();
Ray Milkey0742ec92014-10-13 08:39:55 -070075
76 for (ConnectPoint ingressPoint : intent.ingressPoints()) {
77 Path path = getPath(ingressPoint, intent.egressPoint());
Jonathan Hartf5e35802014-12-01 20:45:18 -080078 for (Link link : path.links()) {
79 if (links.containsKey(link.src().deviceId())) {
80 // We've already reached the existing tree with the first
Pavlin Radoslavov2811c402015-02-25 14:30:17 -080081 // part of this path. Add the merging point with different
82 // incoming port, but don't add the remainder of the path
Jonathan Hartf5e35802014-12-01 20:45:18 -080083 // in case it differs from the path we already have.
Pavlin Radoslavov2811c402015-02-25 14:30:17 -080084 links.put(link.src().deviceId(), link);
Jonathan Hartf5e35802014-12-01 20:45:18 -080085 break;
86 }
87
88 links.put(link.src().deviceId(), link);
89 }
Ray Milkey0742ec92014-10-13 08:39:55 -070090 }
91
Thomas Vachuskab97cf282014-10-20 23:31:12 -070092 Intent result = new LinkCollectionIntent(intent.appId(),
93 intent.selector(), intent.treatment(),
Pavlin Radoslavov2811c402015-02-25 14:30:17 -080094 Sets.newHashSet(links.values()),
95 intent.ingressPoints(),
96 ImmutableSet.of(intent.egressPoint()),
97 Collections.emptyList());
Ray Milkey0742ec92014-10-13 08:39:55 -070098 return Arrays.asList(result);
99 }
100
101 /**
102 * Computes a path between two ConnectPoints.
103 *
104 * @param one start of the path
105 * @param two end of the path
106 * @return Path between the two
Brian O'Connorabafb502014-12-02 22:26:20 -0800107 * @throws org.onosproject.net.intent.impl.PathNotFoundException if a path cannot be found
Ray Milkey0742ec92014-10-13 08:39:55 -0700108 */
109 private Path getPath(ConnectPoint one, ConnectPoint two) {
110 Set<Path> paths = pathService.getPaths(one.deviceId(), two.deviceId());
111 if (paths.isEmpty()) {
Sho SHIMIZU877ec2c2015-02-09 12:50:36 -0800112 throw new PathNotFoundException(one.elementId(), two.elementId());
Ray Milkey0742ec92014-10-13 08:39:55 -0700113 }
114 // TODO: let's be more intelligent about this eventually
115 return paths.iterator().next();
116 }
117}