blob: e277d7edc5048f6970b431be8878bedf702c826a [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;
21import org.onosproject.drivers.utilities.XmlConfigParser;
22import 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;
28import org.onosproject.netconf.NetconfException;
29import 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;
36import static org.onosproject.drivers.juniper.JuniperUtils.FAILED_CFG;
37import static org.onosproject.drivers.juniper.JuniperUtils.REQ_IF_INFO;
38import static org.onosproject.drivers.juniper.JuniperUtils.REQ_MAC_ADD_INFO;
39import static org.onosproject.drivers.juniper.JuniperUtils.REQ_SYS_INFO;
40import static org.onosproject.drivers.juniper.JuniperUtils.requestBuilder;
41import 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
48public 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}