blob: 89fd33126eeb99caf1f2526c1676b16470314bf3 [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;
22import org.onosproject.net.resource.LinkResourceAllocations;
23
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) {
78 return new MplsPathIntent(intent.appId(),
79 intent.selector(), intent.treatment(), path,
80 intent.ingressLabel(), intent.egressLabel(),
81 intent.constraints());
82 }
83
84
85}