blob: 92a9b7f5ebaae02caa298810ad8ec3d3e9382670 [file] [log] [blame]
Eunjin Choi8fcdf282017-05-17 16:56:52 +09001/*
2 * Copyright 2017-present Open Networking Laboratory
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.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";
56
57 private static final String SHOW_INTERFACES_CMD = "show interface";
58 private static final String SHOW_VERSION_CMD = "show version";
59
60 private static final String MANUFACTURER = "manufacturer";
61 private static final String CHASSIS_ID = "chassis_id";
62 private static final String KICKSTART_VER = "kickstart_ver_str";
63 private static final String ROW_INTERFACE = "ROW_interface";
64 private static final String INTERFACE = "interface";
65 private static final String ETH = "Eth";
66 private static final String ETHERNET = "Ethernet";
67 private static final String STATE = "state";
68 private static final String UP = "up";
69 private static final String ETH_BW = "eth_bw";
70 private static final String SLASH = "/";
71 private static final String ZERO = "0";
72 private static final int ONE_THOUSAND = 1000;
73
74
75 @Override
76 public DeviceDescription discoverDeviceDetails() {
77 DriverHandler handler = handler();
78 RestSBController controller = checkNotNull(handler.get(RestSBController.class));
79 DeviceId deviceId = handler.data().deviceId();
80
81 ArrayList<String> cmd = new ArrayList<>();
82 cmd.add(SHOW_VERSION_CMD);
83
84 String req = NxApiRequest.generate(cmd, NxApiRequest.CommandType.CLI);
85
86 String response = NxApiRequest.post(controller, deviceId, req);
87
88 String mrf = UNKNOWN;
89 String hwVer = UNKNOWN;
90 String swVer = UNKNOWN;
91 String serialNum = UNKNOWN;
92
93 try {
94 ObjectMapper om = new ObjectMapper();
95 JsonNode json = om.readTree(response);
96
97 JsonNode body = json.findValue("body");
98 if (body != null) {
99 mrf = body.get(MANUFACTURER).asText();
100 hwVer = body.get(CHASSIS_ID).asText();
101 swVer = body.get(KICKSTART_VER).asText();
102 }
103 } catch (IOException e) {
104 log.error("Failed to to retrieve Device Information {}", e);
105 }
106
107 DeviceService deviceService = checkNotNull(handler().get(DeviceService.class));
108 Device device = deviceService.getDevice(deviceId);
109 return new DefaultDeviceDescription(device.id().uri(), Device.Type.SWITCH,
110 mrf, hwVer, swVer, serialNum,
111 new ChassisId(), (SparseAnnotations) device.annotations());
112 }
113
114 @Override
115 public List<PortDescription> discoverPortDetails() {
116 DriverHandler handler = handler();
117 RestSBController controller = checkNotNull(handler.get(RestSBController.class));
118 DeviceId deviceId = handler.data().deviceId();
119
120 ArrayList<String> cmd = new ArrayList<>();
121 cmd.add(SHOW_INTERFACES_CMD);
122
123 String req = NxApiRequest.generate(cmd, NxApiRequest.CommandType.CLI);
124
125 String response = NxApiRequest.post(controller, deviceId, req);
126
127 // parse interface information from response
128 List<PortDescription> ports = Lists.newArrayList();
129 try {
130 ObjectMapper om = new ObjectMapper();
131 JsonNode json = om.readTree(response);
132
133 JsonNode interfaces = json.findValue(ROW_INTERFACE);
134 if (interfaces != null) {
135 interfaces.forEach(itf -> {
136 String ifName = itf.get(INTERFACE).asText();
137 if (ifName.startsWith(ETH)) {
138 String ifNum = ifName.substring(ETHERNET.length()).replace(SLASH, ZERO);
139 boolean state = itf.get(STATE).asText().equals(UP);
140 long portSpeed = itf.get(ETH_BW).asLong() / ONE_THOUSAND; //in Mbps
141 DefaultAnnotations.Builder annotations = DefaultAnnotations.builder()
142 .set(AnnotationKeys.PORT_NAME, ifName);
143 PortDescription desc = new DefaultPortDescription(PortNumber.portNumber(ifNum), state,
144 Port.Type.FIBER, portSpeed, annotations.build());
145 ports.add(desc);
146 }
147 });
148 }
149 } catch (IOException e) {
150 log.error("Failed to to retrieve Interfaces {}", e);
151 }
152
153 return ports;
154 }
155}