blob: 0e4b3f994400a5ecc1ea41b41ebd67919b5b3180 [file] [log] [blame]
DongRyeol Cha06041fa2018-06-07 10:23:32 +09001/*
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
17package org.onosproject.drivers.arista;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.ObjectMapper;
21import com.fasterxml.jackson.databind.node.ArrayNode;
22import com.fasterxml.jackson.databind.node.ObjectNode;
23import com.google.common.collect.Lists;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.driver.DriverHandler;
26import org.onosproject.protocol.rest.RestSBController;
27import org.slf4j.Logger;
28
29import javax.ws.rs.core.MediaType;
30import java.io.ByteArrayInputStream;
31import java.io.IOException;
HelloONOS0854c042019-02-18 20:09:17 +090032import java.util.ArrayList;
DongRyeol Cha06041fa2018-06-07 10:23:32 +090033import java.util.List;
34import java.util.Optional;
35
36import static com.google.common.base.Preconditions.checkNotNull;
37import static org.slf4j.LoggerFactory.getLogger;
38
39final class AristaUtils {
40
sdn9952dab2018-06-12 14:52:51 +090041 private static final String API_ENDPOINT = "/command-api";
DongRyeol Cha06041fa2018-06-07 10:23:32 +090042 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 }
sdn9952dab2018-06-12 14:52:51 +0900132
HelloONOS0854c042019-02-18 20:09:17 +0900133
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 Cha06041fa2018-06-07 10:23:32 +0900168}