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; |
Michele Santuari | f594537 | 2017-01-19 16:39:21 +0100 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableList; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 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; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 28 | import org.onosproject.netconf.NetconfSession; |
Yuta HIGUCHI | 0184a7b | 2017-03-31 13:13:58 -0700 | [diff] [blame] | 29 | import org.slf4j.Logger; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 30 | |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 31 | import java.io.IOException; |
| 32 | import java.util.List; |
| 33 | |
| 34 | import static com.google.common.base.Preconditions.checkNotNull; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 35 | import static org.onosproject.drivers.juniper.JuniperUtils.REQ_IF_INFO; |
| 36 | import static org.onosproject.drivers.juniper.JuniperUtils.REQ_MAC_ADD_INFO; |
| 37 | import static org.onosproject.drivers.juniper.JuniperUtils.REQ_SYS_INFO; |
| 38 | import static org.onosproject.drivers.juniper.JuniperUtils.requestBuilder; |
Yuta HIGUCHI | 0184a7b | 2017-03-31 13:13:58 -0700 | [diff] [blame] | 39 | import static org.onosproject.drivers.utilities.XmlConfigParser.loadXmlString; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 40 | import static org.slf4j.LoggerFactory.getLogger; |
| 41 | |
| 42 | /** |
| 43 | * Retrieve the Device information and ports via NETCONF for Juniper Router. |
| 44 | * Tested with MX240 junos 14.2 |
| 45 | */ |
| 46 | @Beta |
| 47 | public class DeviceDiscoveryJuniperImpl extends AbstractHandlerBehaviour |
| 48 | implements DeviceDescriptionDiscovery { |
| 49 | |
Yuta HIGUCHI | 0184a7b | 2017-03-31 13:13:58 -0700 | [diff] [blame] | 50 | private final Logger log = getLogger(getClass()); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 51 | |
| 52 | @Override |
| 53 | public DeviceDescription discoverDeviceDetails() { |
Michele Santuari | f594537 | 2017-01-19 16:39:21 +0100 | [diff] [blame] | 54 | DeviceId devId = handler().data().deviceId(); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 55 | NetconfController controller = checkNotNull(handler().get(NetconfController.class)); |
Michele Santuari | f594537 | 2017-01-19 16:39:21 +0100 | [diff] [blame] | 56 | NetconfSession session = controller.getDevicesMap().get(devId).getSession(); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 57 | String sysInfo; |
| 58 | String chassis; |
| 59 | try { |
| 60 | sysInfo = session.get(requestBuilder(REQ_SYS_INFO)); |
| 61 | chassis = session.get(requestBuilder(REQ_MAC_ADD_INFO)); |
| 62 | } catch (IOException e) { |
Michele Santuari | f594537 | 2017-01-19 16:39:21 +0100 | [diff] [blame] | 63 | log.warn("Failed to retrieve device details for {}", devId); |
| 64 | return null; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 65 | } |
Yuta HIGUCHI | 0184a7b | 2017-03-31 13:13:58 -0700 | [diff] [blame] | 66 | log.trace("Device {} system-information {}", devId, sysInfo); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 67 | DeviceDescription description = |
Yuta HIGUCHI | 0184a7b | 2017-03-31 13:13:58 -0700 | [diff] [blame] | 68 | JuniperUtils.parseJuniperDescription(devId, |
| 69 | loadXmlString(sysInfo), |
| 70 | chassis); |
| 71 | log.debug("Device {} description {}", devId, description); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 72 | return description; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public List<PortDescription> discoverPortDetails() { |
Michele Santuari | f594537 | 2017-01-19 16:39:21 +0100 | [diff] [blame] | 77 | DeviceId devId = handler().data().deviceId(); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 78 | NetconfController controller = checkNotNull(handler().get(NetconfController.class)); |
Michele Santuari | f594537 | 2017-01-19 16:39:21 +0100 | [diff] [blame] | 79 | NetconfSession session = controller.getDevicesMap().get(devId).getSession(); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 80 | String reply; |
| 81 | try { |
| 82 | reply = session.get(requestBuilder(REQ_IF_INFO)); |
| 83 | } catch (IOException e) { |
Michele Santuari | f594537 | 2017-01-19 16:39:21 +0100 | [diff] [blame] | 84 | log.warn("Failed to retrieve ports for device {}", devId); |
| 85 | return ImmutableList.of(); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 86 | } |
Yuta HIGUCHI | 0184a7b | 2017-03-31 13:13:58 -0700 | [diff] [blame] | 87 | log.trace("Device {} interface-information {}", devId, reply); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 88 | List<PortDescription> descriptions = |
Yuta HIGUCHI | 0184a7b | 2017-03-31 13:13:58 -0700 | [diff] [blame] | 89 | JuniperUtils.parseJuniperPorts(loadXmlString(reply)); |
| 90 | log.debug("Device {} Discovered ports {}", devId, descriptions); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 91 | return descriptions; |
| 92 | } |
| 93 | } |