DongRyeol Cha | 06041fa | 2018-06-07 10:23:32 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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.arista; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 22 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 23 | import com.google.common.collect.Lists; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.driver.DriverHandler; |
| 26 | import org.onosproject.protocol.rest.RestSBController; |
| 27 | import org.slf4j.Logger; |
| 28 | |
| 29 | import javax.ws.rs.core.MediaType; |
| 30 | import java.io.ByteArrayInputStream; |
| 31 | import java.io.IOException; |
HelloONOS | 0854c04 | 2019-02-18 20:09:17 +0900 | [diff] [blame] | 32 | import java.util.ArrayList; |
DongRyeol Cha | 06041fa | 2018-06-07 10:23:32 +0900 | [diff] [blame] | 33 | import java.util.List; |
| 34 | import java.util.Optional; |
| 35 | |
| 36 | import static com.google.common.base.Preconditions.checkNotNull; |
| 37 | import static org.slf4j.LoggerFactory.getLogger; |
| 38 | |
| 39 | final class AristaUtils { |
| 40 | |
sdn | 9952dab | 2018-06-12 14:52:51 +0900 | [diff] [blame] | 41 | private static final String API_ENDPOINT = "/command-api"; |
DongRyeol Cha | 06041fa | 2018-06-07 10:23:32 +0900 | [diff] [blame] | 42 | private static final String JSONRPC = "jsonrpc"; |
| 43 | private static final String METHOD = "method"; |
| 44 | private static final String RUN_CMDS = "runCmds"; |
| 45 | private static final String VERSION = "version"; |
| 46 | private static final String ID = "id"; |
| 47 | private static final String PARAMS = "params"; |
| 48 | private static final String FORMAT = "format"; |
| 49 | private static final String TIMESTAMPS = "timestamps"; |
| 50 | private static final String CMDS = "cmds"; |
| 51 | private static final String ENABLE = "enable"; |
| 52 | private static final String JSON = "json"; |
| 53 | private static final String TWO_POINT_ZERO = "2.0"; |
| 54 | private static final String ONOS_REST = "onos-rest"; |
| 55 | private static final Boolean FALSE = false; |
| 56 | private static final int VERSION_1 = 1; |
| 57 | private static final String RESULT = "result"; |
| 58 | private static final String ERROR = "error"; |
| 59 | |
| 60 | public static final int RESULT_START_INDEX = 1; |
| 61 | |
| 62 | private static final Logger log = getLogger(AristaUtils.class); |
| 63 | |
| 64 | private AristaUtils() { |
| 65 | |
| 66 | } |
| 67 | |
| 68 | public static Optional<JsonNode> retrieveCommandResult(DriverHandler handler, String cmd) { |
| 69 | List<String> cmds = Lists.newArrayList(); |
| 70 | |
| 71 | cmds.add(cmd); |
| 72 | |
| 73 | return retrieveCommandResult(handler, cmds); |
| 74 | } |
| 75 | |
| 76 | public static Optional<JsonNode> retrieveCommandResult(DriverHandler handler, List<String> cmds) { |
| 77 | RestSBController controller = checkNotNull(handler.get(RestSBController.class)); |
| 78 | DeviceId deviceId = checkNotNull(handler.data()).deviceId(); |
| 79 | String request = generate(cmds); |
| 80 | |
| 81 | log.debug("request :{}", request); |
| 82 | |
| 83 | String response = controller.post(deviceId, API_ENDPOINT, new ByteArrayInputStream(request.getBytes()), |
| 84 | MediaType.APPLICATION_JSON_TYPE, String.class); |
| 85 | |
| 86 | log.debug("response :{}", response); |
| 87 | |
| 88 | try { |
| 89 | ObjectMapper mapper = new ObjectMapper(); |
| 90 | ObjectNode node = (ObjectNode) mapper.readTree(response); |
| 91 | |
| 92 | if (node.has(ERROR)) { |
| 93 | log.error("Error {}", node.get(ERROR)); |
| 94 | return Optional.empty(); |
| 95 | } else { |
| 96 | return Optional.ofNullable(node.get(RESULT)); |
| 97 | } |
| 98 | } catch (IOException e) { |
| 99 | log.warn("IO exception occurred because of ", e); |
| 100 | } |
| 101 | return Optional.empty(); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Generates a ObjectNode from a list of commands in String format. |
| 106 | * |
| 107 | * @param commands a list of commands |
| 108 | * @return an ObjectNode generated from a list of commands in String format |
| 109 | */ |
| 110 | private static String generate(List<String> commands) { |
| 111 | ObjectMapper om = new ObjectMapper(); |
| 112 | |
| 113 | ArrayNode cmds = om.createArrayNode(); |
| 114 | cmds.add(ENABLE); |
| 115 | commands.stream().forEach(cmds::add); //commands here |
| 116 | |
| 117 | ObjectNode parm = om.createObjectNode(); |
| 118 | parm.put(FORMAT, JSON); |
| 119 | parm.put(TIMESTAMPS, FALSE); |
| 120 | parm.put(CMDS, cmds); |
| 121 | parm.put(VERSION, VERSION_1); |
| 122 | |
| 123 | ObjectNode node = om.createObjectNode(); |
| 124 | node.put(JSONRPC, TWO_POINT_ZERO); |
| 125 | node.put(METHOD, RUN_CMDS); |
| 126 | |
| 127 | node.put(PARAMS, parm); |
| 128 | node.put(ID, ONOS_REST); |
| 129 | |
| 130 | return node.toString(); |
| 131 | } |
sdn | 9952dab | 2018-06-12 14:52:51 +0900 | [diff] [blame] | 132 | |
HelloONOS | 0854c04 | 2019-02-18 20:09:17 +0900 | [diff] [blame] | 133 | |
| 134 | public static boolean getWithChecking(DriverHandler handler, String command) { |
| 135 | List<String> cmds = new ArrayList<>(); |
| 136 | |
| 137 | cmds.add(command); |
| 138 | |
| 139 | return getWithChecking(handler, cmds); |
| 140 | } |
| 141 | |
| 142 | public static boolean getWithChecking(DriverHandler handler, List<String> commands) { |
| 143 | RestSBController controller = checkNotNull(handler.get(RestSBController.class)); |
| 144 | DeviceId deviceId = checkNotNull(handler.data()).deviceId(); |
| 145 | String response = generate(commands); |
| 146 | |
| 147 | log.debug("request :{}", response); |
| 148 | |
| 149 | try { |
| 150 | ObjectMapper om = new ObjectMapper(); |
| 151 | JsonNode json = om.readTree(response); |
| 152 | JsonNode errNode = json.findPath(ERROR); |
| 153 | |
| 154 | if (errNode.isMissingNode()) { |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | log.error("Error get with checking {}", errNode.asText("")); |
| 159 | for (String str : commands) { |
| 160 | log.error("Command Failed due to Cmd : {}", str); |
| 161 | } |
| 162 | return false; |
| 163 | } catch (IOException e) { |
| 164 | log.error("IO exception occured because of ", e); |
| 165 | return false; |
| 166 | } |
| 167 | } |
DongRyeol Cha | 06041fa | 2018-06-07 10:23:32 +0900 | [diff] [blame] | 168 | } |