blob: 544ec2e7ce3e633c9e12a196f60d6de16f88ecb6 [file] [log] [blame]
Eunjin Choi8fcdf282017-05-17 16:56:52 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Eunjin Choi8fcdf282017-05-17 16:56:52 +09003 *
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 */
16
17package org.onosproject.drivers.cisco.rest;
18
19import java.io.ByteArrayInputStream;
20import java.io.InputStream;
21import java.nio.charset.StandardCharsets;
22import java.util.List;
23import java.util.stream.IntStream;
24
gyewan.aneeb4cf52018-12-24 11:47:57 +090025import com.google.common.collect.Lists;
Eunjin Choi8fcdf282017-05-17 16:56:52 +090026import org.onosproject.net.DeviceId;
sdnb0302912018-06-07 15:44:10 +090027import org.onosproject.net.driver.DriverHandler;
Eunjin Choi8fcdf282017-05-17 16:56:52 +090028import org.onosproject.protocol.rest.RestSBController;
29
30import com.fasterxml.jackson.databind.ObjectMapper;
31import com.fasterxml.jackson.databind.node.ArrayNode;
32import com.fasterxml.jackson.databind.node.ObjectNode;
33
34import javax.ws.rs.core.MediaType;
sdnb0302912018-06-07 15:44:10 +090035import static com.google.common.base.Preconditions.checkNotNull;
Eunjin Choi8fcdf282017-05-17 16:56:52 +090036
37/**
38 * Generate json-rpc request for Cisco NX-API.
39 */
40public final class NxApiRequest {
41 public enum CommandType {
42 /**
43 * Command outputs are in JSON format.
44 */
45 CLI,
46 /**
47 * Command outputs are in raw ASCII text.
48 */
49 CLI_ASCII,
50 }
51
52 private static final String CMD = "cmd";
53 private static final String VERSION = "version";
54 private static final String JSONRPC = "jsonrpc";
55 private static final String TWO_POINT_ZERO = "2.0";
56 private static final String METHOD = "method";
57 private static final String CLI = "cli";
58 private static final String CLI_ASCII = "cli_ascii";
59 private static final String PARAMS = "params";
60 private static final String ID = "id";
61 private static final int ONE = 1;
62
63 private static final String API_URI = "/ins";
64 private static final String APP_JSON_RPC = "application/json-rpc";
65
66 private NxApiRequest() {
67 }
68
sdnb0302912018-06-07 15:44:10 +090069 public static String generate(String command, CommandType type) {
70 return generate(Lists.newArrayList(command), type);
71 }
72
Eunjin Choi8fcdf282017-05-17 16:56:52 +090073 /**
74 * Generates a NX-API request message to execute on the Cisco NXOS device.
75 * @param commands list of commands to execute
76 * @param type response message format
77 * @return the NX-API request string
78 */
79 public static String generate(List<String> commands, CommandType type) {
80 ObjectMapper om = new ObjectMapper();
81 ArrayNode aryNode = om.createArrayNode();
82
83 if (commands == null) {
84 return aryNode.toString();
85 }
86
87 IntStream.range(0, commands.size()).forEach(idx -> {
88 ObjectNode parm = om.createObjectNode();
89 parm.put(CMD, commands.get(idx));
90 parm.put(VERSION, ONE);
91
92 ObjectNode node = om.createObjectNode();
93 node.put(JSONRPC, TWO_POINT_ZERO);
94 switch (type) {
95 case CLI_ASCII:
96 node.put(METHOD, CLI_ASCII);
97 break;
98 case CLI:
99 default:
100 node.put(METHOD, CLI);
101 break;
102 }
103
104 node.set(PARAMS, parm);
105 node.put(ID, idx + 1);
106
107 aryNode.add(node);
108 });
109
110 return aryNode.toString();
111 }
112
113 /**
114 * Sends NX-API request message to the device.
115 * @param controller RestSBController for Cisco REST device
116 * @param deviceId DeviceId for Cisco REST device
117 * @param request NX-API request string
118 * @return the response string
119 */
120 public static String post(RestSBController controller, DeviceId deviceId, String request) {
121 InputStream stream = new ByteArrayInputStream(request.getBytes(StandardCharsets.UTF_8));
122 String response = controller.post(deviceId, API_URI, stream,
123 MediaType.valueOf(APP_JSON_RPC), String.class);
124
125 return response;
126 }
sdnb0302912018-06-07 15:44:10 +0900127
128 /**
129 * Sends NX-API request message to the device.
130 * @param handler device's driver handler
gyewan.aneeb4cf52018-12-24 11:47:57 +0900131 * @param cmds NX-API list of command strings
132 * @return the response string
133 */
134 public static String postClis(DriverHandler handler, List<String> cmds) {
135 RestSBController controller = checkNotNull(handler.get(RestSBController.class));
136 DeviceId deviceId = handler.data().deviceId();
137
138 String request = generate(cmds, CommandType.CLI);
139 InputStream stream = new ByteArrayInputStream(request.getBytes(StandardCharsets.UTF_8));
140 return controller.post(deviceId, API_URI, stream, MediaType.valueOf(APP_JSON_RPC), String.class);
141 }
142
143 /**
144 * Sends NX-API request message to the device.
145 * @param handler device's driver handler
sdnb0302912018-06-07 15:44:10 +0900146 * @param command NX-API command string
147 * @param type response message format
148 * @return the response string
149 */
150 public static String post(DriverHandler handler, String command, CommandType type) {
151 RestSBController controller = checkNotNull(handler.get(RestSBController.class));
152 DeviceId deviceId = handler.data().deviceId();
153
154 String request = generate(command, type);
155 return post(controller, deviceId, request);
156 }
157
158 /**
gyewan.aneeb4cf52018-12-24 11:47:57 +0900159 * Sends NX-API request message to the device with CLI command.
160 * @param handler device handler
161 * @param command NX-API command string
sdnb0302912018-06-07 15:44:10 +0900162 * @return the response string
163 */
gyewan.aneeb4cf52018-12-24 11:47:57 +0900164 public static String postCli(DriverHandler handler, String command) {
165 return post(handler, command, CommandType.CLI);
sdnb0302912018-06-07 15:44:10 +0900166 }
Eunjin Choi8fcdf282017-05-17 16:56:52 +0900167}