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