blob: 900709354d9eda1b577f6e7f4e05c0f18d8b740b [file] [log] [blame]
tejeshwer degala3fe1ed52016-04-22 17:04:01 +05301/*
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 */
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 +
61 " consumed.");
62 processRefreshLsp(wrapper);
63 break;
64 case IsisConstants.MAXAGELSP:
65 log.debug("LSPQueueConsumer: Message - " + IsisConstants.MAXAGELSP +
66 " consumed.");
67 processMaxAgeLsa(wrapper);
68 break;
69 default:
70 log.debug("Unknown command to process the LSP in queue ...!!!");
71 break;
72 }
73 }
74 }
75
76 } catch (Exception e) {
77 log.debug("Error::LSPQueueConsumer::{}", e.getMessage());
78 }
79 }
80
81 /**
82 * Process refresh LSP.
83 *
84 * @param wrapper LSP wrapper instance
85 */
86 private void processRefreshLsp(LspWrapper wrapper) throws Exception {
87 if (wrapper.isSelfOriginated()) { //self originated
88 DefaultIsisInterface isisInterface = (DefaultIsisInterface) wrapper.isisInterface();
89 Channel channel = isisInterface.channel();
90 if (channel != null && channel.isConnected()) {
91 LsPdu lsPdu = (LsPdu) wrapper.lsPdu();
92 lsPdu.setSequenceNumber(isisInterface.isisLsdb().lsSequenceNumber(
93 IsisPduType.get(lsPdu.pduType())));
94 lsPdu.setRemainingLifeTime(IsisConstants.LSPMAXAGE);
95 byte[] lspBytes = lsPdu.asBytes();
96 lspBytes = IsisUtil.addLengthAndMarkItInReserved(lspBytes, IsisConstants.LENGTHPOSITION,
97 IsisConstants.LENGTHPOSITION + 1,
98 IsisConstants.RESERVEDPOSITION);
99 lspBytes = IsisUtil.addChecksum(lspBytes, IsisConstants.CHECKSUMPOSITION,
100 IsisConstants.CHECKSUMPOSITION + 1);
101 //write to the channel
102 channel.write(IsisUtil.framePacket(lspBytes, isisInterface.interfaceIndex()));
103
104 log.debug("LSPQueueConsumer: processRefreshLsp - Flooded SelfOriginated LSP {}",
105 wrapper.lsPdu());
106 }
107
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 {}",
125 wrapper.lsPdu());
126 }
127 }
128}