tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame] | 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.lsdb; |
| 17 | |
| 18 | import org.jboss.netty.channel.Channel; |
| 19 | import org.onosproject.isis.controller.IsisLsdb; |
| 20 | import org.onosproject.isis.controller.IsisPduType; |
| 21 | import org.onosproject.isis.controller.LspWrapper; |
| 22 | import org.onosproject.isis.controller.impl.DefaultIsisInterface; |
| 23 | import org.onosproject.isis.io.isispacket.pdu.LsPdu; |
| 24 | import org.onosproject.isis.io.util.IsisConstants; |
| 25 | import org.onosproject.isis.io.util.IsisUtil; |
| 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
| 29 | import java.util.concurrent.BlockingQueue; |
| 30 | |
| 31 | /** |
| 32 | * Representation of LSP queue consumer. |
| 33 | */ |
| 34 | public 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 vk | 7bdf4d4 | 2016-06-24 12:29:43 +0530 | [diff] [blame] | 61 | " consumed."); |
tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame] | 62 | processRefreshLsp(wrapper); |
| 63 | break; |
| 64 | case IsisConstants.MAXAGELSP: |
| 65 | log.debug("LSPQueueConsumer: Message - " + IsisConstants.MAXAGELSP + |
sunish vk | 7bdf4d4 | 2016-06-24 12:29:43 +0530 | [diff] [blame] | 66 | " consumed."); |
tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame] | 67 | processMaxAgeLsa(wrapper); |
| 68 | break; |
| 69 | default: |
| 70 | log.debug("Unknown command to process the LSP in queue ...!!!"); |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | } |
tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame] | 75 | } 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 Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame^] | 85 | private void processRefreshLsp(LspWrapper wrapper) { |
tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame] | 86 | 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 vk | 7bdf4d4 | 2016-06-24 12:29:43 +0530 | [diff] [blame] | 96 | IsisConstants.LENGTHPOSITION + 1, |
| 97 | IsisConstants.RESERVEDPOSITION); |
tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame] | 98 | lspBytes = IsisUtil.addChecksum(lspBytes, IsisConstants.CHECKSUMPOSITION, |
sunish vk | 7bdf4d4 | 2016-06-24 12:29:43 +0530 | [diff] [blame] | 99 | IsisConstants.CHECKSUMPOSITION + 1); |
tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame] | 100 | //write to the channel |
| 101 | channel.write(IsisUtil.framePacket(lspBytes, isisInterface.interfaceIndex())); |
sunish vk | 4b5ce00 | 2016-05-09 20:18:35 +0530 | [diff] [blame] | 102 | // Updating the database with resetting remaining life time to default. |
| 103 | IsisLsdb isisDb = isisInterface.isisLsdb(); |
| 104 | isisDb.addLsp(lsPdu, true, isisInterface); |
tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame] | 105 | log.debug("LSPQueueConsumer: processRefreshLsp - Flooded SelfOriginated LSP {}", |
sunish vk | 7bdf4d4 | 2016-06-24 12:29:43 +0530 | [diff] [blame] | 106 | wrapper.lsPdu()); |
tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame] | 107 | } |
tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame] | 108 | } |
| 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 vk | 7bdf4d4 | 2016-06-24 12:29:43 +0530 | [diff] [blame] | 125 | wrapper.lsPdu()); |
tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | } |