blob: 89c4aba8c7528e470b7aff163629cd3d708df8f7 [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 */
Brian O'Connor66630c82014-10-02 21:08:19 -070016package org.onlab.onos.net.intent.impl;
17
Brian O'Connor66630c82014-10-02 21:08:19 -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;
Thomas Vachuska5dd52f72014-11-28 19:27:45 -080023import org.onlab.onos.net.DefaultLink;
24import org.onlab.onos.net.DefaultPath;
tomf5c9d922014-10-03 15:22:03 -070025import org.onlab.onos.net.Host;
Thomas Vachuska5dd52f72014-11-28 19:27:45 -080026import org.onlab.onos.net.Link;
Brian O'Connor66630c82014-10-02 21:08:19 -070027import org.onlab.onos.net.Path;
tomf5c9d922014-10-03 15:22:03 -070028import org.onlab.onos.net.flow.TrafficSelector;
29import org.onlab.onos.net.host.HostService;
Brian O'Connor66630c82014-10-02 21:08:19 -070030import org.onlab.onos.net.intent.HostToHostIntent;
Brian O'Connor66630c82014-10-02 21:08:19 -070031import org.onlab.onos.net.intent.Intent;
Brian O'Connor66630c82014-10-02 21:08:19 -070032import org.onlab.onos.net.intent.PathIntent;
Thomas Vachuska5dd52f72014-11-28 19:27:45 -080033import org.onlab.onos.net.intent.constraint.AsymmetricPathConstraint;
Brian O'Connorfa81eae2014-10-30 13:20:05 -070034import org.onlab.onos.net.resource.LinkResourceAllocations;
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080035
Thomas Vachuska5dd52f72014-11-28 19:27:45 -080036import java.util.ArrayList;
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080037import java.util.Arrays;
38import java.util.List;
Brian O'Connorfa81eae2014-10-30 13:20:05 -070039import java.util.Set;
Brian O'Connor66630c82014-10-02 21:08:19 -070040
tomf5c9d922014-10-03 15:22:03 -070041import static org.onlab.onos.net.flow.DefaultTrafficSelector.builder;
42
Brian O'Connor66630c82014-10-02 21:08:19 -070043/**
44 * A intent compiler for {@link HostToHostIntent}.
45 */
46@Component(immediate = true)
47public class HostToHostIntentCompiler
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080048 extends ConnectivityIntentCompiler<HostToHostIntent> {
Brian O'Connor66630c82014-10-02 21:08:19 -070049
tomf5c9d922014-10-03 15:22:03 -070050 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
51 protected HostService hostService;
52
Brian O'Connor66630c82014-10-02 21:08:19 -070053 @Activate
54 public void activate() {
Brian O'Connor66630c82014-10-02 21:08:19 -070055 intentManager.registerCompiler(HostToHostIntent.class, this);
56 }
57
58 @Deactivate
59 public void deactivate() {
60 intentManager.unregisterCompiler(HostToHostIntent.class);
61 }
62
63 @Override
Brian O'Connorfa81eae2014-10-30 13:20:05 -070064 public List<Intent> compile(HostToHostIntent intent, List<Intent> installable,
65 Set<LinkResourceAllocations> resources) {
Thomas Vachuska5dd52f72014-11-28 19:27:45 -080066 boolean isAsymmetric = intent.constraints().contains(new AsymmetricPathConstraint());
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080067 Path pathOne = getPath(intent, intent.one(), intent.two());
Thomas Vachuska5dd52f72014-11-28 19:27:45 -080068 Path pathTwo = isAsymmetric ?
69 getPath(intent, intent.two(), intent.one()) : invertPath(pathOne);
Brian O'Connor66630c82014-10-02 21:08:19 -070070
tomf5c9d922014-10-03 15:22:03 -070071 Host one = hostService.getHost(intent.one());
72 Host two = hostService.getHost(intent.two());
73
74 return Arrays.asList(createPathIntent(pathOne, one, two, intent),
75 createPathIntent(pathTwo, two, one, intent));
76 }
77
Thomas Vachuska5dd52f72014-11-28 19:27:45 -080078 // Inverts the specified path. This makes an assumption that each link in
79 // the path has a reverse link available. Under most circumstances, this
80 // assumption will hold.
81 private Path invertPath(Path path) {
82 List<Link> reverseLinks = new ArrayList<>(path.links().size());
83 for (Link link : path.links()) {
84 reverseLinks.add(0, reverseLink(link));
85 }
86 return new DefaultPath(path.providerId(), reverseLinks, path.cost());
87 }
88
89 // Produces a reverse variant of the specified link.
90 private Link reverseLink(Link link) {
91 return new DefaultLink(link.providerId(), link.dst(), link.src(),
92 link.type(), link.state(), link.isDurable());
93 }
94
tomf5c9d922014-10-03 15:22:03 -070095 // Creates a path intent from the specified path and original connectivity intent.
96 private Intent createPathIntent(Path path, Host src, Host dst,
97 HostToHostIntent intent) {
tom85258ee2014-10-07 00:10:02 -070098 TrafficSelector selector = builder(intent.selector())
tomf5c9d922014-10-03 15:22:03 -070099 .matchEthSrc(src.mac()).matchEthDst(dst.mac()).build();
Ray Milkey460f4022014-11-05 15:41:43 -0800100 return new PathIntent(intent.appId(), selector, intent.treatment(),
101 path, intent.constraints());
tomf5c9d922014-10-03 15:22:03 -0700102 }
103
Brian O'Connor66630c82014-10-02 21:08:19 -0700104}