blob: b01500a7844dc1c8f3aa1f4679cf71db120b3fd8 [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 */
Ray Milkey0742ec92014-10-13 08:39:55 -070016package org.onlab.onos.net.intent.impl;
17
Ray Milkey0742ec92014-10-13 08:39:55 -070018import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
23import org.onlab.onos.net.ConnectPoint;
24import org.onlab.onos.net.Link;
25import org.onlab.onos.net.Path;
Ray Milkey0742ec92014-10-13 08:39:55 -070026import org.onlab.onos.net.intent.Intent;
27import org.onlab.onos.net.intent.IntentCompiler;
28import org.onlab.onos.net.intent.IntentExtensionService;
Ray Milkey0742ec92014-10-13 08:39:55 -070029import org.onlab.onos.net.intent.LinkCollectionIntent;
30import org.onlab.onos.net.intent.MultiPointToSinglePointIntent;
31import org.onlab.onos.net.intent.PointToPointIntent;
32import org.onlab.onos.net.topology.PathService;
33
Thomas Vachuskab97cf282014-10-20 23:31:12 -070034import java.util.Arrays;
35import java.util.HashSet;
36import java.util.List;
37import java.util.Set;
38
Ray Milkey0742ec92014-10-13 08:39:55 -070039/**
40 * An intent compiler for
41 * {@link org.onlab.onos.net.intent.MultiPointToSinglePointIntent}.
42 */
43@Component(immediate = true)
44public class MultiPointToSinglePointIntentCompiler
45 implements IntentCompiler<MultiPointToSinglePointIntent> {
46
47 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
48 protected IntentExtensionService intentManager;
49
50 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
51 protected PathService pathService;
52
Ray Milkey0742ec92014-10-13 08:39:55 -070053 @Activate
54 public void activate() {
Ray Milkey0742ec92014-10-13 08:39:55 -070055 intentManager.registerCompiler(MultiPointToSinglePointIntent.class, this);
56 }
57
58 @Deactivate
59 public void deactivate() {
60 intentManager.unregisterCompiler(PointToPointIntent.class);
61 }
62
63 @Override
64 public List<Intent> compile(MultiPointToSinglePointIntent intent) {
65 Set<Link> links = new HashSet<>();
66
67 for (ConnectPoint ingressPoint : intent.ingressPoints()) {
68 Path path = getPath(ingressPoint, intent.egressPoint());
69 links.addAll(path.links());
70 }
71
Thomas Vachuskab97cf282014-10-20 23:31:12 -070072 Intent result = new LinkCollectionIntent(intent.appId(),
73 intent.selector(), intent.treatment(),
74 links, intent.egressPoint());
Ray Milkey0742ec92014-10-13 08:39:55 -070075 return Arrays.asList(result);
76 }
77
78 /**
79 * Computes a path between two ConnectPoints.
80 *
81 * @param one start of the path
82 * @param two end of the path
83 * @return Path between the two
84 * @throws org.onlab.onos.net.intent.impl.PathNotFoundException if a path cannot be found
85 */
86 private Path getPath(ConnectPoint one, ConnectPoint two) {
87 Set<Path> paths = pathService.getPaths(one.deviceId(), two.deviceId());
88 if (paths.isEmpty()) {
89 throw new PathNotFoundException("No path from " + one + " to " + two);
90 }
91 // TODO: let's be more intelligent about this eventually
92 return paths.iterator().next();
93 }
94}