Sho SHIMIZU | 6c28f83 | 2015-02-20 16:12:19 -0800 | [diff] [blame] | 1 | package org.onosproject.net.intent.impl.compiler; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 2 | |
| 3 | import static java.util.Arrays.asList; |
| 4 | import static org.onosproject.net.DefaultEdgeLink.createEdgeLink; |
| 5 | |
| 6 | import java.util.ArrayList; |
| 7 | import java.util.List; |
| 8 | import java.util.Set; |
| 9 | |
| 10 | |
| 11 | import org.apache.felix.scr.annotations.Activate; |
| 12 | import org.apache.felix.scr.annotations.Component; |
| 13 | import org.apache.felix.scr.annotations.Deactivate; |
| 14 | import org.onosproject.net.ConnectPoint; |
| 15 | import org.onosproject.net.DefaultPath; |
| 16 | import org.onosproject.net.Link; |
| 17 | import org.onosproject.net.Path; |
| 18 | import org.onosproject.net.intent.Intent; |
| 19 | import org.onosproject.net.intent.MplsIntent; |
| 20 | import org.onosproject.net.intent.MplsPathIntent; |
| 21 | import org.onosproject.net.provider.ProviderId; |
| 22 | import org.onosproject.net.resource.LinkResourceAllocations; |
| 23 | |
| 24 | |
| 25 | @Component(immediate = true) |
| 26 | public class MplsIntentCompiler extends ConnectivityIntentCompiler<MplsIntent> { |
| 27 | |
| 28 | // TODO: use off-the-shell core provider ID |
| 29 | private static final ProviderId PID = |
| 30 | new ProviderId("core", "org.onosproject.core", true); |
| 31 | // TODO: consider whether the default cost is appropriate or not |
| 32 | public static final int DEFAULT_COST = 1; |
| 33 | |
| 34 | |
| 35 | @Activate |
| 36 | public void activate() { |
| 37 | intentManager.registerCompiler(MplsIntent.class, this); |
| 38 | } |
| 39 | |
| 40 | @Deactivate |
| 41 | public void deactivate() { |
| 42 | intentManager.unregisterCompiler(MplsIntent.class); |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public List<Intent> compile(MplsIntent intent, List<Intent> installable, |
| 47 | Set<LinkResourceAllocations> resources) { |
| 48 | ConnectPoint ingressPoint = intent.ingressPoint(); |
| 49 | ConnectPoint egressPoint = intent.egressPoint(); |
| 50 | |
| 51 | if (ingressPoint.deviceId().equals(egressPoint.deviceId())) { |
| 52 | List<Link> links = asList(createEdgeLink(ingressPoint, true), createEdgeLink(egressPoint, false)); |
| 53 | return asList(createPathIntent(new DefaultPath(PID, links, DEFAULT_COST), intent)); |
| 54 | } |
| 55 | |
| 56 | List<Link> links = new ArrayList<>(); |
| 57 | Path path = getPath(intent, ingressPoint.deviceId(), |
| 58 | egressPoint.deviceId()); |
| 59 | |
| 60 | links.add(createEdgeLink(ingressPoint, true)); |
| 61 | links.addAll(path.links()); |
| 62 | |
| 63 | links.add(createEdgeLink(egressPoint, false)); |
| 64 | |
| 65 | return asList(createPathIntent(new DefaultPath(PID, links, path.cost(), |
| 66 | path.annotations()), intent)); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Creates a path intent from the specified path and original |
| 71 | * connectivity intent. |
| 72 | * |
| 73 | * @param path path to create an intent for |
| 74 | * @param intent original intent |
| 75 | */ |
| 76 | private Intent createPathIntent(Path path, |
| 77 | MplsIntent intent) { |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame^] | 78 | return MplsPathIntent.builder() |
| 79 | .appId(intent.appId()) |
| 80 | .selector(intent.selector()) |
| 81 | .treatment(intent.treatment()) |
| 82 | .path(path) |
| 83 | .ingressLabel(intent.ingressLabel()) |
| 84 | .egressLabel(intent.egressLabel()) |
| 85 | .constraints(intent.constraints()) |
| 86 | .priority(intent.priority()) |
| 87 | .build(); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | |
| 91 | } |