blob: 2b99faabc84e90a4ac70ebaf946f74f07bba7f04 [file] [log] [blame]
Michele Santuari21c14012016-11-14 13:31:33 +01001/*
2 * Copyright 2016-present 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
Michele Santuari21c14012016-11-14 13:31:33 +010019import com.google.common.annotations.Beta;
Michele Santuarif5945372017-01-19 16:39:21 +010020import com.google.common.collect.ImmutableSet;
Michele Santuari21c14012016-11-14 13:31:33 +010021import com.google.common.collect.Iterables;
22import org.onosproject.drivers.utilities.XmlConfigParser;
23import org.onosproject.net.Device;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.Port;
26import org.onosproject.net.behaviour.LinkDiscovery;
27import org.onosproject.net.device.DeviceService;
28import org.onosproject.net.driver.AbstractHandlerBehaviour;
29import org.onosproject.net.link.LinkDescription;
30import org.onosproject.netconf.NetconfController;
Michele Santuari21c14012016-11-14 13:31:33 +010031import org.onosproject.netconf.NetconfSession;
32import org.slf4j.Logger;
33
34import java.io.ByteArrayInputStream;
35import java.io.IOException;
36import java.util.HashSet;
37import java.util.Optional;
38import java.util.Set;
39
40import static com.google.common.base.Preconditions.checkNotNull;
41import static org.onosproject.drivers.juniper.JuniperUtils.LinkAbstraction;
42import static org.onosproject.drivers.juniper.JuniperUtils.parseJuniperLldp;
43import static org.onosproject.drivers.juniper.JuniperUtils.requestBuilder;
44import static org.onosproject.net.AnnotationKeys.PORT_NAME;
45import static org.onosproject.drivers.juniper.JuniperUtils.REQ_LLDP_NBR_INFO;
Michele Santuari21c14012016-11-14 13:31:33 +010046import static org.slf4j.LoggerFactory.getLogger;
47
48
49/**
50 * Retrieve Links discovered by the device LLDP.
51 * Tested with MX240 junos 14.2
52 */
53@Beta
54public class LinkDiscoveryJuniperImpl extends AbstractHandlerBehaviour
55 implements LinkDiscovery {
56
57 private final Logger log = getLogger(getClass());
58
59 @Override
60 public Set<LinkDescription> getLinks() {
61 DeviceId localDeviceId = this.handler().data().deviceId();
62 NetconfController controller =
63 checkNotNull(handler().get(NetconfController.class));
64 NetconfSession session =
65 controller.getDevicesMap().get(localDeviceId).getSession();
66
67 String reply;
68 try {
69 reply = session.get(requestBuilder(REQ_LLDP_NBR_INFO));
70 } catch (IOException e) {
Michele Santuarif5945372017-01-19 16:39:21 +010071 log.warn("Failed to retrieve ports for device {}", localDeviceId);
72 return ImmutableSet.of();
Michele Santuari21c14012016-11-14 13:31:33 +010073 }
74 log.debug("Reply from device {} : {}", localDeviceId, reply);
75 Set<LinkAbstraction> linkAbstractions = parseJuniperLldp(
76 XmlConfigParser.loadXml(new ByteArrayInputStream(reply.getBytes())));
77 log.debug("Set of LinkAbstraction discovered {}", linkAbstractions);
78
79 DeviceService deviceService = this.handler().get(DeviceService.class);
80 Set<LinkDescription> descriptions = new HashSet<>();
81
82 //for each lldp neighbor create two LinkDescription
83 for (LinkAbstraction linkAbs : linkAbstractions) {
84
85 //find source port by local port name
86 Optional<Port> localPort = deviceService.getPorts(localDeviceId).stream()
87 .filter(port -> {
88 if (linkAbs.localPortName.equals(
89 port.annotations().value(PORT_NAME))) {
90 return true;
91 }
92 return false;
93 }).findAny();
94 if (!localPort.isPresent()) {
95 log.warn("Port name {} does not exist in device {}",
96 linkAbs.localPortName, localDeviceId);
97 continue;
98 }
99 //find destination device by remote chassis id
100 com.google.common.base.Optional<Device> dev = Iterables.tryFind(
101 deviceService.getAvailableDevices(),
102 input -> input.chassisId().equals(linkAbs.remoteChassisId));
103
104 if (!dev.isPresent()) {
105 log.warn("Device with chassis ID {} does not exist",
106 linkAbs.remoteChassisId);
107 continue;
108 }
109 Device remoteDevice = dev.get();
110
111 //find destination port by interface index
112 Optional<Port> remotePort = deviceService.getPorts(remoteDevice.id())
113 .stream().filter(port -> {
114 if (port.annotations().value("index") != null &&
115 Integer.parseInt(port.annotations().value("index"))
116 == linkAbs.remotePortIndex) {
117 return true;
118 }
119 return false;
120 }).findAny();
121 if (!remotePort.isPresent()) {
122 log.warn("Port with index {} does not exist in device {}",
123 linkAbs.remotePortIndex, remoteDevice.id());
124 continue;
125 }
126
127 JuniperUtils.createBiDirLinkDescription(localDeviceId,
128 localPort.get(),
129 remoteDevice.id(),
130 remotePort.get(),
131 descriptions);
132
133 }
134 return descriptions;
135 }
136}