blob: ddee6475e754d7525065c42bc2b31de8b749184d [file] [log] [blame]
tejeshwer degala3fe1ed52016-04-22 17:04:01 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
tejeshwer degala3fe1ed52016-04-22 17:04:01 +05303 *
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 */
16package org.onosproject.isis.controller.impl.lsdb;
17
18import org.jboss.netty.channel.Channel;
19import org.onosproject.isis.controller.IsisLsdb;
20import org.onosproject.isis.controller.IsisPduType;
21import org.onosproject.isis.controller.LspWrapper;
22import org.onosproject.isis.controller.impl.DefaultIsisInterface;
23import org.onosproject.isis.io.isispacket.pdu.LsPdu;
24import org.onosproject.isis.io.util.IsisConstants;
25import org.onosproject.isis.io.util.IsisUtil;
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29import java.util.concurrent.BlockingQueue;
30
31/**
32 * Representation of LSP queue consumer.
33 */
34public class IsisLspQueueConsumer implements Runnable {
35 private static final Logger log = LoggerFactory.getLogger(IsisLspQueueConsumer.class);
36 private BlockingQueue queue = null;
37
38 /**
39 * Creates an instance of LSP queue consumer.
40 *
41 * @param queue queue instance
42 */
43 public IsisLspQueueConsumer(BlockingQueue queue) {
44 this.queue = queue;
45 }
46
47 /**
48 * Gets the LSP wrapper instance from queue and process it.
49 */
50 @Override
51 public void run() {
52 log.debug("LSPQueueConsumer:run...!!!");
53 try {
54 while (true) {
55 if (!queue.isEmpty()) {
56 LspWrapper wrapper = (LspWrapper) queue.take();
57 String lspProcessing = wrapper.lspProcessing();
58 switch (lspProcessing) {
59 case IsisConstants.REFRESHLSP:
60 log.debug("LSPQueueConsumer: Message - " + IsisConstants.REFRESHLSP +
sunish vk7bdf4d42016-06-24 12:29:43 +053061 " consumed.");
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053062 processRefreshLsp(wrapper);
63 break;
64 case IsisConstants.MAXAGELSP:
65 log.debug("LSPQueueConsumer: Message - " + IsisConstants.MAXAGELSP +
sunish vk7bdf4d42016-06-24 12:29:43 +053066 " consumed.");
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053067 processMaxAgeLsa(wrapper);
68 break;
69 default:
70 log.debug("Unknown command to process the LSP in queue ...!!!");
71 break;
72 }
73 }
74 }
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053075 } catch (Exception e) {
76 log.debug("Error::LSPQueueConsumer::{}", e.getMessage());
77 }
78 }
79
80 /**
81 * Process refresh LSP.
82 *
83 * @param wrapper LSP wrapper instance
84 */
Ray Milkey986a47a2018-01-25 11:38:51 -080085 private void processRefreshLsp(LspWrapper wrapper) {
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053086 if (wrapper.isSelfOriginated()) { //self originated
87 DefaultIsisInterface isisInterface = (DefaultIsisInterface) wrapper.isisInterface();
88 Channel channel = isisInterface.channel();
89 if (channel != null && channel.isConnected()) {
90 LsPdu lsPdu = (LsPdu) wrapper.lsPdu();
91 lsPdu.setSequenceNumber(isisInterface.isisLsdb().lsSequenceNumber(
92 IsisPduType.get(lsPdu.pduType())));
93 lsPdu.setRemainingLifeTime(IsisConstants.LSPMAXAGE);
94 byte[] lspBytes = lsPdu.asBytes();
95 lspBytes = IsisUtil.addLengthAndMarkItInReserved(lspBytes, IsisConstants.LENGTHPOSITION,
sunish vk7bdf4d42016-06-24 12:29:43 +053096 IsisConstants.LENGTHPOSITION + 1,
97 IsisConstants.RESERVEDPOSITION);
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053098 lspBytes = IsisUtil.addChecksum(lspBytes, IsisConstants.CHECKSUMPOSITION,
sunish vk7bdf4d42016-06-24 12:29:43 +053099 IsisConstants.CHECKSUMPOSITION + 1);
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530100 //write to the channel
101 channel.write(IsisUtil.framePacket(lspBytes, isisInterface.interfaceIndex()));
sunish vk4b5ce002016-05-09 20:18:35 +0530102 // Updating the database with resetting remaining life time to default.
103 IsisLsdb isisDb = isisInterface.isisLsdb();
104 isisDb.addLsp(lsPdu, true, isisInterface);
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530105 log.debug("LSPQueueConsumer: processRefreshLsp - Flooded SelfOriginated LSP {}",
sunish vk7bdf4d42016-06-24 12:29:43 +0530106 wrapper.lsPdu());
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530107 }
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530108 }
109 }
110
111 /**
112 * Process max age LSP.
113 *
114 * @param wrapper LSP wrapper instance
115 */
116 private void processMaxAgeLsa(LspWrapper wrapper) {
117 //set the destination
118 DefaultIsisInterface isisInterface = (DefaultIsisInterface) wrapper.isisInterface();
119 if (isisInterface != null) {
120 //delete from db
121 LsPdu lsPdu = (LsPdu) wrapper.lsPdu();
122 IsisLsdb isisDb = isisInterface.isisLsdb();
123 isisDb.deleteLsp(lsPdu);
124 log.debug("LSPQueueConsumer: processMaxAgeLsp - Removed-Max Age LSP {}",
sunish vk7bdf4d42016-06-24 12:29:43 +0530125 wrapper.lsPdu());
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530126 }
127 }
128}