blob: 52621e2ff95ed4c23d7c909b8782de770a4abcfd [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
2 * Copyright 2015 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 */
Sho SHIMIZU6c28f832015-02-20 16:12:19 -080016package org.onosproject.net.intent.impl.compiler;
Michele Santuari4b6019e2014-12-19 11:31:45 +010017
18import static java.util.Arrays.asList;
19import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
20
21import java.util.ArrayList;
22import java.util.List;
23import java.util.Set;
24
25
26import org.apache.felix.scr.annotations.Activate;
27import org.apache.felix.scr.annotations.Component;
28import org.apache.felix.scr.annotations.Deactivate;
29import org.onosproject.net.ConnectPoint;
30import org.onosproject.net.DefaultPath;
31import org.onosproject.net.Link;
32import org.onosproject.net.Path;
33import org.onosproject.net.intent.Intent;
34import org.onosproject.net.intent.MplsIntent;
35import org.onosproject.net.intent.MplsPathIntent;
36import org.onosproject.net.provider.ProviderId;
Brian O'Connor6de2e202015-05-21 14:30:41 -070037import org.onosproject.net.resource.link.LinkResourceAllocations;
Michele Santuari4b6019e2014-12-19 11:31:45 +010038
39
40@Component(immediate = true)
41public class MplsIntentCompiler extends ConnectivityIntentCompiler<MplsIntent> {
42
43 // TODO: use off-the-shell core provider ID
44 private static final ProviderId PID =
45 new ProviderId("core", "org.onosproject.core", true);
46 // TODO: consider whether the default cost is appropriate or not
47 public static final int DEFAULT_COST = 1;
48
49
50 @Activate
51 public void activate() {
52 intentManager.registerCompiler(MplsIntent.class, this);
53 }
54
55 @Deactivate
56 public void deactivate() {
57 intentManager.unregisterCompiler(MplsIntent.class);
58 }
59
60 @Override
61 public List<Intent> compile(MplsIntent intent, List<Intent> installable,
62 Set<LinkResourceAllocations> resources) {
63 ConnectPoint ingressPoint = intent.ingressPoint();
64 ConnectPoint egressPoint = intent.egressPoint();
65
66 if (ingressPoint.deviceId().equals(egressPoint.deviceId())) {
67 List<Link> links = asList(createEdgeLink(ingressPoint, true), createEdgeLink(egressPoint, false));
68 return asList(createPathIntent(new DefaultPath(PID, links, DEFAULT_COST), intent));
69 }
70
71 List<Link> links = new ArrayList<>();
72 Path path = getPath(intent, ingressPoint.deviceId(),
73 egressPoint.deviceId());
74
75 links.add(createEdgeLink(ingressPoint, true));
76 links.addAll(path.links());
77
78 links.add(createEdgeLink(egressPoint, false));
79
80 return asList(createPathIntent(new DefaultPath(PID, links, path.cost(),
81 path.annotations()), intent));
82 }
83
84 /**
85 * Creates a path intent from the specified path and original
86 * connectivity intent.
87 *
88 * @param path path to create an intent for
89 * @param intent original intent
90 */
91 private Intent createPathIntent(Path path,
92 MplsIntent intent) {
Ray Milkeyebc5d222015-03-18 15:45:36 -070093 return MplsPathIntent.builder()
94 .appId(intent.appId())
95 .selector(intent.selector())
96 .treatment(intent.treatment())
97 .path(path)
98 .ingressLabel(intent.ingressLabel())
99 .egressLabel(intent.egressLabel())
100 .constraints(intent.constraints())
101 .priority(intent.priority())
102 .build();
Michele Santuari4b6019e2014-12-19 11:31:45 +0100103 }
104
105
106}