Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.intent.impl; |
| 2 | |
| 3 | import java.util.Arrays; |
| 4 | import java.util.List; |
| 5 | import java.util.Set; |
| 6 | |
| 7 | import org.apache.felix.scr.annotations.Activate; |
| 8 | import org.apache.felix.scr.annotations.Component; |
| 9 | import org.apache.felix.scr.annotations.Deactivate; |
| 10 | import org.apache.felix.scr.annotations.Reference; |
| 11 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 12 | import org.onlab.onos.net.ConnectPoint; |
| 13 | import org.onlab.onos.net.Path; |
| 14 | import org.onlab.onos.net.PortNumber; |
| 15 | import org.onlab.onos.net.intent.HostToHostIntent; |
| 16 | import org.onlab.onos.net.intent.IdGenerator; |
| 17 | import org.onlab.onos.net.intent.Intent; |
| 18 | import org.onlab.onos.net.intent.IntentCompiler; |
| 19 | import org.onlab.onos.net.intent.IntentExtensionService; |
| 20 | import org.onlab.onos.net.intent.IntentId; |
| 21 | import org.onlab.onos.net.intent.PathIntent; |
| 22 | import org.onlab.onos.net.topology.PathService; |
| 23 | |
| 24 | /** |
| 25 | * A intent compiler for {@link HostToHostIntent}. |
| 26 | */ |
| 27 | @Component(immediate = true) |
| 28 | public class HostToHostIntentCompiler |
| 29 | implements IntentCompiler<HostToHostIntent> { |
| 30 | |
| 31 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 32 | protected IntentExtensionService intentManager; |
| 33 | |
| 34 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 35 | protected PathService pathService; |
| 36 | |
| 37 | private IdGenerator<IntentId> intentIdGenerator; |
| 38 | |
| 39 | @Activate |
| 40 | public void activate() { |
| 41 | IdBlockAllocator idBlockAllocator = new DummyIdBlockAllocator(); |
| 42 | intentIdGenerator = new IdBlockAllocatorBasedIntentIdGenerator(idBlockAllocator); |
| 43 | intentManager.registerCompiler(HostToHostIntent.class, this); |
| 44 | } |
| 45 | |
| 46 | @Deactivate |
| 47 | public void deactivate() { |
| 48 | intentManager.unregisterCompiler(HostToHostIntent.class); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public List<Intent> compile(HostToHostIntent intent) { |
| 53 | Set<Path> paths = pathService.getPaths(intent.getSrc(), intent.getDst()); |
| 54 | if (paths.isEmpty()) { |
| 55 | throw new PathNotFoundException(); |
| 56 | } |
| 57 | Path path = paths.iterator().next(); |
| 58 | |
| 59 | return Arrays.asList((Intent) new PathIntent( |
| 60 | intentIdGenerator.getNewId(), |
| 61 | intent.getTrafficSelector(), |
| 62 | intent.getTrafficTreatment(), |
| 63 | new ConnectPoint(intent.getSrc(), PortNumber.ALL), |
| 64 | new ConnectPoint(intent.getDst(), PortNumber.ALL), |
| 65 | path)); |
| 66 | } |
| 67 | } |