ivoutsas | e78f988 | 2016-06-28 23:32:14 +0300 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.drivers.cisco; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | |
| 21 | import org.onosproject.net.Device; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 24 | import org.onosproject.net.device.DeviceDescription; |
| 25 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
| 26 | import org.onosproject.net.device.DeviceService; |
| 27 | import org.onosproject.net.device.PortDescription; |
| 28 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 29 | import org.onosproject.netconf.NetconfController; |
| 30 | import org.onosproject.netconf.NetconfException; |
| 31 | import org.onosproject.netconf.NetconfSession; |
| 32 | import org.slf4j.Logger; |
| 33 | import java.io.IOException; |
| 34 | import java.util.List; |
| 35 | import static com.google.common.base.Preconditions.checkNotNull; |
| 36 | import static org.slf4j.LoggerFactory.getLogger; |
| 37 | |
| 38 | public 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 | } |