blob: 79a1d58aa043dfba150fc239d377212ac6a13b79 [file] [log] [blame]
Eunjin Choi8fcdf282017-05-17 16:56:52 +09001/*
2 * Copyright 2017-present Open Networking Laboratory
3 *
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;
26import org.onosproject.protocol.rest.RestSBController;
27
28import com.fasterxml.jackson.databind.ObjectMapper;
29import com.fasterxml.jackson.databind.node.ArrayNode;
30import com.fasterxml.jackson.databind.node.ObjectNode;
31
32import javax.ws.rs.core.MediaType;
33
34/**
35 * Generate json-rpc request for Cisco NX-API.
36 */
37public final class NxApiRequest {
38 public enum CommandType {
39 /**
40 * Command outputs are in JSON format.
41 */
42 CLI,
43 /**
44 * Command outputs are in raw ASCII text.
45 */
46 CLI_ASCII,
47 }
48
49 private static final String CMD = "cmd";
50 private static final String VERSION = "version";
51 private static final String JSONRPC = "jsonrpc";
52 private static final String TWO_POINT_ZERO = "2.0";
53 private static final String METHOD = "method";
54 private static final String CLI = "cli";
55 private static final String CLI_ASCII = "cli_ascii";
56 private static final String PARAMS = "params";
57 private static final String ID = "id";
58 private static final int ONE = 1;
59
60 private static final String API_URI = "/ins";
61 private static final String APP_JSON_RPC = "application/json-rpc";
62
63 private NxApiRequest() {
64 }
65
66 /**
67 * Generates a NX-API request message to execute on the Cisco NXOS device.
68 * @param commands list of commands to execute
69 * @param type response message format
70 * @return the NX-API request string
71 */
72 public static String generate(List<String> commands, CommandType type) {
73 ObjectMapper om = new ObjectMapper();
74 ArrayNode aryNode = om.createArrayNode();
75
76 if (commands == null) {
77 return aryNode.toString();
78 }
79
80 IntStream.range(0, commands.size()).forEach(idx -> {
81 ObjectNode parm = om.createObjectNode();
82 parm.put(CMD, commands.get(idx));
83 parm.put(VERSION, ONE);
84
85 ObjectNode node = om.createObjectNode();
86 node.put(JSONRPC, TWO_POINT_ZERO);
87 switch (type) {
88 case CLI_ASCII:
89 node.put(METHOD, CLI_ASCII);
90 break;
91 case CLI:
92 default:
93 node.put(METHOD, CLI);
94 break;
95 }
96
97 node.set(PARAMS, parm);
98 node.put(ID, idx + 1);
99
100 aryNode.add(node);
101 });
102
103 return aryNode.toString();
104 }
105
106 /**
107 * Sends NX-API request message to the device.
108 * @param controller RestSBController for Cisco REST device
109 * @param deviceId DeviceId for Cisco REST device
110 * @param request NX-API request string
111 * @return the response string
112 */
113 public static String post(RestSBController controller, DeviceId deviceId, String request) {
114 InputStream stream = new ByteArrayInputStream(request.getBytes(StandardCharsets.UTF_8));
115 String response = controller.post(deviceId, API_URI, stream,
116 MediaType.valueOf(APP_JSON_RPC), String.class);
117
118 return response;
119 }
120}