blob: 3f9cd5944c107a6149c59db117aa039a3ed1b43b [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 * Returns the bits name.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053071 *
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +053072 * @return the bits name
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053073 */
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +053074 public String getBitsName() {
75 return bitsName;
76 }
77
78 /**
79 * Sets the bits name.
80 *
81 * @param bitsName the bits name
82 */
83 public void setBitsName(String bitsName) {
84 this.bitsName = bitsName;
85 }
86
87 /**
88 * Returns the bit data set.
89 *
90 * @return the bit data set
91 */
92 public BitSet getBitDataSet() {
93 return bitDataSet;
94 }
95
96 /**
97 * Sets the bit data set.
98 *
99 * @param bitNames the set of bit names
100 * @throws DataModelException due to violation in data model rules
101 */
102 public void setBitDataSet(String[] bitNames) throws DataModelException {
103 YangBit bit;
104 for (String bitName : bitNames) {
105 bit = bitNameMap.get(bitName);
106 if (bit == null) {
107 throw new DataModelException("YANG file error: Unable to find " +
108 "corresponding bit position for bit name: " + bitName);
109 }
110 bitDataSet.set(bit.getPosition());
111 }
112 }
113
114 /**
115 * Returns the bit name map.
116 *
117 * @return the bit name map
118 */
119 public Map<String, YangBit> getBitNameMap() {
120 return bitNameMap;
121 }
122
123 /**
124 * Sets the bit name map.
125 *
126 * @param bitNameMap the bit name map
127 */
128 public void setBitNameMap(Map<String, YangBit> bitNameMap) {
129 this.bitNameMap = bitNameMap;
130 }
131
132 /**
133 * Checks whether bit name already available.
134 *
135 * @param bitName bit name
136 * @return true if bit name already available otherwise returns false
137 */
138 public boolean isBitNameExists(String bitName) {
139 return bitNameMap.containsKey(bitName);
140 }
141
142 /**
143 * Returns the bit position map.
144 *
145 * @return the bit position map
146 */
147 public Map<Integer, YangBit> getBitPositionMap() {
148 return bitPositionMap;
149 }
150
151 /**
152 * Sets the bit position map.
153 *
154 * @param bitPositionMap the bit position map
155 */
156 public void setBitPositionMap(Map<Integer, YangBit> bitPositionMap) {
157 this.bitPositionMap = bitPositionMap;
158 }
159
160 /**
161 * Checks whether bit position already available.
162 *
163 * @param bitPosition bit position
164 * @return true if bit position already available otherwise returns false
165 */
166 public boolean isBitPositionExists(Integer bitPosition) {
167 return bitPositionMap.containsKey(bitPosition);
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530168 }
169
170 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530171 * Adds bit info.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530172 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530173 * @param bitInfo the bit information to be added
174 * @throws DataModelException due to violation in data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530175 */
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530176 public void addBitInfo(YangBit bitInfo) throws DataModelException {
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530177 if (bitNameMap.put(bitInfo.getBitName(), bitInfo) != null) {
178 throw new DataModelException("YANG file error: Duplicate bit name detected, same as bit name \""
179 + bitInfo.getBitName() + "\"");
180 }
181 if (bitPositionMap.put(bitInfo.getPosition(), bitInfo) != null) {
182 throw new DataModelException("YANG file error: Duplicate bit position detected, same as bit position \""
183 + bitInfo.getPosition() + "\"");
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530184 }
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530185 }
186
187 /**
188 * Returns the type of the data.
189 *
190 * @return ParsedDataType returns BITS_DATA
191 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530192 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530193 public YangConstructType getYangConstructType() {
194 return YangConstructType.BITS_DATA;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530195 }
196
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530197 @Override
198 public String toString() {
199 YangBit bit;
200 String bits = new String();
201 for (int i = bitDataSet.nextSetBit(0); i >= 0; i = bitDataSet.nextSetBit(i + 1)) {
202 bit = bitPositionMap.get(i);
203 if (bit == null) {
204 return null;
205 }
206 if (bits.isEmpty()) {
207 bits = bit.getBitName();
208 } else {
209 bits += " " + bit.getBitName();
210 }
211 }
212 return bits.trim();
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530213 }
214
215 /**
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530216 * Returns the object of YANG bits based on specific set of bit names.
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530217 *
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530218 * @param bits set of bit names
219 * @return Object of YANG bits
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530220 */
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530221 public YangBits fromString(String bits) {
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530222 try {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530223 String[] bitNames = bits.trim().split(Pattern.quote(SPACE));
224 setBitDataSet(bitNames);
225 return this;
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530226 } catch (Exception e) {
227 }
228 return null;
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530229 }
230
231 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530232 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530233 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530234 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530235 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530236 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530237 public void validateDataOnEntry() throws DataModelException {
238 // TODO auto-generated method stub, to be implemented by parser
239 }
240
241 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530242 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530243 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530244 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530245 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530246 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530247 public void validateDataOnExit() throws DataModelException {
248 // TODO auto-generated method stub, to be implemented by parser
249 }
250}