Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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.juniper; |
| 18 | |
| 19 | |
| 20 | import com.google.common.annotations.Beta; |
| 21 | import org.onosproject.drivers.utilities.XmlConfigParser; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.device.DeviceDescription; |
| 24 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
| 25 | import org.onosproject.net.device.PortDescription; |
| 26 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 27 | import org.onosproject.netconf.NetconfController; |
| 28 | import org.onosproject.netconf.NetconfException; |
| 29 | import org.onosproject.netconf.NetconfSession; |
| 30 | |
| 31 | import java.io.ByteArrayInputStream; |
| 32 | import java.io.IOException; |
| 33 | import java.util.List; |
| 34 | |
| 35 | import static com.google.common.base.Preconditions.checkNotNull; |
| 36 | import static org.onosproject.drivers.juniper.JuniperUtils.FAILED_CFG; |
| 37 | import static org.onosproject.drivers.juniper.JuniperUtils.REQ_IF_INFO; |
| 38 | import static org.onosproject.drivers.juniper.JuniperUtils.REQ_MAC_ADD_INFO; |
| 39 | import static org.onosproject.drivers.juniper.JuniperUtils.REQ_SYS_INFO; |
| 40 | import static org.onosproject.drivers.juniper.JuniperUtils.requestBuilder; |
| 41 | import static org.slf4j.LoggerFactory.getLogger; |
| 42 | |
| 43 | /** |
| 44 | * Retrieve the Device information and ports via NETCONF for Juniper Router. |
| 45 | * Tested with MX240 junos 14.2 |
| 46 | */ |
| 47 | @Beta |
| 48 | public class DeviceDiscoveryJuniperImpl extends AbstractHandlerBehaviour |
| 49 | implements DeviceDescriptionDiscovery { |
| 50 | |
| 51 | public final org.slf4j.Logger log = getLogger(getClass()); |
| 52 | |
| 53 | @Override |
| 54 | public DeviceDescription discoverDeviceDetails() { |
| 55 | DeviceId deviceId = handler().data().deviceId(); |
| 56 | NetconfController controller = checkNotNull(handler().get(NetconfController.class)); |
| 57 | NetconfSession session = controller.getDevicesMap().get(deviceId).getSession(); |
| 58 | String sysInfo; |
| 59 | String chassis; |
| 60 | try { |
| 61 | sysInfo = session.get(requestBuilder(REQ_SYS_INFO)); |
| 62 | chassis = session.get(requestBuilder(REQ_MAC_ADD_INFO)); |
| 63 | } catch (IOException e) { |
| 64 | throw new RuntimeException(new NetconfException(FAILED_CFG, e)); |
| 65 | } |
| 66 | DeviceDescription description = |
| 67 | JuniperUtils.parseJuniperDescription(deviceId, XmlConfigParser. |
| 68 | loadXml(new ByteArrayInputStream(sysInfo.getBytes())), chassis); |
| 69 | log.debug("Device description {}", description); |
| 70 | return description; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public List<PortDescription> discoverPortDetails() { |
| 75 | NetconfController controller = checkNotNull(handler().get(NetconfController.class)); |
| 76 | NetconfSession session = controller.getDevicesMap().get(handler().data().deviceId()).getSession(); |
| 77 | String reply; |
| 78 | try { |
| 79 | reply = session.get(requestBuilder(REQ_IF_INFO)); |
| 80 | } catch (IOException e) { |
| 81 | throw new RuntimeException(new NetconfException(FAILED_CFG, e)); |
| 82 | } |
| 83 | List<PortDescription> descriptions = |
| 84 | JuniperUtils.parseJuniperPorts(XmlConfigParser. |
| 85 | loadXml(new ByteArrayInputStream(reply.getBytes()))); |
| 86 | log.debug("Discovered ports {}", descriptions); |
| 87 | return descriptions; |
| 88 | } |
| 89 | } |