blob: 609f9a34ca26ba1c6435434c063ca435682cc844 [file] [log] [blame]
Sho SHIMIZU6c28f832015-02-20 16:12:19 -08001package org.onosproject.net.intent.impl.compiler;
Michele Santuari4b6019e2014-12-19 11:31:45 +01002
3import static java.util.Arrays.asList;
4import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
5
6import java.util.ArrayList;
7import java.util.List;
8import java.util.Set;
9
10
11import org.apache.felix.scr.annotations.Activate;
12import org.apache.felix.scr.annotations.Component;
13import org.apache.felix.scr.annotations.Deactivate;
14import org.onosproject.net.ConnectPoint;
15import org.onosproject.net.DefaultPath;
16import org.onosproject.net.Link;
17import org.onosproject.net.Path;
18import org.onosproject.net.intent.Intent;
19import org.onosproject.net.intent.MplsIntent;
20import org.onosproject.net.intent.MplsPathIntent;
21import org.onosproject.net.provider.ProviderId;
Brian O'Connor6de2e202015-05-21 14:30:41 -070022import org.onosproject.net.resource.link.LinkResourceAllocations;
Michele Santuari4b6019e2014-12-19 11:31:45 +010023
24
25@Component(immediate = true)
26public 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 Milkeyebc5d222015-03-18 15:45:36 -070078 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 Santuari4b6019e2014-12-19 11:31:45 +010088 }
89
90
91}