Daniel Park | 531fb48 | 2016-07-02 14:21:31 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0a4e674 | 2016-09-15 23:03:10 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Daniel Park | 531fb48 | 2016-07-02 14:21:31 +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.arista; |
| 18 | |
| 19 | import java.io.ByteArrayInputStream; |
| 20 | import java.io.IOException; |
| 21 | import com.fasterxml.jackson.databind.JsonNode; |
| 22 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 23 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 24 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 25 | import com.google.common.collect.Lists; |
| 26 | import org.onosproject.net.AnnotationKeys; |
| 27 | import org.onosproject.net.DefaultAnnotations; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.Port; |
| 30 | import org.onosproject.net.PortNumber; |
| 31 | import org.onosproject.net.SparseAnnotations; |
| 32 | import org.onosproject.net.device.DefaultPortDescription; |
| 33 | import org.onosproject.net.device.DeviceDescription; |
| 34 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
| 35 | import org.onosproject.net.device.PortDescription; |
| 36 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 37 | import org.onosproject.net.driver.DriverHandler; |
| 38 | import org.onosproject.protocol.rest.RestSBController; |
| 39 | import javax.ws.rs.core.MediaType; |
| 40 | import org.slf4j.Logger; |
| 41 | |
| 42 | import java.util.List; |
| 43 | |
| 44 | import static com.google.common.base.Preconditions.checkNotNull; |
| 45 | import static org.slf4j.LoggerFactory.getLogger; |
| 46 | |
| 47 | /** |
| 48 | * Discovers the ports from Arista EOS device. |
| 49 | */ |
| 50 | public 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"; |
| 62 | private static final int WEIGHTING_FACTOR_MANAGEMENT_INTERFACE = 100; |
| 63 | 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()), |
| 110 | MediaType.APPLICATION_JSON, String.class); |
| 111 | |
| 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) { |
| 139 | log.warn("IO exception occured because of ", e); |
| 140 | } |
| 141 | return ports; |
| 142 | } |
| 143 | |
| 144 | private int getPortNumber(String interfaceName) { |
| 145 | if (interfaceName.startsWith(ETHERNET)) { |
| 146 | return Integer.valueOf(interfaceName.substring(ETHERNET.length())); |
| 147 | } else { |
| 148 | return Integer.valueOf(interfaceName.substring(MANAGEMENT.length())).intValue() |
| 149 | + WEIGHTING_FACTOR_MANAGEMENT_INTERFACE; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |