blob: 74124dd361780db764aeea603b95fa3ecb033fb2 [file] [log] [blame]
Kiran Ramachandrae8699cd2016-02-17 17:15:21 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Kiran Ramachandrae8699cd2016-02-17 17:15:21 +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.ospf.controller.lsdb;
17
18import org.jboss.netty.channel.Channel;
19import org.onosproject.ospf.controller.LsaWrapper;
20import org.onosproject.ospf.controller.OspfArea;
21import org.onosproject.ospf.controller.OspfInterface;
22import org.onosproject.ospf.controller.OspfLsaType;
23import org.onosproject.ospf.controller.area.OspfAreaImpl;
24import org.onosproject.ospf.controller.area.OspfInterfaceImpl;
25import org.onosproject.ospf.protocol.lsa.LsaHeader;
26import org.onosproject.ospf.protocol.lsa.types.NetworkLsa;
27import org.onosproject.ospf.protocol.lsa.types.RouterLsa;
28import org.onosproject.ospf.protocol.util.ChecksumCalculator;
29import org.onosproject.ospf.protocol.util.OspfInterfaceState;
30import org.onosproject.ospf.protocol.util.OspfParameters;
31import org.onosproject.ospf.protocol.util.OspfUtil;
32import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
35import java.util.concurrent.BlockingQueue;
36
37/**
38 * Consumes LSA from the Queue and processes it.
39 * Its a producer consumer implementation using Blocking queue.
40 */
41public class LsaQueueConsumer implements Runnable {
42 private static final Logger log = LoggerFactory.getLogger(LsaQueueConsumer.class);
43 private BlockingQueue queue = null;
44 private Channel channel;
45 private OspfArea ospfArea;
46
47 /**
48 * Creates an instance of LSA queue consumer.
49 *
50 * @param queue queue instance
51 * @param channel netty channel instance
52 * @param ospfArea OSPF area instance
53 */
54 public LsaQueueConsumer(BlockingQueue queue, Channel channel, OspfArea ospfArea) {
55 this.queue = queue;
56 this.channel = channel;
57 this.ospfArea = ospfArea;
58 }
59
60 /**
61 * Threads run method.
62 */
63 public void run() {
64 log.debug("LSAQueueConsumer:run...!!!");
65 try {
66 while (true) {
67 if (!queue.isEmpty()) {
68 LsaWrapper wrapper = (LsaWrapper) queue.take();
69 String lsaProcessing = wrapper.lsaProcessing();
70 switch (lsaProcessing) {
71 case OspfParameters.VERIFYCHECKSUM:
72 log.debug("LSAQueueConsumer: Message - " + OspfParameters.VERIFYCHECKSUM + " consumed.");
73 processVerifyChecksum(wrapper);
74 break;
75 case OspfParameters.REFRESHLSA:
76 log.debug("LSAQueueConsumer: Message - " + OspfParameters.REFRESHLSA + " consumed.");
77 processRefreshLsa(wrapper);
78 break;
79 case OspfParameters.MAXAGELSA:
80 log.debug("LSAQueueConsumer: Message - " + OspfParameters.MAXAGELSA + " consumed.");
81 processMaxAgeLsa(wrapper);
82 break;
83 default:
84 log.debug("Unknown command to process the LSA in queue ...!!!");
85 break;
86 }
87 }
88 }
89
90 } catch (Exception e) {
91 log.debug("Error::LSAQueueConsumer::{}", e.getMessage());
92 }
93 }
94
95 /**
96 * Processes verify checksum - checkAges.
97 *
98 * @param wrapper LSA wrapper instance
99 */
100 private void processVerifyChecksum(LsaWrapper wrapper) throws Exception {
101 ChecksumCalculator checkSum = new ChecksumCalculator();
102 if (!checkSum.isValidLsaCheckSum(wrapper.ospfLsa(), ((LsaWrapperImpl) wrapper).lsaHeader().lsType(),
103 OspfUtil.LSAPACKET_CHECKSUM_POS1,
104 OspfUtil.LSAPACKET_CHECKSUM_POS2)) {
105 log.debug("LSAQueueConsumer::Checksum mismatch. Received LSA packet type {} ",
106 ((LsaWrapperImpl) wrapper).lsaHeader().lsType());
107
108 //Checksum Invalid
109 //RFC 2328 Restart the Router.
110 //Currently we are not restarting. We are not handling this case.
111 }
112 }
113
114 /**
115 * Process refresh LSA.
116 *
117 * @param wrapper LSA wrapper instance
118 */
119 private void processRefreshLsa(LsaWrapper wrapper) throws Exception {
120 if (wrapper.isSelfOriginated()) { //self originated
121 //set the destination
122 OspfInterface ospfInterface = wrapper.ospfInterface();
123 if (ospfInterface != null) {
124 LsaHeader header = ((LsaWrapperImpl) wrapper).lsaHeader();
125 header.setAge(wrapper.currentAge());
126 if (((OspfInterfaceImpl) ospfInterface).state() == OspfInterfaceState.DR) {
127 if (header.lsType() == OspfLsaType.ROUTER.value()) {
128 RouterLsa routerLsa = ((OspfAreaImpl) ospfArea).buildRouterLsa(ospfInterface);
129 ((OspfAreaImpl) ospfArea).addLsa(routerLsa, true, ospfInterface);
130 ((OspfAreaImpl) ospfArea).addToOtherNeighborLsaTxList(routerLsa);
131 } else if (header.lsType() == OspfLsaType.NETWORK.value()) {
132 if (ospfInterface.listOfNeighbors().size() > 0) {
133 NetworkLsa networkLsa = ((OspfAreaImpl) ospfArea).buildNetworkLsa(
134 ospfInterface.ipAddress(), ospfInterface.ipNetworkMask());
135 ospfArea.addLsa(networkLsa, true, ospfInterface);
136 ((OspfAreaImpl) ospfArea).addToOtherNeighborLsaTxList(networkLsa);
137 }
138 }
139 }
140
141 if (((OspfInterfaceImpl) ospfInterface).state() == OspfInterfaceState.BDR ||
142 ((OspfInterfaceImpl) ospfInterface).state() == OspfInterfaceState.POINT2POINT ||
143 ((OspfInterfaceImpl) ospfInterface).state() == OspfInterfaceState.DROTHER) {
144 ospfArea.refreshArea(ospfInterface);
145 }
146 log.debug("LSAQueueConsumer: processRefreshLsa - Flooded SelfOriginated LSA {}",
147 ((LsaWrapperImpl) wrapper).lsaHeader());
148 }
149 }
150 }
151
152 /**
153 * Process max age LSA.
154 *
155 * @param wrapper LSA wrapper instance
156 */
157 private void processMaxAgeLsa(LsaWrapper wrapper) {
158 //set the destination
159 OspfInterface ospfInterface = wrapper.ospfInterface();
160 if (ospfInterface != null) {
161 LsaHeader header = (LsaHeader) wrapper.ospfLsa().lsaHeader();
162 header.setAge(OspfParameters.MAXAGE);
163 ((LsaWrapperImpl) wrapper).lsaHeader().setAge(OspfParameters.MAXAGE);
164 if (((OspfInterfaceImpl) ospfInterface).state() == OspfInterfaceState.DR ||
165 ((OspfInterfaceImpl) ospfInterface).state() == OspfInterfaceState.POINT2POINT) {
166 //remove from db
167 ((OspfAreaImpl) ospfArea).addToOtherNeighborLsaTxList(((LsaWrapperImpl) wrapper).lsaHeader());
168 ((OspfAreaImpl) ospfArea).deleteLsa(((LsaWrapperImpl) wrapper).lsaHeader());
169 } else {
170 ((OspfAreaImpl) ospfArea).deleteLsa(((LsaWrapperImpl) wrapper).lsaHeader());
171 }
172 log.debug("LSAQueueConsumer: processMaxAgeLsa - Flooded SelfOriginated-Max Age LSA {}",
173 ((LsaWrapperImpl) wrapper).lsaHeader());
174 }
175 }
176
177 /**
178 * Sets the channel.
179 *
180 * @param channel channel instance
181 */
182 public void setChannel(Channel channel) {
183 this.channel = channel;
184 }
185}