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