blob: 373d5fffa6bd6005aba8e70b44c59b994cef6c70 [file] [log] [blame]
Yuta HIGUCHIa1071722016-12-07 20:21:16 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Yuta HIGUCHIa1071722016-12-07 20:21:16 -08003 *
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 */
16package org.onosproject.cli.net;
17
18import java.util.ArrayList;
19import java.util.List;
20import java.util.Optional;
21
Ray Milkeyd84f89b2018-08-17 14:54:17 -070022import org.apache.karaf.shell.api.action.Argument;
23import org.apache.karaf.shell.api.action.Command;
24import org.apache.karaf.shell.api.action.lifecycle.Service;
25import org.apache.karaf.shell.api.action.Option;
Yuta HIGUCHIa1071722016-12-07 20:21:16 -080026import org.onlab.packet.VlanId;
27import org.onosproject.cli.AbstractShellCommand;
28import org.onosproject.net.ConnectPoint;
29import org.onosproject.net.DeviceId;
30import org.onosproject.net.FilteredConnectPoint;
31import org.onosproject.net.PortNumber;
32import org.onosproject.net.behaviour.protection.ProtectedTransportEndpointDescription;
33import org.onosproject.net.behaviour.protection.TransportEndpointDescription;
34import org.onosproject.net.device.DeviceService;
35import org.onosproject.net.flow.DefaultTrafficSelector;
36import org.onosproject.net.flow.TrafficSelector;
37import org.onosproject.net.intent.IntentService;
38import org.onosproject.net.intent.Key;
39import org.onosproject.net.intent.ProtectionEndpointIntent;
40
41/**
42 * Test tool to add ProtectionEndpointIntent.
43 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070044@Service
Yuta HIGUCHIa1071722016-12-07 20:21:16 -080045@Command(scope = "onos", name = "test-add-protection-endpoint",
46 description = "Test tool to add ProtectionEndpointIntent")
47public class TestProtectionEndpointIntentCommand extends AbstractShellCommand {
48
49 private static final String DEFAULT_FINGERPRINT = "[fingerprint]";
50
51 @Option(name = "--fingerprint",
52 description = "Fingerprint to identify the protected transport entity",
53 valueToShowInHelp = DEFAULT_FINGERPRINT)
54 private String fingerprint = DEFAULT_FINGERPRINT;
55
56 @Argument(index = 0, name = "deviceId",
57 description = "Device ID to configure",
58 required = true)
59 private String deviceIdStr = null;
60
61 @Argument(index = 1, name = "peerDeviceId",
62 description = "Device ID of remote peer",
63 required = true)
64 private String peerStr = null;
65
66 @Argument(index = 2, name = "portNumber1",
67 description = "PortNumber leading to working path on deviceId",
68 required = true)
69 private String portNumber1Str = null;
70
71 @Argument(index = 3, name = "portNumber2",
72 description = "PortNumber leading to standby path on deviceId",
73 required = true)
74 private String portNumber2Str = null;
75
76 @Option(name = "--vlan1",
77 description = "VLAN ID to push on portNumber1 expressed in decimal")
78 private String vlan1Str = null;
79
80 @Option(name = "--vlan2",
81 description = "VLAN ID to push on portNumber2 expressed in decimal")
82 private String vlan2Str = null;
83
84 private IntentService intentService;
85 private DeviceService deviceService;
86
87 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070088 protected void doExecute() {
Yuta HIGUCHIa1071722016-12-07 20:21:16 -080089 fingerprint = Optional.ofNullable(fingerprint)
90 .orElse(DEFAULT_FINGERPRINT);
91
92 intentService = get(IntentService.class);
93 deviceService = get(DeviceService.class);
94
95 DeviceId did = DeviceId.deviceId(deviceIdStr);
96 DeviceId peer = DeviceId.deviceId(peerStr);
97
98 ProtectedTransportEndpointDescription description;
99 List<TransportEndpointDescription> paths = new ArrayList<>();
100
101 paths.add(TransportEndpointDescription.builder()
102 .withOutput(output(did, portNumber1Str, vlan1Str))
103 .build());
104 paths.add(TransportEndpointDescription.builder()
105 .withOutput(output(did, portNumber2Str, vlan2Str))
106 .build());
107
108 description = ProtectedTransportEndpointDescription.of(paths, peer, fingerprint);
109
110 ProtectionEndpointIntent intent;
111 intent = ProtectionEndpointIntent.builder()
112 .key(Key.of(fingerprint, appId()))
113 .appId(appId())
114 .deviceId(did)
115 .description(description)
116 .build();
117 print("Submitting: %s", intent);
118 intentService.submit(intent);
119 }
120
121 private FilteredConnectPoint output(DeviceId did, String portNumberStr, String vlanStr) {
122 ConnectPoint cp = new ConnectPoint(did,
123 PortNumber.fromString(portNumberStr));
124 if (deviceService.getPort(cp) == null) {
125 print("Unknown port: %s", cp);
126 }
127
128 if (vlanStr == null) {
129 return new FilteredConnectPoint(cp);
130 } else {
131 VlanId vlan = VlanId.vlanId(vlanStr);
132 TrafficSelector sel = DefaultTrafficSelector.builder()
133 .matchVlanId(vlan)
134 .build();
135
136 return new FilteredConnectPoint(cp, sel);
137
138 }
139 }
140
141}