blob: a9bee07c7efdb58ee14f2d2cd6a58eb23317a52d [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present 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;
Brian O'Connor66630c82014-10-02 21:08:19 -070017
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;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.net.DefaultLink;
24import org.onosproject.net.DefaultPath;
25import org.onosproject.net.Host;
26import org.onosproject.net.Link;
27import org.onosproject.net.Path;
28import org.onosproject.net.flow.TrafficSelector;
29import org.onosproject.net.host.HostService;
30import org.onosproject.net.intent.HostToHostIntent;
31import org.onosproject.net.intent.Intent;
32import org.onosproject.net.intent.PathIntent;
33import org.onosproject.net.intent.constraint.AsymmetricPathConstraint;
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080034
Thomas Vachuska5dd52f72014-11-28 19:27:45 -080035import java.util.ArrayList;
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080036import java.util.Arrays;
37import java.util.List;
Brian O'Connor66630c82014-10-02 21:08:19 -070038
Brian O'Connorabafb502014-12-02 22:26:20 -080039import static org.onosproject.net.flow.DefaultTrafficSelector.builder;
tomf5c9d922014-10-03 15:22:03 -070040
Brian O'Connor66630c82014-10-02 21:08:19 -070041/**
42 * A intent compiler for {@link HostToHostIntent}.
43 */
44@Component(immediate = true)
45public class HostToHostIntentCompiler
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080046 extends ConnectivityIntentCompiler<HostToHostIntent> {
Brian O'Connor66630c82014-10-02 21:08:19 -070047
tomf5c9d922014-10-03 15:22:03 -070048 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
49 protected HostService hostService;
50
Brian O'Connor66630c82014-10-02 21:08:19 -070051 @Activate
52 public void activate() {
Brian O'Connor66630c82014-10-02 21:08:19 -070053 intentManager.registerCompiler(HostToHostIntent.class, this);
54 }
55
56 @Deactivate
57 public void deactivate() {
58 intentManager.unregisterCompiler(HostToHostIntent.class);
59 }
60
61 @Override
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080062 public List<Intent> compile(HostToHostIntent intent, List<Intent> installable) {
Thomas Vachuska5dd52f72014-11-28 19:27:45 -080063 boolean isAsymmetric = intent.constraints().contains(new AsymmetricPathConstraint());
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080064 Path pathOne = getPath(intent, intent.one(), intent.two());
Thomas Vachuska5dd52f72014-11-28 19:27:45 -080065 Path pathTwo = isAsymmetric ?
66 getPath(intent, intent.two(), intent.one()) : invertPath(pathOne);
Brian O'Connor66630c82014-10-02 21:08:19 -070067
tomf5c9d922014-10-03 15:22:03 -070068 Host one = hostService.getHost(intent.one());
69 Host two = hostService.getHost(intent.two());
70
71 return Arrays.asList(createPathIntent(pathOne, one, two, intent),
72 createPathIntent(pathTwo, two, one, intent));
73 }
74
Thomas Vachuska5dd52f72014-11-28 19:27:45 -080075 // Inverts the specified path. This makes an assumption that each link in
76 // the path has a reverse link available. Under most circumstances, this
77 // assumption will hold.
78 private Path invertPath(Path path) {
79 List<Link> reverseLinks = new ArrayList<>(path.links().size());
80 for (Link link : path.links()) {
81 reverseLinks.add(0, reverseLink(link));
82 }
83 return new DefaultPath(path.providerId(), reverseLinks, path.cost());
84 }
85
86 // Produces a reverse variant of the specified link.
87 private Link reverseLink(Link link) {
Ray Milkey2693bda2016-01-22 16:08:14 -080088 return DefaultLink.builder().providerId(link.providerId())
89 .src(link.dst())
90 .dst(link.src())
91 .type(link.type())
92 .state(link.state())
93 .isExpected(link.isExpected())
94 .build();
Thomas Vachuska5dd52f72014-11-28 19:27:45 -080095 }
96
tomf5c9d922014-10-03 15:22:03 -070097 // Creates a path intent from the specified path and original connectivity intent.
98 private Intent createPathIntent(Path path, Host src, Host dst,
99 HostToHostIntent intent) {
tom85258ee2014-10-07 00:10:02 -0700100 TrafficSelector selector = builder(intent.selector())
tomf5c9d922014-10-03 15:22:03 -0700101 .matchEthSrc(src.mac()).matchEthDst(dst.mac()).build();
Ray Milkeyebc5d222015-03-18 15:45:36 -0700102 return PathIntent.builder()
103 .appId(intent.appId())
104 .selector(selector)
105 .treatment(intent.treatment())
106 .path(path)
107 .constraints(intent.constraints())
108 .priority(intent.priority())
109 .build();
tomf5c9d922014-10-03 15:22:03 -0700110 }
111
Brian O'Connor66630c82014-10-02 21:08:19 -0700112}