Kalyankumar Asangi | 27728f2 | 2016-02-17 15:46:28 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Laboratory |
Kalyankumar Asangi | 27728f2 | 2016-02-17 15:46:28 +0530 | [diff] [blame] | 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.ospf.controller.lsdb; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import org.onosproject.ospf.controller.LsaBin; |
| 20 | import org.onosproject.ospf.controller.LsaWrapper; |
| 21 | |
| 22 | import java.util.Map; |
| 23 | import java.util.concurrent.ConcurrentHashMap; |
| 24 | |
| 25 | /** |
| 26 | * Represents a bin, where an LSA is stored for Aging. |
| 27 | * A bin is identified by a bin number and can have one or more LSAs |
| 28 | * store in a particular bin location. |
| 29 | */ |
| 30 | public class LsaBinImpl implements LsaBin { |
| 31 | |
| 32 | private int binNumber; |
| 33 | private Map<String, LsaWrapper> listOfLsa = new ConcurrentHashMap<>(); |
| 34 | |
| 35 | /** |
| 36 | * Creates an instance of LSA bin. |
| 37 | * |
| 38 | * @param binNumber unique number of this bin |
| 39 | */ |
| 40 | public LsaBinImpl(int binNumber) { |
| 41 | this.binNumber = binNumber; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Adds the LSA to this bin with the given key. |
| 46 | * |
| 47 | * @param lsaKey key of the LSA |
| 48 | * @param lsaWrapper wrapper instance to store |
| 49 | */ |
| 50 | public void addOspfLsa(String lsaKey, LsaWrapper lsaWrapper) { |
| 51 | if (!listOfLsa.containsKey(lsaKey)) { |
| 52 | listOfLsa.put(lsaKey, lsaWrapper); |
| 53 | lsaWrapper.setBinNumber(this.binNumber); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Gets the LSA from the bin. |
| 59 | * |
| 60 | * @param lsaKey key to search the LSA |
| 61 | * @return LSA Wrapper instance |
| 62 | */ |
| 63 | public LsaWrapper ospfLsa(String lsaKey) { |
| 64 | |
| 65 | return listOfLsa.get(lsaKey); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Removes LSA from the bin. |
| 70 | * |
| 71 | * @param lsaKey key to search LSA |
| 72 | * @param lsaWrapper wrapper object to remove |
| 73 | */ |
| 74 | public void removeOspfLsa(String lsaKey, LsaWrapper lsaWrapper) { |
| 75 | if (listOfLsa.containsKey(lsaKey)) { |
| 76 | listOfLsa.remove(lsaKey); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Gets the list of LSAs in this bin as key value pair. |
| 82 | * |
| 83 | * @return list of LSAs in this bin as key value pair |
| 84 | */ |
| 85 | public Map<String, LsaWrapper> listOfLsa() { |
| 86 | return listOfLsa; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Gets the bin number. |
| 91 | * |
| 92 | * @return the bin number |
| 93 | */ |
| 94 | public int binNumber() { |
| 95 | return binNumber; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public String toString() { |
| 100 | return MoreObjects.toStringHelper(getClass()) |
| 101 | .omitNullValues() |
| 102 | .add("binNumber", binNumber) |
| 103 | .add("listOfLsa", listOfLsa) |
| 104 | .toString(); |
| 105 | } |
| 106 | } |