blob: fc53137e7836b6203ddeb948b70fbeba882e8e67 [file] [log] [blame]
Michele Santuari21c14012016-11-14 13:31:33 +01001/*
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
17package org.onosproject.drivers.juniper;
18
19
20import com.google.common.annotations.Beta;
Michele Santuarif5945372017-01-19 16:39:21 +010021import com.google.common.collect.ImmutableList;
Michele Santuari21c14012016-11-14 13:31:33 +010022import org.onosproject.drivers.utilities.XmlConfigParser;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.device.DeviceDescription;
25import org.onosproject.net.device.DeviceDescriptionDiscovery;
26import org.onosproject.net.device.PortDescription;
27import org.onosproject.net.driver.AbstractHandlerBehaviour;
28import org.onosproject.netconf.NetconfController;
Michele Santuari21c14012016-11-14 13:31:33 +010029import org.onosproject.netconf.NetconfSession;
30
31import java.io.ByteArrayInputStream;
32import java.io.IOException;
33import java.util.List;
34
35import static com.google.common.base.Preconditions.checkNotNull;
Michele Santuari21c14012016-11-14 13:31:33 +010036import static org.onosproject.drivers.juniper.JuniperUtils.REQ_IF_INFO;
37import static org.onosproject.drivers.juniper.JuniperUtils.REQ_MAC_ADD_INFO;
38import static org.onosproject.drivers.juniper.JuniperUtils.REQ_SYS_INFO;
39import static org.onosproject.drivers.juniper.JuniperUtils.requestBuilder;
40import 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
47public class DeviceDiscoveryJuniperImpl extends AbstractHandlerBehaviour
48 implements DeviceDescriptionDiscovery {
49
50 public final org.slf4j.Logger log = getLogger(getClass());
51
52 @Override
53 public DeviceDescription discoverDeviceDetails() {
Michele Santuarif5945372017-01-19 16:39:21 +010054 DeviceId devId = handler().data().deviceId();
Michele Santuari21c14012016-11-14 13:31:33 +010055 NetconfController controller = checkNotNull(handler().get(NetconfController.class));
Michele Santuarif5945372017-01-19 16:39:21 +010056 NetconfSession session = controller.getDevicesMap().get(devId).getSession();
Michele Santuari21c14012016-11-14 13:31:33 +010057 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 Santuarif5945372017-01-19 16:39:21 +010063 log.warn("Failed to retrieve device details for {}", devId);
64 return null;
Michele Santuari21c14012016-11-14 13:31:33 +010065 }
66 DeviceDescription description =
Michele Santuarif5945372017-01-19 16:39:21 +010067 JuniperUtils.parseJuniperDescription(devId, XmlConfigParser.
Michele Santuari21c14012016-11-14 13:31:33 +010068 loadXml(new ByteArrayInputStream(sysInfo.getBytes())), chassis);
69 log.debug("Device description {}", description);
70 return description;
71 }
72
73 @Override
74 public List<PortDescription> discoverPortDetails() {
Michele Santuarif5945372017-01-19 16:39:21 +010075 DeviceId devId = handler().data().deviceId();
Michele Santuari21c14012016-11-14 13:31:33 +010076 NetconfController controller = checkNotNull(handler().get(NetconfController.class));
Michele Santuarif5945372017-01-19 16:39:21 +010077 NetconfSession session = controller.getDevicesMap().get(devId).getSession();
Michele Santuari21c14012016-11-14 13:31:33 +010078 String reply;
79 try {
80 reply = session.get(requestBuilder(REQ_IF_INFO));
81 } catch (IOException e) {
Michele Santuarif5945372017-01-19 16:39:21 +010082 log.warn("Failed to retrieve ports for device {}", devId);
83 return ImmutableList.of();
Michele Santuari21c14012016-11-14 13:31:33 +010084 }
85 List<PortDescription> descriptions =
86 JuniperUtils.parseJuniperPorts(XmlConfigParser.
87 loadXml(new ByteArrayInputStream(reply.getBytes())));
88 log.debug("Discovered ports {}", descriptions);
89 return descriptions;
90 }
91}