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