blob: 2a7bef79c03099e44ef18a9b69a397975e756a2f [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;
32import java.util.List;
33import java.util.Optional;
34
35import static com.google.common.base.Preconditions.checkNotNull;
36import static org.slf4j.LoggerFactory.getLogger;
37
38final class AristaUtils {
39
sdn9952dab2018-06-12 14:52:51 +090040 private static final String API_ENDPOINT = "/command-api";
DongRyeol Cha06041fa2018-06-07 10:23:32 +090041 private static final String JSONRPC = "jsonrpc";
42 private static final String METHOD = "method";
43 private static final String RUN_CMDS = "runCmds";
44 private static final String VERSION = "version";
45 private static final String ID = "id";
46 private static final String PARAMS = "params";
47 private static final String FORMAT = "format";
48 private static final String TIMESTAMPS = "timestamps";
49 private static final String CMDS = "cmds";
50 private static final String ENABLE = "enable";
51 private static final String JSON = "json";
52 private static final String TWO_POINT_ZERO = "2.0";
53 private static final String ONOS_REST = "onos-rest";
54 private static final Boolean FALSE = false;
55 private static final int VERSION_1 = 1;
56 private static final String RESULT = "result";
57 private static final String ERROR = "error";
58
59 public static final int RESULT_START_INDEX = 1;
60
61 private static final Logger log = getLogger(AristaUtils.class);
62
63 private AristaUtils() {
64
65 }
66
67 public static Optional<JsonNode> retrieveCommandResult(DriverHandler handler, String cmd) {
68 List<String> cmds = Lists.newArrayList();
69
70 cmds.add(cmd);
71
72 return retrieveCommandResult(handler, cmds);
73 }
74
75 public static Optional<JsonNode> retrieveCommandResult(DriverHandler handler, List<String> cmds) {
76 RestSBController controller = checkNotNull(handler.get(RestSBController.class));
77 DeviceId deviceId = checkNotNull(handler.data()).deviceId();
78 String request = generate(cmds);
79
80 log.debug("request :{}", request);
81
82 String response = controller.post(deviceId, API_ENDPOINT, new ByteArrayInputStream(request.getBytes()),
83 MediaType.APPLICATION_JSON_TYPE, String.class);
84
85 log.debug("response :{}", response);
86
87 try {
88 ObjectMapper mapper = new ObjectMapper();
89 ObjectNode node = (ObjectNode) mapper.readTree(response);
90
91 if (node.has(ERROR)) {
92 log.error("Error {}", node.get(ERROR));
93 return Optional.empty();
94 } else {
95 return Optional.ofNullable(node.get(RESULT));
96 }
97 } catch (IOException e) {
98 log.warn("IO exception occurred because of ", e);
99 }
100 return Optional.empty();
101 }
102
103 /**
104 * Generates a ObjectNode from a list of commands in String format.
105 *
106 * @param commands a list of commands
107 * @return an ObjectNode generated from a list of commands in String format
108 */
109 private static String generate(List<String> commands) {
110 ObjectMapper om = new ObjectMapper();
111
112 ArrayNode cmds = om.createArrayNode();
113 cmds.add(ENABLE);
114 commands.stream().forEach(cmds::add); //commands here
115
116 ObjectNode parm = om.createObjectNode();
117 parm.put(FORMAT, JSON);
118 parm.put(TIMESTAMPS, FALSE);
119 parm.put(CMDS, cmds);
120 parm.put(VERSION, VERSION_1);
121
122 ObjectNode node = om.createObjectNode();
123 node.put(JSONRPC, TWO_POINT_ZERO);
124 node.put(METHOD, RUN_CMDS);
125
126 node.put(PARAMS, parm);
127 node.put(ID, ONOS_REST);
128
129 return node.toString();
130 }
sdn9952dab2018-06-12 14:52:51 +0900131
DongRyeol Cha06041fa2018-06-07 10:23:32 +0900132}