blob: d52c4701e475bb13289d691fc1f7fb874fa61e6d [file] [log] [blame]
ivoutsase78f9882016-06-28 23:32:14 +03001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
ivoutsase78f9882016-06-28 23:32:14 +03003 *
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;
18
19import com.google.common.collect.ImmutableList;
20
21import org.onosproject.net.Device;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.device.DefaultDeviceDescription;
24import org.onosproject.net.device.DeviceDescription;
25import org.onosproject.net.device.DeviceDescriptionDiscovery;
26import org.onosproject.net.device.DeviceService;
27import org.onosproject.net.device.PortDescription;
28import org.onosproject.net.driver.AbstractHandlerBehaviour;
29import org.onosproject.netconf.NetconfController;
30import org.onosproject.netconf.NetconfException;
31import org.onosproject.netconf.NetconfSession;
32import org.slf4j.Logger;
ivoutsase78f9882016-06-28 23:32:14 +030033import java.util.List;
34import static com.google.common.base.Preconditions.checkNotNull;
35import static org.slf4j.LoggerFactory.getLogger;
36
37public class CiscoIosDeviceDescription extends AbstractHandlerBehaviour
38 implements DeviceDescriptionDiscovery {
39
40
41 private final Logger log = getLogger(getClass());
42 private String version;
43 private String interfaces;
44
45 @Override
46 public DeviceDescription discoverDeviceDetails() {
47 NetconfController controller = checkNotNull(handler().get(NetconfController.class));
48 NetconfSession session = controller.getDevicesMap().get(handler().data().deviceId()).getSession();
49 try {
50 version = session.get(showVersionRequestBuilder());
Yuta HIGUCHI234eaf32017-09-06 13:45:05 -070051 } catch (NetconfException e) {
Ray Milkey986a47a2018-01-25 11:38:51 -080052 throw new IllegalStateException(new NetconfException("Failed to retrieve version info.", e));
ivoutsase78f9882016-06-28 23:32:14 +030053 }
54
55 String[] details = TextBlockParserCisco.parseCiscoIosDeviceDetails(version);
56
57 DeviceService deviceService = checkNotNull(handler().get(DeviceService.class));
58 DeviceId deviceId = handler().data().deviceId();
59 Device device = deviceService.getDevice(deviceId);
60
61 return new DefaultDeviceDescription(device.id().uri(), Device.Type.SWITCH,
62 details[0], details[1],
63 details[2], details[3],
64 device.chassisId());
65 }
66
67 @Override
68 public List<PortDescription> discoverPortDetails() {
69 NetconfController controller = checkNotNull(handler().get(NetconfController.class));
70 NetconfSession session = controller.getDevicesMap().get(handler().data().deviceId()).getSession();
71 try {
72 interfaces = session.get(showInterfacesRequestBuilder());
Yuta HIGUCHI234eaf32017-09-06 13:45:05 -070073 } catch (NetconfException e) {
ivoutsase78f9882016-06-28 23:32:14 +030074 log.error("Failed to retrieve Interfaces");
75 return ImmutableList.of();
76 }
77 return ImmutableList.copyOf(TextBlockParserCisco.parseCiscoIosPorts(interfaces));
78 }
79
80 /**
81 * Builds a request crafted to get the configuration required to create
82 * details descriptions for the device.
83 *
84 * @return The request string.
85 */
86 private String showVersionRequestBuilder() {
87 StringBuilder rpc = new StringBuilder("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">");
88 rpc.append("<get>");
89 rpc.append("<filter>");
90 rpc.append("<config-format-text-block>");
91 rpc.append("<text-filter-spec> | include exp_to_match_run_conf </text-filter-spec>");
92 rpc.append("</config-format-text-block>");
93 rpc.append("<oper-data-format-text-block>");
94 rpc.append("<show>version</show>");
95 rpc.append("</oper-data-format-text-block>");
96 rpc.append("</filter>");
97 rpc.append("</get>");
98 rpc.append("</rpc>]]>]]>");
99 return rpc.toString();
100 }
101
102 /**
103 * Builds a request crafted to get the configuration required to create
104 * details descriptions for the device.
105 *
106 * @return The request string.
107 */
108 private String showInterfacesRequestBuilder() {
109 //Message ID is injected later.
110 StringBuilder rpc = new StringBuilder("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">");
111 rpc.append("<get>");
112 rpc.append("<filter>");
113 rpc.append("<config-format-text-block>");
114 rpc.append("<text-filter-spec> | include exp_to_match_run_conf </text-filter-spec>");
115 rpc.append("</config-format-text-block>");
116 rpc.append("<oper-data-format-text-block>");
117 rpc.append("<show>interfaces</show>");
118 rpc.append("</oper-data-format-text-block>");
119 rpc.append("</filter>");
120 rpc.append("</get>");
121 rpc.append("</rpc>]]>]]>");
122 return rpc.toString();
123 }
124
125}