Eunjin Choi | 8fcdf28 | 2017-05-17 16:56:52 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Eunjin Choi | 8fcdf28 | 2017-05-17 16:56:52 +0900 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.drivers.cisco.rest; |
| 18 | |
| 19 | import java.io.ByteArrayInputStream; |
| 20 | import java.io.InputStream; |
| 21 | import java.nio.charset.StandardCharsets; |
| 22 | import java.util.List; |
| 23 | import java.util.stream.IntStream; |
| 24 | |
gyewan.an | eeb4cf5 | 2018-12-24 11:47:57 +0900 | [diff] [blame^] | 25 | import com.google.common.collect.Lists; |
Eunjin Choi | 8fcdf28 | 2017-05-17 16:56:52 +0900 | [diff] [blame] | 26 | import org.onosproject.net.DeviceId; |
sdn | b030291 | 2018-06-07 15:44:10 +0900 | [diff] [blame] | 27 | import org.onosproject.net.driver.DriverHandler; |
Eunjin Choi | 8fcdf28 | 2017-05-17 16:56:52 +0900 | [diff] [blame] | 28 | import org.onosproject.protocol.rest.RestSBController; |
| 29 | |
| 30 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 31 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 32 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 33 | |
| 34 | import javax.ws.rs.core.MediaType; |
sdn | b030291 | 2018-06-07 15:44:10 +0900 | [diff] [blame] | 35 | import static com.google.common.base.Preconditions.checkNotNull; |
Eunjin Choi | 8fcdf28 | 2017-05-17 16:56:52 +0900 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Generate json-rpc request for Cisco NX-API. |
| 39 | */ |
| 40 | public 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 | |
sdn | b030291 | 2018-06-07 15:44:10 +0900 | [diff] [blame] | 69 | public static String generate(String command, CommandType type) { |
| 70 | return generate(Lists.newArrayList(command), type); |
| 71 | } |
| 72 | |
Eunjin Choi | 8fcdf28 | 2017-05-17 16:56:52 +0900 | [diff] [blame] | 73 | /** |
| 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 | } |
sdn | b030291 | 2018-06-07 15:44:10 +0900 | [diff] [blame] | 127 | |
| 128 | /** |
| 129 | * Sends NX-API request message to the device. |
| 130 | * @param handler device's driver handler |
gyewan.an | eeb4cf5 | 2018-12-24 11:47:57 +0900 | [diff] [blame^] | 131 | * @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 |
sdn | b030291 | 2018-06-07 15:44:10 +0900 | [diff] [blame] | 146 | * @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.an | eeb4cf5 | 2018-12-24 11:47:57 +0900 | [diff] [blame^] | 159 | * Sends NX-API request message to the device with CLI command. |
| 160 | * @param handler device handler |
| 161 | * @param command NX-API command string |
sdn | b030291 | 2018-06-07 15:44:10 +0900 | [diff] [blame] | 162 | * @return the response string |
| 163 | */ |
gyewan.an | eeb4cf5 | 2018-12-24 11:47:57 +0900 | [diff] [blame^] | 164 | public static String postCli(DriverHandler handler, String command) { |
| 165 | return post(handler, command, CommandType.CLI); |
sdn | b030291 | 2018-06-07 15:44:10 +0900 | [diff] [blame] | 166 | } |
Eunjin Choi | 8fcdf28 | 2017-05-17 16:56:52 +0900 | [diff] [blame] | 167 | } |