blob: c60c4489262b1cfc5ce99cbec5efaa24abdfc32c [file] [log] [blame]
Michele Santuari21c14012016-11-14 13:31:33 +01001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016 Open Networking Foundation
Michele Santuari21c14012016-11-14 13:31:33 +01003 *
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.net.DeviceId;
23import org.onosproject.net.device.DeviceDescription;
24import org.onosproject.net.device.DeviceDescriptionDiscovery;
25import org.onosproject.net.device.PortDescription;
26import org.onosproject.net.driver.AbstractHandlerBehaviour;
27import org.onosproject.netconf.NetconfController;
Yuta HIGUCHI234eaf32017-09-06 13:45:05 -070028import org.onosproject.netconf.NetconfException;
Michele Santuari21c14012016-11-14 13:31:33 +010029import org.onosproject.netconf.NetconfSession;
Yuta HIGUCHI0184a7b2017-03-31 13:13:58 -070030import org.slf4j.Logger;
Michele Santuari21c14012016-11-14 13:31:33 +010031
Michele Santuari21c14012016-11-14 13:31:33 +010032import java.util.List;
33
34import static com.google.common.base.Preconditions.checkNotNull;
Michele Santuari21c14012016-11-14 13:31:33 +010035import static org.onosproject.drivers.juniper.JuniperUtils.REQ_IF_INFO;
36import static org.onosproject.drivers.juniper.JuniperUtils.REQ_MAC_ADD_INFO;
37import static org.onosproject.drivers.juniper.JuniperUtils.REQ_SYS_INFO;
38import static org.onosproject.drivers.juniper.JuniperUtils.requestBuilder;
Yuta HIGUCHI0184a7b2017-03-31 13:13:58 -070039import static org.onosproject.drivers.utilities.XmlConfigParser.loadXmlString;
Michele Santuari21c14012016-11-14 13:31:33 +010040import 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
Yuta HIGUCHI0184a7b2017-03-31 13:13:58 -070050 private final Logger log = getLogger(getClass());
Michele Santuari21c14012016-11-14 13:31:33 +010051
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));
Yuta HIGUCHI234eaf32017-09-06 13:45:05 -070062 } catch (NetconfException 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 }
Yuta HIGUCHI0184a7b2017-03-31 13:13:58 -070066 log.trace("Device {} system-information {}", devId, sysInfo);
Michele Santuari21c14012016-11-14 13:31:33 +010067 DeviceDescription description =
Yuta HIGUCHI0184a7b2017-03-31 13:13:58 -070068 JuniperUtils.parseJuniperDescription(devId,
69 loadXmlString(sysInfo),
70 chassis);
71 log.debug("Device {} description {}", devId, description);
Michele Santuari21c14012016-11-14 13:31:33 +010072 return description;
73 }
74
75 @Override
76 public List<PortDescription> discoverPortDetails() {
Michele Santuarif5945372017-01-19 16:39:21 +010077 DeviceId devId = handler().data().deviceId();
Michele Santuari21c14012016-11-14 13:31:33 +010078 NetconfController controller = checkNotNull(handler().get(NetconfController.class));
Michele Santuarif5945372017-01-19 16:39:21 +010079 NetconfSession session = controller.getDevicesMap().get(devId).getSession();
Michele Santuari21c14012016-11-14 13:31:33 +010080 String reply;
81 try {
82 reply = session.get(requestBuilder(REQ_IF_INFO));
Yuta HIGUCHI234eaf32017-09-06 13:45:05 -070083 } catch (NetconfException e) {
Michele Santuarif5945372017-01-19 16:39:21 +010084 log.warn("Failed to retrieve ports for device {}", devId);
85 return ImmutableList.of();
Michele Santuari21c14012016-11-14 13:31:33 +010086 }
Yuta HIGUCHI0184a7b2017-03-31 13:13:58 -070087 log.trace("Device {} interface-information {}", devId, reply);
Michele Santuari21c14012016-11-14 13:31:33 +010088 List<PortDescription> descriptions =
Yuta HIGUCHI0184a7b2017-03-31 13:13:58 -070089 JuniperUtils.parseJuniperPorts(loadXmlString(reply));
90 log.debug("Device {} Discovered ports {}", devId, descriptions);
Michele Santuari21c14012016-11-14 13:31:33 +010091 return descriptions;
92 }
93}