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