Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +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 | |
| 17 | package org.onosproject.yangutils.datamodel; |
| 18 | |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 19 | import java.io.Serializable; |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame^] | 20 | import java.util.BitSet; |
| 21 | import java.util.HashMap; |
| 22 | import java.util.Map; |
| 23 | import java.util.regex.Pattern; |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 24 | |
| 25 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
| 27 | import org.onosproject.yangutils.datamodel.utils.YangConstructType; |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | * Reference RFC 6020. |
| 31 | * |
| 32 | * The bits built-in type represents a bit set. That is, a bits value |
| 33 | * is a set of flags identified by small integer position numbers |
| 34 | * starting at 0. Each bit number has an assigned name. |
| 35 | */ |
| 36 | |
| 37 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 38 | * Represents the bits data type information. |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 39 | */ |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 40 | public class YangBits implements Parsable, Serializable { |
| 41 | |
| 42 | private static final long serialVersionUID = 806201641L; |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame^] | 43 | private static final String SPACE = " "; |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 44 | |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame^] | 45 | // Bits name |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 46 | private String bitsName; |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame^] | 47 | // Bits data contains bit-positions will be used to send to ONOS application |
| 48 | private BitSet bitDataSet; |
| 49 | /** |
| 50 | * Mapping bit name to YangBit. In data input (jason), only bit name will be received. |
| 51 | * By using the bit name corresponding (yang) bit-position will be retrieved from bitNameMap map. |
| 52 | */ |
| 53 | private Map<String, YangBit> bitNameMap; |
| 54 | /** |
| 55 | * Mapping bit position to YangBit. The bit-position received from ONOS application |
| 56 | * will be converted into bit-name by using bitPositionMap map to send (jason) output data as response. |
| 57 | */ |
| 58 | private Map<Integer, YangBit> bitPositionMap; |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 59 | |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 60 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 61 | * Creates a YANG bits type object. |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 62 | */ |
| 63 | public YangBits() { |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame^] | 64 | bitDataSet = new BitSet(); |
| 65 | setBitNameMap(new HashMap<>()); |
| 66 | setBitPositionMap(new HashMap<>()); |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame^] | 70 | * Creates an instance of YANG bits. |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 71 | * |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame^] | 72 | * @param bits set of bit names |
| 73 | * @throws DataModelException due to violation in data model rules |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 74 | */ |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame^] | 75 | public YangBits(String bits) throws DataModelException { |
| 76 | String[] bitNames = bits.trim().split(Pattern.quote(SPACE)); |
| 77 | setBitDataSet(bitNames); |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame^] | 81 | * Returns the bits name. |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 82 | * |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame^] | 83 | * @return the bits name |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 84 | */ |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame^] | 85 | public String getBitsName() { |
| 86 | return bitsName; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Sets the bits name. |
| 91 | * |
| 92 | * @param bitsName the bits name |
| 93 | */ |
| 94 | public void setBitsName(String bitsName) { |
| 95 | this.bitsName = bitsName; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Returns the bit data set. |
| 100 | * |
| 101 | * @return the bit data set |
| 102 | */ |
| 103 | public BitSet getBitDataSet() { |
| 104 | return bitDataSet; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Sets the bit data set. |
| 109 | * |
| 110 | * @param bitNames the set of bit names |
| 111 | * @throws DataModelException due to violation in data model rules |
| 112 | */ |
| 113 | public void setBitDataSet(String[] bitNames) throws DataModelException { |
| 114 | YangBit bit; |
| 115 | for (String bitName : bitNames) { |
| 116 | bit = bitNameMap.get(bitName); |
| 117 | if (bit == null) { |
| 118 | throw new DataModelException("YANG file error: Unable to find " + |
| 119 | "corresponding bit position for bit name: " + bitName); |
| 120 | } |
| 121 | bitDataSet.set(bit.getPosition()); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Returns the bit name map. |
| 127 | * |
| 128 | * @return the bit name map |
| 129 | */ |
| 130 | public Map<String, YangBit> getBitNameMap() { |
| 131 | return bitNameMap; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Sets the bit name map. |
| 136 | * |
| 137 | * @param bitNameMap the bit name map |
| 138 | */ |
| 139 | public void setBitNameMap(Map<String, YangBit> bitNameMap) { |
| 140 | this.bitNameMap = bitNameMap; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Checks whether bit name already available. |
| 145 | * |
| 146 | * @param bitName bit name |
| 147 | * @return true if bit name already available otherwise returns false |
| 148 | */ |
| 149 | public boolean isBitNameExists(String bitName) { |
| 150 | return bitNameMap.containsKey(bitName); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Returns the bit position map. |
| 155 | * |
| 156 | * @return the bit position map |
| 157 | */ |
| 158 | public Map<Integer, YangBit> getBitPositionMap() { |
| 159 | return bitPositionMap; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Sets the bit position map. |
| 164 | * |
| 165 | * @param bitPositionMap the bit position map |
| 166 | */ |
| 167 | public void setBitPositionMap(Map<Integer, YangBit> bitPositionMap) { |
| 168 | this.bitPositionMap = bitPositionMap; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Checks whether bit position already available. |
| 173 | * |
| 174 | * @param bitPosition bit position |
| 175 | * @return true if bit position already available otherwise returns false |
| 176 | */ |
| 177 | public boolean isBitPositionExists(Integer bitPosition) { |
| 178 | return bitPositionMap.containsKey(bitPosition); |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 182 | * Adds bit info. |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 183 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 184 | * @param bitInfo the bit information to be added |
| 185 | * @throws DataModelException due to violation in data model rules |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 186 | */ |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 187 | public void addBitInfo(YangBit bitInfo) throws DataModelException { |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame^] | 188 | if (bitNameMap.put(bitInfo.getBitName(), bitInfo) != null) { |
| 189 | throw new DataModelException("YANG file error: Duplicate bit name detected, same as bit name \"" |
| 190 | + bitInfo.getBitName() + "\""); |
| 191 | } |
| 192 | if (bitPositionMap.put(bitInfo.getPosition(), bitInfo) != null) { |
| 193 | throw new DataModelException("YANG file error: Duplicate bit position detected, same as bit position \"" |
| 194 | + bitInfo.getPosition() + "\""); |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 195 | } |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Returns the type of the data. |
| 200 | * |
| 201 | * @return ParsedDataType returns BITS_DATA |
| 202 | */ |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 203 | @Override |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 204 | public YangConstructType getYangConstructType() { |
| 205 | return YangConstructType.BITS_DATA; |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 206 | } |
| 207 | |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame^] | 208 | @Override |
| 209 | public String toString() { |
| 210 | YangBit bit; |
| 211 | String bits = new String(); |
| 212 | for (int i = bitDataSet.nextSetBit(0); i >= 0; i = bitDataSet.nextSetBit(i + 1)) { |
| 213 | bit = bitPositionMap.get(i); |
| 214 | if (bit == null) { |
| 215 | return null; |
| 216 | } |
| 217 | if (bits.isEmpty()) { |
| 218 | bits = bit.getBitName(); |
| 219 | } else { |
| 220 | bits += " " + bit.getBitName(); |
| 221 | } |
| 222 | } |
| 223 | return bits.trim(); |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /** |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame^] | 227 | * Returns the object of YANG bits based on specific set of bit names. |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 228 | * |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame^] | 229 | * @param bits set of bit names |
| 230 | * @return Object of YANG bits |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 231 | */ |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame^] | 232 | public static YangBits fromString(String bits) { |
| 233 | try { |
| 234 | return new YangBits(bits); |
| 235 | } catch (Exception e) { |
| 236 | } |
| 237 | return null; |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 241 | * Validates the data on entering the corresponding parse tree node. |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 242 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 243 | * @throws DataModelException a violation of data model rules |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 244 | */ |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 245 | @Override |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 246 | public void validateDataOnEntry() throws DataModelException { |
| 247 | // TODO auto-generated method stub, to be implemented by parser |
| 248 | } |
| 249 | |
| 250 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 251 | * Validates the data on exiting the corresponding parse tree node. |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 252 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 253 | * @throws DataModelException a violation of data model rules |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 254 | */ |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 255 | @Override |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 256 | public void validateDataOnExit() throws DataModelException { |
| 257 | // TODO auto-generated method stub, to be implemented by parser |
| 258 | } |
| 259 | } |