blob: 5c158e04a6c97e5aec21a3ae4f1f8c81f171f8f6 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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;
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'Connor6de2e202015-05-21 14:30:41 -070036import org.onosproject.net.resource.link.LinkResourceAllocations;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import org.onosproject.net.topology.PathService;
Ray Milkey0742ec92014-10-13 08:39:55 -070038
Jonathan Hart066244c2015-06-23 09:46:19 -070039import java.util.Collections;
40import java.util.HashMap;
41import java.util.List;
42import java.util.Map;
43import java.util.Set;
Ray Milkey6e0fb302015-04-16 14:44:12 -070044
Ray Milkey0742ec92014-10-13 08:39:55 -070045/**
46 * An intent compiler for
Brian O'Connorabafb502014-12-02 22:26:20 -080047 * {@link org.onosproject.net.intent.MultiPointToSinglePointIntent}.
Ray Milkey0742ec92014-10-13 08:39:55 -070048 */
49@Component(immediate = true)
50public class MultiPointToSinglePointIntentCompiler
51 implements IntentCompiler<MultiPointToSinglePointIntent> {
52
53 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
54 protected IntentExtensionService intentManager;
55
56 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
57 protected PathService pathService;
58
Ray Milkey0742ec92014-10-13 08:39:55 -070059 @Activate
60 public void activate() {
Ray Milkey0742ec92014-10-13 08:39:55 -070061 intentManager.registerCompiler(MultiPointToSinglePointIntent.class, this);
62 }
63
64 @Deactivate
65 public void deactivate() {
66 intentManager.unregisterCompiler(PointToPointIntent.class);
67 }
68
69 @Override
Brian O'Connorfa81eae2014-10-30 13:20:05 -070070 public List<Intent> compile(MultiPointToSinglePointIntent intent, List<Intent> installable,
71 Set<LinkResourceAllocations> resources) {
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
75 for (ConnectPoint ingressPoint : intent.ingressPoints()) {
Ray Milkey6e0fb302015-04-16 14:44:12 -070076 if (ingressPoint.deviceId().equals(egressPoint.deviceId())) {
Jonathan Hart066244c2015-06-23 09:46:19 -070077 continue;
78 }
79 Path path = getPath(ingressPoint, intent.egressPoint());
80 for (Link link : path.links()) {
81 if (links.containsKey(link.src().deviceId())) {
82 // We've already reached the existing tree with the first
83 // part of this path. Add the merging point with different
84 // incoming port, but don't add the remainder of the path
85 // in case it differs from the path we already have.
Ray Milkey6e0fb302015-04-16 14:44:12 -070086 links.put(link.src().deviceId(), link);
Jonathan Hart066244c2015-06-23 09:46:19 -070087 break;
Ray Milkey6e0fb302015-04-16 14:44:12 -070088 }
Jonathan Hart066244c2015-06-23 09:46:19 -070089
90 links.put(link.src().deviceId(), link);
Jonathan Hartf5e35802014-12-01 20:45:18 -080091 }
Ray Milkey0742ec92014-10-13 08:39:55 -070092 }
93
Ray Milkeyebc5d222015-03-18 15:45:36 -070094 Intent result = LinkCollectionIntent.builder()
95 .appId(intent.appId())
96 .selector(intent.selector())
97 .treatment(intent.treatment())
Jonathan Hart066244c2015-06-23 09:46:19 -070098 .links(Sets.newHashSet(links.values()))
Ray Milkeyebc5d222015-03-18 15:45:36 -070099 .ingressPoints(intent.ingressPoints())
100 .egressPoints(ImmutableSet.of(intent.egressPoint()))
101 .priority(intent.priority())
Sho SHIMIZU1b46a972015-04-10 17:33:00 -0700102 .constraints(intent.constraints())
Ray Milkeyebc5d222015-03-18 15:45:36 -0700103 .build();
104
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700105 return Collections.singletonList(result);
Ray Milkey0742ec92014-10-13 08:39:55 -0700106 }
107
108 /**
109 * Computes a path between two ConnectPoints.
110 *
111 * @param one start of the path
112 * @param two end of the path
113 * @return Path between the two
Brian O'Connorabafb502014-12-02 22:26:20 -0800114 * @throws org.onosproject.net.intent.impl.PathNotFoundException if a path cannot be found
Ray Milkey0742ec92014-10-13 08:39:55 -0700115 */
116 private Path getPath(ConnectPoint one, ConnectPoint two) {
117 Set<Path> paths = pathService.getPaths(one.deviceId(), two.deviceId());
118 if (paths.isEmpty()) {
Sho SHIMIZU877ec2c2015-02-09 12:50:36 -0800119 throw new PathNotFoundException(one.elementId(), two.elementId());
Ray Milkey0742ec92014-10-13 08:39:55 -0700120 }
121 // TODO: let's be more intelligent about this eventually
122 return paths.iterator().next();
123 }
124}