Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 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'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.intent.impl; |
| 17 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 23 | import org.onlab.onos.net.Host; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 24 | import org.onlab.onos.net.Path; |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.flow.TrafficSelector; |
| 26 | import org.onlab.onos.net.host.HostService; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.intent.HostToHostIntent; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.intent.Intent; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.intent.PathIntent; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.resource.LinkResourceAllocations; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 31 | |
| 32 | import java.util.Arrays; |
| 33 | import java.util.List; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 34 | import java.util.Set; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 35 | |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 36 | import static org.onlab.onos.net.flow.DefaultTrafficSelector.builder; |
| 37 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 38 | /** |
| 39 | * A intent compiler for {@link HostToHostIntent}. |
| 40 | */ |
| 41 | @Component(immediate = true) |
| 42 | public class HostToHostIntentCompiler |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 43 | extends ConnectivityIntentCompiler<HostToHostIntent> { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 44 | |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 45 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 46 | protected HostService hostService; |
| 47 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 48 | @Activate |
| 49 | public void activate() { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 50 | intentManager.registerCompiler(HostToHostIntent.class, this); |
| 51 | } |
| 52 | |
| 53 | @Deactivate |
| 54 | public void deactivate() { |
| 55 | intentManager.unregisterCompiler(HostToHostIntent.class); |
| 56 | } |
| 57 | |
| 58 | @Override |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 59 | public List<Intent> compile(HostToHostIntent intent, List<Intent> installable, |
| 60 | Set<LinkResourceAllocations> resources) { |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 61 | Path pathOne = getPath(intent, intent.one(), intent.two()); |
| 62 | Path pathTwo = getPath(intent, intent.two(), intent.one()); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 63 | |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 64 | Host one = hostService.getHost(intent.one()); |
| 65 | Host two = hostService.getHost(intent.two()); |
| 66 | |
| 67 | return Arrays.asList(createPathIntent(pathOne, one, two, intent), |
| 68 | createPathIntent(pathTwo, two, one, intent)); |
| 69 | } |
| 70 | |
| 71 | // Creates a path intent from the specified path and original connectivity intent. |
| 72 | private Intent createPathIntent(Path path, Host src, Host dst, |
| 73 | HostToHostIntent intent) { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 74 | TrafficSelector selector = builder(intent.selector()) |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 75 | .matchEthSrc(src.mac()).matchEthDst(dst.mac()).build(); |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 76 | return new PathIntent(intent.appId(), selector, intent.treatment(), |
| 77 | path, intent.constraints()); |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 80 | } |