blob: 63cd8390273d83c72a4d96f5619fd5b557823d93 [file] [log] [blame]
Eunjin Choi8fcdf282017-05-17 16:56:52 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Eunjin Choi8fcdf282017-05-17 16:56:52 +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.cisco.rest;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.ObjectMapper;
21import com.google.common.collect.Lists;
22import org.onlab.packet.ChassisId;
23import org.onosproject.net.AnnotationKeys;
24import org.onosproject.net.DefaultAnnotations;
25import org.onosproject.net.Device;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.Port;
28import org.onosproject.net.PortNumber;
29import org.onosproject.net.SparseAnnotations;
30import org.onosproject.net.device.DefaultDeviceDescription;
31import org.onosproject.net.device.DefaultPortDescription;
32import org.onosproject.net.device.DeviceDescription;
33import org.onosproject.net.device.DeviceDescriptionDiscovery;
34import org.onosproject.net.device.DeviceService;
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 org.slf4j.Logger;
40
41import java.io.IOException;
42import java.util.ArrayList;
43import java.util.List;
44
45import static com.google.common.base.Preconditions.checkNotNull;
46import static org.slf4j.LoggerFactory.getLogger;
47
48/**
49 * Discovers device information from a Cisco NXOS device.
50 */
51public class CiscoNxosDeviceDescription extends AbstractHandlerBehaviour
52 implements DeviceDescriptionDiscovery {
53 private final Logger log = getLogger(getClass());
54
55 private static final String UNKNOWN = "unknown";
Eunjin Choi8fcdf282017-05-17 16:56:52 +090056 private static final String SHOW_INTERFACES_CMD = "show interface";
57 private static final String SHOW_VERSION_CMD = "show version";
Eunjin Choi8fcdf282017-05-17 16:56:52 +090058 private static final String MANUFACTURER = "manufacturer";
59 private static final String CHASSIS_ID = "chassis_id";
60 private static final String KICKSTART_VER = "kickstart_ver_str";
61 private static final String ROW_INTERFACE = "ROW_interface";
62 private static final String INTERFACE = "interface";
63 private static final String ETH = "Eth";
64 private static final String ETHERNET = "Ethernet";
65 private static final String STATE = "state";
66 private static final String UP = "up";
67 private static final String ETH_BW = "eth_bw";
68 private static final String SLASH = "/";
69 private static final String ZERO = "0";
70 private static final int ONE_THOUSAND = 1000;
71
Eunjin Choi8fcdf282017-05-17 16:56:52 +090072 @Override
73 public DeviceDescription discoverDeviceDetails() {
74 DriverHandler handler = handler();
75 RestSBController controller = checkNotNull(handler.get(RestSBController.class));
76 DeviceId deviceId = handler.data().deviceId();
77
78 ArrayList<String> cmd = new ArrayList<>();
79 cmd.add(SHOW_VERSION_CMD);
80
81 String req = NxApiRequest.generate(cmd, NxApiRequest.CommandType.CLI);
82
83 String response = NxApiRequest.post(controller, deviceId, req);
84
85 String mrf = UNKNOWN;
86 String hwVer = UNKNOWN;
87 String swVer = UNKNOWN;
88 String serialNum = UNKNOWN;
89
90 try {
91 ObjectMapper om = new ObjectMapper();
92 JsonNode json = om.readTree(response);
93
94 JsonNode body = json.findValue("body");
95 if (body != null) {
96 mrf = body.get(MANUFACTURER).asText();
97 hwVer = body.get(CHASSIS_ID).asText();
98 swVer = body.get(KICKSTART_VER).asText();
99 }
100 } catch (IOException e) {
101 log.error("Failed to to retrieve Device Information {}", e);
102 }
103
104 DeviceService deviceService = checkNotNull(handler().get(DeviceService.class));
105 Device device = deviceService.getDevice(deviceId);
106 return new DefaultDeviceDescription(device.id().uri(), Device.Type.SWITCH,
107 mrf, hwVer, swVer, serialNum,
108 new ChassisId(), (SparseAnnotations) device.annotations());
109 }
110
111 @Override
112 public List<PortDescription> discoverPortDetails() {
113 DriverHandler handler = handler();
114 RestSBController controller = checkNotNull(handler.get(RestSBController.class));
115 DeviceId deviceId = handler.data().deviceId();
116
117 ArrayList<String> cmd = new ArrayList<>();
118 cmd.add(SHOW_INTERFACES_CMD);
119
120 String req = NxApiRequest.generate(cmd, NxApiRequest.CommandType.CLI);
Eunjin Choi8fcdf282017-05-17 16:56:52 +0900121 String response = NxApiRequest.post(controller, deviceId, req);
122
123 // parse interface information from response
124 List<PortDescription> ports = Lists.newArrayList();
125 try {
126 ObjectMapper om = new ObjectMapper();
127 JsonNode json = om.readTree(response);
128
129 JsonNode interfaces = json.findValue(ROW_INTERFACE);
130 if (interfaces != null) {
131 interfaces.forEach(itf -> {
132 String ifName = itf.get(INTERFACE).asText();
133 if (ifName.startsWith(ETH)) {
134 String ifNum = ifName.substring(ETHERNET.length()).replace(SLASH, ZERO);
135 boolean state = itf.get(STATE).asText().equals(UP);
136 long portSpeed = itf.get(ETH_BW).asLong() / ONE_THOUSAND; //in Mbps
137 DefaultAnnotations.Builder annotations = DefaultAnnotations.builder()
138 .set(AnnotationKeys.PORT_NAME, ifName);
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800139 PortDescription desc = DefaultPortDescription.builder()
140 .withPortNumber(PortNumber.portNumber(ifNum))
141 .isEnabled(state)
142 .type(Port.Type.FIBER).portSpeed(portSpeed).annotations(annotations.build())
143 .build();
Eunjin Choi8fcdf282017-05-17 16:56:52 +0900144 ports.add(desc);
145 }
146 });
147 }
148 } catch (IOException e) {
149 log.error("Failed to to retrieve Interfaces {}", e);
150 }
151
152 return ports;
153 }
154}