blob: 07509c9baf98e8ceadd368f242274f712c720523 [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
25import org.onosproject.net.DeviceId;
sdnb0302912018-06-07 15:44:10 +090026import org.onosproject.net.driver.DriverHandler;
Eunjin Choi8fcdf282017-05-17 16:56:52 +090027import org.onosproject.protocol.rest.RestSBController;
28
29import com.fasterxml.jackson.databind.ObjectMapper;
30import com.fasterxml.jackson.databind.node.ArrayNode;
31import com.fasterxml.jackson.databind.node.ObjectNode;
32
33import javax.ws.rs.core.MediaType;
sdnb0302912018-06-07 15:44:10 +090034import com.google.common.collect.Lists;
35import 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
131 * @param command NX-API command string
132 * @param type response message format
133 * @return the response string
134 */
135 public static String post(DriverHandler handler, String command, CommandType type) {
136 RestSBController controller = checkNotNull(handler.get(RestSBController.class));
137 DeviceId deviceId = handler.data().deviceId();
138
139 String request = generate(command, type);
140 return post(controller, deviceId, request);
141 }
142
143 /**
144 * Sends NX-API request message to the device.
145 * @param handler device's driver handler
146 * @param cmds NX-API list of command strings
147 * @return the response string
148 */
149 static String postClis(DriverHandler handler, List<String> cmds) {
150 RestSBController controller = checkNotNull(handler.get(RestSBController.class));
151 DeviceId deviceId = handler.data().deviceId();
152
153 String request = generate(cmds, CommandType.CLI);
154 InputStream stream = new ByteArrayInputStream(request.getBytes(StandardCharsets.UTF_8));
155 return controller.post(deviceId, API_URI, stream, MediaType.valueOf(APP_JSON_RPC), String.class);
156 }
Eunjin Choi8fcdf282017-05-17 16:56:52 +0900157}