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