blob: 8a5826948d4b6661c6baec632e8ccc4857f019fa [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
chidambar babu344dc812016-05-02 19:13:10 +053018import org.jboss.netty.buffer.ChannelBuffers;
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053019import org.onosproject.isis.controller.IsisInterface;
20import org.onosproject.isis.controller.IsisLsdb;
21import org.onosproject.isis.controller.IsisLsdbAge;
22import org.onosproject.isis.controller.IsisLspBin;
23import org.onosproject.isis.controller.IsisMessage;
24import org.onosproject.isis.controller.IsisPduType;
25import org.onosproject.isis.controller.LspWrapper;
26import org.onosproject.isis.io.isispacket.pdu.LsPdu;
27import org.onosproject.isis.io.util.IsisConstants;
chidambar babu344dc812016-05-02 19:13:10 +053028import org.onosproject.isis.io.util.IsisUtil;
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053029import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32import java.util.Iterator;
33import java.util.List;
34import java.util.Map;
35import java.util.concurrent.ConcurrentHashMap;
36import java.util.concurrent.CopyOnWriteArrayList;
37
38/**
39 * Representation of ISIS link state database.
40 */
41public class DefaultIsisLsdb implements IsisLsdb {
42 private static final Logger log = LoggerFactory.getLogger(DefaultIsisLsdb.class);
43 private Map<String, LspWrapper> isisL1Db = new ConcurrentHashMap<>();
44 private Map<String, LspWrapper> isisL2Db = new ConcurrentHashMap<>();
45 private IsisLsdbAge lsdbAge = null;
chidambar babu344dc812016-05-02 19:13:10 +053046
47
48
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053049 private int l1LspSeqNo = IsisConstants.STARTLSSEQUENCENUM;
50 private int l2LspSeqNo = IsisConstants.STARTLSSEQUENCENUM;
51
52 /**
53 * Creates an instance of ISIS LSDB.
54 */
55 public DefaultIsisLsdb() {
56 lsdbAge = new DefaultIsisLsdbAge();
57 }
58
59 /**
60 * Initializes the link state database.
61 */
62 public void initializeDb() {
63 lsdbAge.startDbAging();
64 }
65
66 /**
chidambar babu344dc812016-05-02 19:13:10 +053067 * Sets the level 1 link state sequence number.
68 *
69 * @param l1LspSeqNo link state sequence number
70 */
71 public void setL1LspSeqNo(int l1LspSeqNo) {
72 this.l1LspSeqNo = l1LspSeqNo;
73 }
74
75 /**
76 * Sets the level 2 link state sequence number.
77 *
78 * @param l2LspSeqNo link state sequence number
79 */
80 public void setL2LspSeqNo(int l2LspSeqNo) {
81 this.l2LspSeqNo = l2LspSeqNo;
82 }
83 /**
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053084 * Returns the LSDB LSP key.
85 *
86 * @param systemId system ID
87 * @return key
88 */
89 public String lspKey(String systemId) {
90 StringBuilder lspKey = new StringBuilder();
91 lspKey.append(systemId);
92 lspKey.append(".00");
93 lspKey.append("-");
94 lspKey.append("00");
95
96 return lspKey.toString();
97 }
98
99 /**
100 * Returns the neighbor L1 database information.
101 *
102 * @return neighbor L1 database information
103 */
104 public Map<String, LspWrapper> getL1Db() {
105 return isisL1Db;
106 }
107
108 /**
109 * Returns the neighbor L2 database information.
110 *
111 * @return neighbor L2 database information
112 */
113 public Map<String, LspWrapper> getL2Db() {
114 return isisL2Db;
115 }
116
117 /**
118 * Returns the LSDB instance.
119 *
120 * @return LSDB instance
121 */
122 public IsisLsdb isisLsdb() {
123 return this;
124 }
125
126 /**
127 * Returns all LSPs (L1 and L2).
128 *
129 * @param excludeMaxAgeLsp exclude the max age LSPs
130 * @return List of LSPs
131 */
132 public List<LspWrapper> allLspHeaders(boolean excludeMaxAgeLsp) {
chidambar babu344dc812016-05-02 19:13:10 +0530133 List<LspWrapper> summaryList = new CopyOnWriteArrayList<>();
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530134 addLspToHeaderList(summaryList, excludeMaxAgeLsp, isisL1Db);
135 addLspToHeaderList(summaryList, excludeMaxAgeLsp, isisL2Db);
136
137 return summaryList;
138 }
139
140 /**
141 * Adds the LSPs to summary list.
142 *
143 * @param summaryList summary list
144 * @param excludeMaxAgeLsp exclude max age LSP
145 * @param lspMap map of LSP
146 */
147 private void addLspToHeaderList(List summaryList, boolean excludeMaxAgeLsp, Map lspMap) {
148 Iterator slotVals = lspMap.values().iterator();
149 while (slotVals.hasNext()) {
150 LspWrapper wrapper = (LspWrapper) slotVals.next();
151 if (excludeMaxAgeLsp) {
152 //if current age of lsa is max age or lsa present in Max Age bin
153 if (wrapper.remainingLifetime() != 0) {
154 addToList(wrapper, summaryList);
155 }
156 } else {
157 addToList(wrapper, summaryList);
158 }
159 }
160 }
161
162 /**
163 * Adds the LSPWrapper to summary list.
164 *
165 * @param wrapper LSP wrapper instance
166 * @param summList LSP summary list
167 */
168 private void addToList(LspWrapper wrapper, List summList) {
169 //set the current age
170 ((LsPdu) wrapper.lsPdu()).setRemainingLifeTime(wrapper.remainingLifetime());
171 summList.add(wrapper);
172 }
173
174 /**
175 * Finds the LSP from appropriate maps L1 or L2 based on type.
176 *
177 * @param pduType L1 or L2 LSP
178 * @param lspId LSP ID
179 * @return LSP wrapper object
180 */
181 public LspWrapper findLsp(IsisPduType pduType, String lspId) {
182 LspWrapper lspWrapper = null;
183
184 switch (pduType) {
185 case L1LSPDU:
186 lspWrapper = isisL1Db.get(lspId);
187 break;
188 case L2LSPDU:
189 lspWrapper = isisL2Db.get(lspId);
190 break;
191 default:
192 log.debug("Unknown LSP type..!!!");
193 break;
194 }
195
196 //set the current age
197 if (lspWrapper != null) {
198 //set the current age
199 ((DefaultLspWrapper) lspWrapper).lsPdu().setRemainingLifeTime(lspWrapper.remainingLifetime());
200 }
201
202 return lspWrapper;
203 }
204
205 /**
206 * Installs a new self-originated LSP.
207 *
208 * @return true if successfully added
209 */
210 public boolean addLsp(IsisMessage isisMessage, boolean isSelfOriginated, IsisInterface isisInterface) {
211 LsPdu lspdu = (LsPdu) isisMessage;
chidambar babu344dc812016-05-02 19:13:10 +0530212 if (isSelfOriginated) {
213 //Add length and checksum
214 byte[] lspBytes = lspdu.asBytes();
215 lspdu.setPduLength(lspBytes.length);
216 lspBytes = IsisUtil.addChecksum(lspBytes, IsisConstants.CHECKSUMPOSITION,
217 IsisConstants.CHECKSUMPOSITION + 1);
218 byte[] checkSum = {lspBytes[IsisConstants.CHECKSUMPOSITION], lspBytes[IsisConstants.CHECKSUMPOSITION + 1]};
219 lspdu.setCheckSum(ChannelBuffers.copiedBuffer(checkSum).readUnsignedShort());
220 }
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530221 DefaultLspWrapper lspWrapper = new DefaultLspWrapper();
222 lspWrapper.setLspAgeReceived(IsisConstants.LSPMAXAGE - lspdu.remainingLifeTime());
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530223 lspWrapper.setLspType(IsisPduType.get(lspdu.pduType()));
224 lspWrapper.setLsPdu(lspdu);
225 lspWrapper.setAgeCounterWhenReceived(lsdbAge.ageCounter());
226 lspWrapper.setAgeCounterRollOverWhenAdded(lsdbAge.ageCounterRollOver());
227 lspWrapper.setSelfOriginated(isSelfOriginated);
228 lspWrapper.setIsisInterface(isisInterface);
229 lspWrapper.setLsdbAge(lsdbAge);
230 addLsp(lspWrapper, lspdu.lspId());
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530231 log.debug("Added LSp In LSDB: {}", lspWrapper);
232
233 return true;
234 }
235
236 /**
237 * Adds the LSP to L1 or L2 database.
238 *
239 * @param lspWrapper LSA wrapper instance
240 * @param key key
241 * @return True if added else false
242 */
243 private boolean addLsp(LspWrapper lspWrapper, String key) {
244 //Remove the lsa from bin if exist.
245 removeLspFromBin(lspWrapper);
246
247 switch (lspWrapper.lsPdu().isisPduType()) {
248 case L1LSPDU:
chidambar babu344dc812016-05-02 19:13:10 +0530249 isisL1Db.remove(key);
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530250 isisL1Db.put(key, lspWrapper);
251 break;
252 case L2LSPDU:
chidambar babu344dc812016-05-02 19:13:10 +0530253 isisL2Db.remove(key);
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530254 isisL2Db.put(key, lspWrapper);
255 break;
256 default:
257 log.debug("Unknown LSP type to add..!!!");
258 break;
259 }
260
261 //add it to bin
chidambar babu344dc812016-05-02 19:13:10 +0530262 Integer binNumber = lsdbAge.age2Bin(IsisConstants.LSPMAXAGE - lspWrapper.lspAgeReceived());
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530263 IsisLspBin lspBin = lsdbAge.getLspBin(binNumber);
264 if (lspBin != null) {
265 //remove from existing
266 lspWrapper.setBinNumber(binNumber);
267 lspBin.addIsisLsp(key, lspWrapper);
268 lsdbAge.addLspBin(binNumber, lspBin);
269 log.debug("Added Type {} LSP to LSDB and LSABin[{}], Remaining life time of LSA {}",
270 lspWrapper.lsPdu().isisPduType(),
271 binNumber, lspWrapper.remainingLifetime());
272 }
273
274 return false;
275 }
276
277 /**
278 * Removes LSP from Bin.
279 *
280 * @param lsaWrapper LSP wrapper instance
281 */
282 public void removeLspFromBin(LspWrapper lsaWrapper) {
283 if (lsaWrapper != null) {
284 lsdbAge.removeLspFromBin(lsaWrapper);
285 }
286 }
287
288 /**
289 * Returns new ,latest or old according to the type of ISIS message received.
290 *
291 * @param lsp1 LSP instance
292 * @param lsp2 LSP instance
293 * @return string status
294 */
295 public String isNewerOrSameLsp(IsisMessage lsp1, IsisMessage lsp2) {
296 LsPdu receivedLsp = (LsPdu) lsp1;
297 LsPdu lspFromDb = (LsPdu) lsp2;
chidambar babu344dc812016-05-02 19:13:10 +0530298 if (receivedLsp.sequenceNumber() > lspFromDb.sequenceNumber() ||
299 receivedLsp.checkSum() != lspFromDb.checkSum()) {
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530300 return "latest";
301 } else if (receivedLsp.sequenceNumber() < lspFromDb.sequenceNumber()) {
302 return "old";
303 } else if (receivedLsp.sequenceNumber() == lspFromDb.sequenceNumber()) {
304 return "same";
305 }
306
307 return "";
308 }
309
310 /**
311 * Returns the sequence number.
312 *
313 * @param lspType type of LSP
314 * @return sequence number
315 */
316 public int lsSequenceNumber(IsisPduType lspType) {
317 switch (lspType) {
318 case L1LSPDU:
319 return l1LspSeqNo++;
320 case L2LSPDU:
321 return l2LspSeqNo++;
322 default:
323 return IsisConstants.STARTLSSEQUENCENUM;
324 }
325 }
326
327 /**
328 * Deletes the given LSP.
329 *
330 * @param lspMessage LSP instance
331 */
332 public void deleteLsp(IsisMessage lspMessage) {
333 LsPdu lsp = (LsPdu) lspMessage;
334 String lspKey = lsp.lspId();
335 switch (lsp.isisPduType()) {
336 case L1LSPDU:
337 isisL1Db.remove(lspKey);
338 break;
339 case L2LSPDU:
340 isisL2Db.remove(lspKey);
341 break;
342 default:
343 log.debug("Unknown LSP type to remove..!!!");
344 break;
345 }
346 }
chidambar babu344dc812016-05-02 19:13:10 +0530347}