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