blob: 5a21de0d21f6d2d3a337183c17a5aa3482d079b4 [file] [log] [blame]
Daniel Park531fb482016-07-02 14:21:31 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Daniel Park531fb482016-07-02 14:21:31 +09003 *
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 java.io.ByteArrayInputStream;
20import java.io.IOException;
21import com.fasterxml.jackson.databind.JsonNode;
22import com.fasterxml.jackson.databind.ObjectMapper;
23import com.fasterxml.jackson.databind.node.ArrayNode;
24import com.fasterxml.jackson.databind.node.ObjectNode;
25import com.google.common.collect.Lists;
26import org.onosproject.net.AnnotationKeys;
27import org.onosproject.net.DefaultAnnotations;
28import org.onosproject.net.DeviceId;
29import org.onosproject.net.Port;
30import org.onosproject.net.PortNumber;
31import org.onosproject.net.SparseAnnotations;
32import org.onosproject.net.device.DefaultPortDescription;
33import org.onosproject.net.device.DeviceDescription;
34import org.onosproject.net.device.DeviceDescriptionDiscovery;
35import org.onosproject.net.device.PortDescription;
36import org.onosproject.net.driver.AbstractHandlerBehaviour;
37import org.onosproject.net.driver.DriverHandler;
38import org.onosproject.protocol.rest.RestSBController;
39import javax.ws.rs.core.MediaType;
40import org.slf4j.Logger;
41
42import java.util.List;
43
44import static com.google.common.base.Preconditions.checkNotNull;
45import static org.slf4j.LoggerFactory.getLogger;
46
47/**
48 * Discovers the ports from Arista EOS device.
49 */
50public class DeviceDescriptionDiscoveryAristaImpl extends AbstractHandlerBehaviour
51 implements DeviceDescriptionDiscovery {
52
53 private static final String JSON = "json";
54 private static final String RESULT = "result";
55 private static final String INTERFACE_STATUSES = "interfaceStatuses";
56 private static final String LINK_STATUS = "linkStatus";
57 private static final String LINE_PROTOCOL_STATUS = "lineProtocolStatus";
58 private static final String BANDWIDTH = "bandwidth";
59 private static final String ETHERNET = "Ethernet";
60 private static final String MANAGEMENT = "Management";
61 private static final String INTERFACE_TYPE = "interfaceType";
Eunjin Choi51244d32017-05-15 14:09:56 +090062 private static final int WEIGHTING_FACTOR_MANAGEMENT_INTERFACE = 10000;
Daniel Park531fb482016-07-02 14:21:31 +090063 private static final String JSONRPC = "jsonrpc";
64 private static final String METHOD = "method";
65 private static final String RUN_CMDS = "runCmds";
66 private static final String VERSION = "version";
67 private static final String ID = "id";
68 private static final String GET_PORT = "GetPort";
69 private static final String PARAMS = "params";
70 private static final String FORMAT = "format";
71 private static final String TIMESTAMPS = "timestamps";
72 private static final String CMDS = "cmds";
73 private static final String SHOW_INTERFACES_STATUS = "show interfaces status";
74 private static final String TWO_POINT_ZERO = "2.0";
75 private static final long MBPS = 1000000;
76
77 private final Logger log = getLogger(getClass());
78
79 private static final String API_ENDPOINT = "/command-api/";
80
81 @Override
82 public DeviceDescription discoverDeviceDetails() {
83 log.info("No description to be added for device");
84 //TODO to be implemented if needed.
85 return null;
86 }
87
88 @Override
89 public List<PortDescription> discoverPortDetails() {
90 List<PortDescription> ports = Lists.newArrayList();
91 DriverHandler handler = handler();
92 RestSBController controller = checkNotNull(handler.get(RestSBController.class));
93 DeviceId deviceId = handler.data().deviceId();
94
95 ObjectMapper mapper = new ObjectMapper();
96
97 ObjectNode sendObjNode = mapper.createObjectNode();
98
99 sendObjNode.put(JSONRPC, TWO_POINT_ZERO)
100 .put(METHOD, RUN_CMDS)
101 .put(ID, GET_PORT)
102 .putObject(PARAMS)
103 .put(FORMAT, JSON)
104 .put(TIMESTAMPS, false)
105 .put(VERSION, 1)
106 .putArray(CMDS).add(SHOW_INTERFACES_STATUS);
107
108 String response = controller.post(deviceId, API_ENDPOINT,
109 new ByteArrayInputStream(sendObjNode.toString().getBytes()),
Eunjin Choi51244d32017-05-15 14:09:56 +0900110 MediaType.APPLICATION_JSON_TYPE, String.class);
Daniel Park531fb482016-07-02 14:21:31 +0900111
112 try {
113 ObjectNode node = (ObjectNode) mapper.readTree(response);
114 ArrayNode arrayNode = (ArrayNode) node.get(RESULT);
115
116 JsonNode jsonNode = arrayNode.iterator().next().get(INTERFACE_STATUSES);
117
118 jsonNode.fieldNames().forEachRemaining(name -> {
119 JsonNode interfaceNode = jsonNode.get(name);
120
121 Long bandwidth = interfaceNode.path(BANDWIDTH).asLong() / MBPS;
122
123 SparseAnnotations annotations = DefaultAnnotations.builder()
124 .set(AnnotationKeys.BANDWIDTH, bandwidth.toString())
125 .set(AnnotationKeys.NAME, name)
126 .set(LINK_STATUS, interfaceNode.path(LINK_STATUS).asText())
127 .set(LINE_PROTOCOL_STATUS, interfaceNode.path(LINE_PROTOCOL_STATUS).asText())
128 .set(INTERFACE_TYPE, interfaceNode.path(INTERFACE_TYPE).asText())
129 .build();
130
131 PortDescription portDescription = new DefaultPortDescription(PortNumber
132 .portNumber(getPortNumber(name)),
133 true, Port.Type.FIBER, bandwidth, annotations);
134 ports.add(portDescription);
135
136 });
137
138 } catch (IOException e) {
Frank Wangd8ab0962017-08-11 11:09:30 +0800139 log.warn("IO exception occurred because of ", e);
Daniel Park531fb482016-07-02 14:21:31 +0900140 }
141 return ports;
142 }
143
144 private int getPortNumber(String interfaceName) {
145 if (interfaceName.startsWith(ETHERNET)) {
Eunjin Choi51244d32017-05-15 14:09:56 +0900146 return Integer.valueOf(interfaceName.substring(ETHERNET.length()).replace('/', '0'));
Daniel Park531fb482016-07-02 14:21:31 +0900147 } else {
148 return Integer.valueOf(interfaceName.substring(MANAGEMENT.length())).intValue()
149 + WEIGHTING_FACTOR_MANAGEMENT_INTERFACE;
150 }
151 }
152}
153