sunish vk | 7bdf4d4 | 2016-06-24 12:29:43 +0530 | [diff] [blame] | 1 | /* |
| 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 | package org.onosproject.isis.controller.impl; |
| 17 | |
| 18 | import org.onlab.packet.Ip4Address; |
| 19 | import org.onlab.util.Bandwidth; |
| 20 | import org.onosproject.isis.controller.IsisNetworkType; |
| 21 | import org.onosproject.isis.controller.LspWrapper; |
| 22 | import org.onosproject.isis.controller.impl.topology.DefaultIsisLink; |
| 23 | import org.onosproject.isis.controller.impl.topology.DefaultIsisLinkInformation; |
| 24 | import org.onosproject.isis.controller.impl.topology.DefaultIsisLinkTed; |
| 25 | import org.onosproject.isis.controller.impl.topology.DefaultIsisRouter; |
| 26 | import org.onosproject.isis.controller.impl.topology.TopologyForDeviceAndLinkImpl; |
| 27 | import org.onosproject.isis.controller.topology.IsisLink; |
| 28 | import org.onosproject.isis.controller.topology.IsisLinkTed; |
| 29 | import org.onosproject.isis.controller.topology.IsisRouter; |
| 30 | import org.onosproject.isis.controller.topology.LinkInformation; |
| 31 | import org.onosproject.isis.io.isispacket.pdu.LsPdu; |
| 32 | import org.onosproject.isis.io.isispacket.tlv.IsExtendedReachability; |
| 33 | import org.onosproject.isis.io.isispacket.tlv.IsisTlv; |
| 34 | import org.onosproject.isis.io.isispacket.tlv.NeighborForExtendedIs; |
| 35 | import org.onosproject.isis.io.isispacket.tlv.subtlv.AdministrativeGroup; |
| 36 | import org.onosproject.isis.io.isispacket.tlv.subtlv.InterfaceIpAddress; |
| 37 | import org.onosproject.isis.io.isispacket.tlv.subtlv.MaximumBandwidth; |
| 38 | import org.onosproject.isis.io.isispacket.tlv.subtlv.MaximumReservableBandwidth; |
| 39 | import org.onosproject.isis.io.isispacket.tlv.subtlv.NeighborIpAddress; |
| 40 | import org.onosproject.isis.io.isispacket.tlv.subtlv.TrafficEngineeringMetric; |
| 41 | import org.onosproject.isis.io.isispacket.tlv.subtlv.TrafficEngineeringSubTlv; |
| 42 | import org.onosproject.isis.io.isispacket.tlv.subtlv.UnreservedBandwidth; |
| 43 | import org.onosproject.isis.io.util.IsisConstants; |
| 44 | import org.onosproject.isis.io.util.IsisUtil; |
| 45 | import org.slf4j.Logger; |
| 46 | import org.slf4j.LoggerFactory; |
| 47 | |
| 48 | import java.util.ArrayList; |
| 49 | import java.util.LinkedHashMap; |
| 50 | import java.util.List; |
| 51 | import java.util.Map; |
| 52 | import java.util.concurrent.BlockingQueue; |
| 53 | |
| 54 | /** |
| 55 | * Representation of LSP event consumer. |
| 56 | */ |
| 57 | public class LspEventConsumer implements Runnable { |
| 58 | private static final Logger log = LoggerFactory.getLogger(LspEventConsumer.class); |
| 59 | private BlockingQueue queue = null; |
| 60 | private Controller controller = null; |
| 61 | private TopologyForDeviceAndLinkImpl deviceAndLink = new TopologyForDeviceAndLinkImpl(); |
| 62 | private Map<String, IsisRouter> isisRouterDetails = new LinkedHashMap<>(); |
| 63 | |
| 64 | /** |
| 65 | * Creates an instance of this. |
| 66 | * |
| 67 | * @param queue blocking queue instance |
| 68 | * @param controller controller instance |
| 69 | */ |
| 70 | public LspEventConsumer(BlockingQueue queue, Controller controller) { |
| 71 | this.queue = queue; |
| 72 | this.controller = controller; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public void run() { |
| 77 | try { |
| 78 | while (true) { |
| 79 | if (!queue.isEmpty()) { |
| 80 | LspWrapper wrapper = (LspWrapper) queue.take(); |
| 81 | LsPdu lsPdu = (LsPdu) wrapper.lsPdu(); |
| 82 | if (wrapper.lspProcessing().equals(IsisConstants.LSPREMOVED)) { |
| 83 | callTopologyToRemoveInfo(lsPdu); |
| 84 | } else if (wrapper.lspProcessing().equals(IsisConstants.LSPADDED)) { |
| 85 | callTopologyToSendInfo(lsPdu, wrapper.isisInterface().networkType(), |
| 86 | wrapper.isisInterface().systemId() + ".00"); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | } catch (Exception e) { |
| 91 | log.debug("Error::LspsForProvider::{}", e.getMessage()); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Sends topology information to core. |
| 97 | * |
| 98 | * @param lsPdu ls pdu instance |
| 99 | * @param isisNetworkType ISIS network type |
| 100 | * @param ownSystemId own system ID |
| 101 | */ |
| 102 | private void callTopologyToSendInfo(LsPdu lsPdu, IsisNetworkType isisNetworkType, |
| 103 | String ownSystemId) { |
| 104 | if ((lsPdu.lspId().equals(ownSystemId + "-00"))) { |
| 105 | return; |
| 106 | } |
| 107 | sendDeviceInfo(createDeviceInfo(lsPdu)); |
| 108 | |
| 109 | for (IsisTlv isisTlv : lsPdu.tlvs()) { |
| 110 | if (isisTlv instanceof IsExtendedReachability) { |
| 111 | IsExtendedReachability isExtendedReachability = (IsExtendedReachability) isisTlv; |
| 112 | List<NeighborForExtendedIs> neighbours = isExtendedReachability.neighbours(); |
| 113 | for (NeighborForExtendedIs teTlv : neighbours) { |
| 114 | String neighbor = teTlv.neighborId(); |
| 115 | IsisRouter isisRouter = isisRouterDetails.get(neighbor); |
| 116 | if (isisRouter != null) { |
| 117 | IsisRouter sourceRouter = isisRouterDetails.get(IsisUtil.removeTailingZeros(lsPdu.lspId())); |
| 118 | IsisRouter destinationRouter = isisRouter; |
| 119 | if (sourceRouter.isDis()) { |
| 120 | LinkInformation linkInformation = createLinkInfo(sourceRouter.systemId(), |
| 121 | destinationRouter.systemId(), |
| 122 | sourceRouter.interfaceId(), |
| 123 | destinationRouter.interfaceId(), lsPdu); |
| 124 | controller.addLinkDetails(createIsisLink(linkInformation, lsPdu)); |
| 125 | } else if (destinationRouter.isDis()) { |
| 126 | LinkInformation linkInformation1 = createLinkInfo(destinationRouter.systemId(), |
| 127 | sourceRouter.systemId(), |
| 128 | destinationRouter.interfaceId(), |
| 129 | sourceRouter.interfaceId(), lsPdu); |
| 130 | controller.addLinkDetails(createIsisLink(linkInformation1, lsPdu)); |
| 131 | } else { |
| 132 | LinkInformation linkInformation = createLinkInfo(sourceRouter.systemId(), |
| 133 | destinationRouter.systemId(), |
| 134 | sourceRouter.interfaceId(), |
| 135 | destinationRouter.interfaceId(), lsPdu); |
| 136 | controller.addLinkDetails(createIsisLink(linkInformation, lsPdu)); |
| 137 | LinkInformation linkInformation1 = createLinkInfo(destinationRouter.systemId(), |
| 138 | sourceRouter.systemId(), |
| 139 | destinationRouter.interfaceId(), |
| 140 | sourceRouter.interfaceId(), lsPdu); |
| 141 | controller.addLinkDetails(createIsisLink(linkInformation1, lsPdu)); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Removes topology information from core. |
| 151 | * |
| 152 | * @param lsPdu ls pdu instance |
| 153 | */ |
| 154 | private void callTopologyToRemoveInfo(LsPdu lsPdu) { |
| 155 | String routerId = IsisUtil.removeTailingZeros(lsPdu.lspId()); |
| 156 | IsisRouter isisRouter = isisRouterDetails.get(routerId); |
| 157 | removeDeviceInfo(isisRouter); |
| 158 | removeLinkInfo(lsPdu); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Sends the device information to topology provider. |
| 163 | * |
| 164 | * @param isisRouter ISIS router instance |
| 165 | */ |
| 166 | private void sendDeviceInfo(IsisRouter isisRouter) { |
| 167 | if (isisRouter.systemId() != null) { |
| 168 | controller.addDeviceDetails(isisRouter); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Creates Device instance. |
| 174 | * |
| 175 | * @param lsPdu ISIS LSPDU instance |
| 176 | * @return isisRouter isisRouter instance |
| 177 | */ |
| 178 | public IsisRouter createDeviceInfo(LsPdu lsPdu) { |
| 179 | IsisRouter isisRouter = createIsisRouter(lsPdu); |
| 180 | if (isisRouter.systemId() != null) { |
| 181 | isisRouterDetails.put(isisRouter.systemId(), isisRouter); |
| 182 | } |
| 183 | return isisRouter; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Creates ISIS router instance. |
| 188 | * |
| 189 | * @param lsPdu lsp instance |
| 190 | * @return isisRouter instance |
| 191 | */ |
| 192 | private IsisRouter createIsisRouter(LsPdu lsPdu) { |
| 193 | IsisRouter isisRouter = new DefaultIsisRouter(); |
| 194 | if (IsisUtil.checkIsDis(lsPdu.lspId())) { |
| 195 | isisRouter.setDis(true); |
| 196 | } else { |
| 197 | isisRouter.setDis(false); |
| 198 | } |
| 199 | isisRouter.setSystemId(IsisUtil.removeTailingZeros(lsPdu.lspId())); |
| 200 | for (IsisTlv isisTlv : lsPdu.tlvs()) { |
| 201 | if (isisTlv instanceof IsExtendedReachability) { |
| 202 | IsExtendedReachability isExtendedReachability = (IsExtendedReachability) isisTlv; |
| 203 | List<NeighborForExtendedIs> neighbours = isExtendedReachability.neighbours(); |
| 204 | for (NeighborForExtendedIs teTlv : neighbours) { |
| 205 | List<TrafficEngineeringSubTlv> teSubTlvs = teTlv.teSubTlv(); |
| 206 | for (TrafficEngineeringSubTlv teSubTlv : teSubTlvs) { |
| 207 | if (teSubTlv instanceof InterfaceIpAddress) { |
| 208 | InterfaceIpAddress localIpAddress = (InterfaceIpAddress) teSubTlv; |
| 209 | isisRouter.setInterfaceId(localIpAddress.localInterfaceIPAddress()); |
| 210 | } else if (teSubTlv instanceof NeighborIpAddress) { |
| 211 | NeighborIpAddress neighborIpAddress = (NeighborIpAddress) teSubTlv; |
| 212 | isisRouter.setNeighborRouterId(neighborIpAddress.neighborIPAddress()); |
| 213 | } |
| 214 | |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | if (isisRouter.interfaceId() == null) { |
| 220 | isisRouter.setInterfaceId(IsisConstants.DEFAULTIP); |
| 221 | } |
| 222 | if (isisRouter.neighborRouterId() == null) { |
| 223 | isisRouter.setNeighborRouterId(IsisConstants.DEFAULTIP); |
| 224 | } |
| 225 | return isisRouter; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Creates link information. |
| 230 | * |
| 231 | * @param localSystemId local system ID |
| 232 | * @param remoteSystemId remote system ID |
sunish vk | 7bdf4d4 | 2016-06-24 12:29:43 +0530 | [diff] [blame] | 233 | * @param interfaceIp interface address |
| 234 | * @param neighborIp neighbor address |
| 235 | * @param lsPdu link state PDU instance |
| 236 | * @return link information instance |
| 237 | */ |
| 238 | public LinkInformation createLinkInfo(String localSystemId, String remoteSystemId, |
| 239 | Ip4Address interfaceIp, Ip4Address neighborIp, |
| 240 | LsPdu lsPdu) { |
| 241 | |
| 242 | String linkId = "link:" + localSystemId + "-" + remoteSystemId; |
| 243 | LinkInformation linkInformation = new DefaultIsisLinkInformation(); |
| 244 | linkInformation.setInterfaceIp(interfaceIp); |
| 245 | linkInformation.setNeighborIp(neighborIp); |
| 246 | linkInformation.setLinkId(linkId); |
| 247 | linkInformation.setAlreadyCreated(false); |
| 248 | linkInformation.setLinkDestinationId(remoteSystemId); |
| 249 | linkInformation.setLinkSourceId(localSystemId); |
| 250 | |
| 251 | return linkInformation; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Removes the device information from topology provider. |
| 256 | * |
| 257 | * @param isisRouter ISIS router instance |
| 258 | */ |
| 259 | private void removeDeviceInfo(IsisRouter isisRouter) { |
| 260 | if (isisRouter.systemId() != null) { |
| 261 | controller.removeDeviceDetails(isisRouter); |
| 262 | } |
| 263 | isisRouterDetails.remove(isisRouter.systemId()); |
| 264 | } |
| 265 | |
| 266 | |
| 267 | /** |
| 268 | * Removes the link information from topology provider. |
| 269 | * |
| 270 | * @param lsPdu ls pdu instance |
| 271 | */ |
| 272 | private void removeLinkInfo(LsPdu lsPdu) { |
| 273 | Map<String, LinkInformation> linkInformationList = deviceAndLink.removeLinkInfo(lsPdu.lspId()); |
| 274 | for (String key : linkInformationList.keySet()) { |
| 275 | LinkInformation linkInformation = linkInformationList.get(key); |
| 276 | controller.removeLinkDetails(createIsisLink(linkInformation, lsPdu)); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Creates ISIS link instance. |
| 282 | * |
| 283 | * @param linkInformation link information instance |
| 284 | * @return isisLink instance |
| 285 | */ |
| 286 | private IsisLink createIsisLink(LinkInformation linkInformation, LsPdu lsPdu) { |
| 287 | IsisLink isisLink = new DefaultIsisLink(); |
| 288 | isisLink.setLocalSystemId(linkInformation.linkSourceId()); |
| 289 | isisLink.setRemoteSystemId(linkInformation.linkDestinationId()); |
| 290 | isisLink.setInterfaceIp(linkInformation.interfaceIp()); |
| 291 | isisLink.setNeighborIp(linkInformation.neighborIp()); |
| 292 | isisLink.setLinkTed(createIsisLinkTedInfo(lsPdu)); |
| 293 | return isisLink; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Creates the ISIS link TED information. |
| 298 | * |
| 299 | * @param lsPdu link state PDU |
| 300 | * @return isisLinkTed |
| 301 | */ |
| 302 | public IsisLinkTed createIsisLinkTedInfo(LsPdu lsPdu) { |
| 303 | IsisLinkTed isisLinkTed = new DefaultIsisLinkTed(); |
| 304 | for (IsisTlv isisTlv : lsPdu.tlvs()) { |
| 305 | if (isisTlv instanceof IsExtendedReachability) { |
| 306 | IsExtendedReachability isExtendedReachability = (IsExtendedReachability) isisTlv; |
| 307 | List<NeighborForExtendedIs> neighbours = isExtendedReachability.neighbours(); |
| 308 | for (NeighborForExtendedIs teTlv : neighbours) { |
| 309 | List<TrafficEngineeringSubTlv> teSubTlvs = teTlv.teSubTlv(); |
| 310 | for (TrafficEngineeringSubTlv teSubTlv : teSubTlvs) { |
| 311 | if (teSubTlv instanceof AdministrativeGroup) { |
| 312 | AdministrativeGroup ag = (AdministrativeGroup) teSubTlv; |
| 313 | isisLinkTed.setAdministrativeGroup(ag.administrativeGroup()); |
| 314 | } |
| 315 | if (teSubTlv instanceof InterfaceIpAddress) { |
| 316 | InterfaceIpAddress localIpAddress = (InterfaceIpAddress) teSubTlv; |
| 317 | isisLinkTed.setIpv4InterfaceAddress(localIpAddress.localInterfaceIPAddress()); |
| 318 | } |
| 319 | if (teSubTlv instanceof NeighborIpAddress) { |
| 320 | NeighborIpAddress neighborIpAddress = (NeighborIpAddress) teSubTlv; |
| 321 | isisLinkTed.setIpv4NeighborAddress(neighborIpAddress.neighborIPAddress()); |
| 322 | } |
| 323 | if (teSubTlv instanceof TrafficEngineeringMetric) { |
| 324 | TrafficEngineeringMetric teM = (TrafficEngineeringMetric) teSubTlv; |
| 325 | isisLinkTed.setTeDefaultMetric(teM.getTrafficEngineeringMetricValue()); |
| 326 | } |
| 327 | if (teSubTlv instanceof MaximumBandwidth) { |
| 328 | MaximumBandwidth maxLinkBandwidth = (MaximumBandwidth) teSubTlv; |
| 329 | isisLinkTed.setMaximumLinkBandwidth( |
| 330 | Bandwidth.bps(maxLinkBandwidth.getMaximumBandwidthValue())); |
| 331 | } |
| 332 | if (teSubTlv instanceof MaximumReservableBandwidth) { |
| 333 | MaximumReservableBandwidth maxReservableBw = (MaximumReservableBandwidth) teSubTlv; |
| 334 | isisLinkTed.setMaximumReservableLinkBandwidth( |
| 335 | Bandwidth.bps(maxReservableBw.getMaximumBandwidthValue())); |
| 336 | } |
| 337 | if (teSubTlv instanceof UnreservedBandwidth) { |
| 338 | UnreservedBandwidth unReservedBandwidth = (UnreservedBandwidth) teSubTlv; |
| 339 | List<Bandwidth> bandwidthList = new ArrayList<>(); |
| 340 | List<Float> unReservedBandwidthList = unReservedBandwidth.unReservedBandwidthValue(); |
| 341 | for (Float unReservedBandwidthFloatValue : unReservedBandwidthList) { |
| 342 | Bandwidth bandwidth = Bandwidth.bps(unReservedBandwidthFloatValue); |
| 343 | bandwidthList.add(bandwidth); |
| 344 | } |
| 345 | isisLinkTed.setUnreservedBandwidth(bandwidthList); |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | return isisLinkTed; |
| 352 | } |
Ray Milkey | 0bb1e10 | 2016-11-10 14:51:27 -0800 | [diff] [blame^] | 353 | } |