blob: a736f99adf887a0f2f54291551affc2a6a4b60a1 [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 */
Michele Santuari4b6019e2014-12-19 11:31:45 +010016package org.onosproject.cli.net;
17
Michele Santuari4b6019e2014-12-19 11:31:45 +010018import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.apache.karaf.shell.commands.Option;
Michele Santuari4b6019e2014-12-19 11:31:45 +010021import org.onlab.packet.MplsLabel;
22import org.onosproject.net.ConnectPoint;
Michele Santuari4b6019e2014-12-19 11:31:45 +010023import org.onosproject.net.flow.TrafficSelector;
24import org.onosproject.net.flow.TrafficTreatment;
25import org.onosproject.net.intent.Constraint;
26import org.onosproject.net.intent.IntentService;
27import org.onosproject.net.intent.MplsIntent;
28
Jonathan Hartc3af35a2015-04-30 22:20:29 -070029import java.util.List;
30import java.util.Optional;
Ray Milkey50a9b722015-03-12 10:38:55 -070031
Charles M.C. Chan6f5bdc62015-04-22 01:21:09 +080032/**
33 * Installs MPLS intents.
34 */
Michele Santuari4b6019e2014-12-19 11:31:45 +010035@Command(scope = "onos", name = "add-mpls-intent", description = "Installs mpls connectivity intent")
36public class AddMplsIntent extends ConnectivityIntentCommand {
37
38 @Argument(index = 0, name = "ingressDevice",
39 description = "Ingress Device/Port Description",
40 required = true,
41 multiValued = false)
42 private String ingressDeviceString = null;
43
44 @Option(name = "--ingressLabel",
45 description = "Ingress Mpls label",
46 required = false,
47 multiValued = false)
48 private String ingressLabelString = "";
49
50 @Argument(index = 1, name = "egressDevice",
51 description = "Egress Device/Port Description",
52 required = true,
53 multiValued = false)
54 private String egressDeviceString = null;
55
56 @Option(name = "--egressLabel",
57 description = "Egress Mpls label",
58 required = false,
59 multiValued = false)
60 private String egressLabelString = "";
61
62 @Override
63 protected void execute() {
64 IntentService service = get(IntentService.class);
65
Jonathan Hartc3af35a2015-04-30 22:20:29 -070066 ConnectPoint ingress = ConnectPoint.deviceConnectPoint(ingressDeviceString);
Michele Santuari4b6019e2014-12-19 11:31:45 +010067 Optional<MplsLabel> ingressLabel = Optional.empty();
68 if (!ingressLabelString.isEmpty()) {
69 ingressLabel = Optional
70 .ofNullable(MplsLabel.mplsLabel(parseInt(ingressLabelString)));
71 }
72
Jonathan Hartc3af35a2015-04-30 22:20:29 -070073 ConnectPoint egress = ConnectPoint.deviceConnectPoint(egressDeviceString);
Michele Santuari4b6019e2014-12-19 11:31:45 +010074 Optional<MplsLabel> egressLabel = Optional.empty();
Charles Chan4b003c52015-08-23 23:04:27 +080075 if (!egressLabelString.isEmpty()) {
Michele Santuari4b6019e2014-12-19 11:31:45 +010076 egressLabel = Optional
77 .ofNullable(MplsLabel.mplsLabel(parseInt(egressLabelString)));
78 }
79
80 TrafficSelector selector = buildTrafficSelector();
81 TrafficTreatment treatment = buildTrafficTreatment();
82
83 List<Constraint> constraints = buildConstraints();
84
Ray Milkeyebc5d222015-03-18 15:45:36 -070085 MplsIntent intent = MplsIntent.builder()
86 .appId(appId())
87 .selector(selector)
88 .treatment(treatment)
89 .ingressPoint(ingress)
90 .ingressLabel(ingressLabel)
91 .egressPoint(egress)
92 .egressLabel(egressLabel)
93 .constraints(constraints)
94 .priority(priority())
95 .build();
Michele Santuari4b6019e2014-12-19 11:31:45 +010096 service.submit(intent);
97 }
98
Michele Santuari4b6019e2014-12-19 11:31:45 +010099 protected Integer parseInt(String value) {
100 try {
101 return Integer.parseInt(value);
102 } catch (NumberFormatException nfe) {
103 return null;
104 }
105 }
106}