blob: b0810e9fed139343cddbeb612d7a1aecd00a7c53 [file] [log] [blame]
Vinod Kumar S2ff139c2016-02-16 01:37:16 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S2ff139c2016-02-16 01:37:16 +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 */
16
17package org.onosproject.yangutils.datamodel;
18
Bharat saraswal96dfef02016-06-16 00:29:12 +053019import java.io.Serializable;
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +053020import java.util.BitSet;
21import java.util.HashMap;
22import java.util.Map;
23import java.util.regex.Pattern;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053024
25import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Bharat saraswal96dfef02016-06-16 00:29:12 +053026import org.onosproject.yangutils.datamodel.utils.Parsable;
27import org.onosproject.yangutils.datamodel.utils.YangConstructType;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053028
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 saraswald9822e92016-04-05 15:13:44 +053038 * Represents the bits data type information.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053039 */
Bharat saraswal96dfef02016-06-16 00:29:12 +053040public class YangBits implements Parsable, Serializable {
41
42 private static final long serialVersionUID = 806201641L;
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +053043 private static final String SPACE = " ";
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053044
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +053045 // Bits name
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053046 private String bitsName;
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +053047 // 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 Agrawal0cfdeed2016-02-26 02:47:39 +053059
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053060 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053061 * Creates a YANG bits type object.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053062 */
63 public YangBits() {
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +053064 bitDataSet = new BitSet();
65 setBitNameMap(new HashMap<>());
66 setBitPositionMap(new HashMap<>());
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053067 }
68
69 /**
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +053070 * Creates an instance of YANG bits.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053071 *
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +053072 * @param bits set of bit names
73 * @throws DataModelException due to violation in data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053074 */
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +053075 public YangBits(String bits) throws DataModelException {
76 String[] bitNames = bits.trim().split(Pattern.quote(SPACE));
77 setBitDataSet(bitNames);
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053078 }
79
80 /**
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +053081 * Returns the bits name.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053082 *
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +053083 * @return the bits name
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053084 */
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +053085 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 S2ff139c2016-02-16 01:37:16 +0530179 }
180
181 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530182 * Adds bit info.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530183 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530184 * @param bitInfo the bit information to be added
185 * @throws DataModelException due to violation in data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530186 */
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530187 public void addBitInfo(YangBit bitInfo) throws DataModelException {
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530188 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 Agrawal0cfdeed2016-02-26 02:47:39 +0530195 }
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530196 }
197
198 /**
199 * Returns the type of the data.
200 *
201 * @return ParsedDataType returns BITS_DATA
202 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530203 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530204 public YangConstructType getYangConstructType() {
205 return YangConstructType.BITS_DATA;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530206 }
207
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530208 @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 Agrawal0cfdeed2016-02-26 02:47:39 +0530224 }
225
226 /**
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530227 * Returns the object of YANG bits based on specific set of bit names.
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530228 *
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530229 * @param bits set of bit names
230 * @return Object of YANG bits
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530231 */
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530232 public static YangBits fromString(String bits) {
233 try {
234 return new YangBits(bits);
235 } catch (Exception e) {
236 }
237 return null;
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530238 }
239
240 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530241 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530242 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530243 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530244 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530245 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530246 public void validateDataOnEntry() throws DataModelException {
247 // TODO auto-generated method stub, to be implemented by parser
248 }
249
250 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530251 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530252 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530253 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530254 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530255 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530256 public void validateDataOnExit() throws DataModelException {
257 // TODO auto-generated method stub, to be implemented by parser
258 }
259}