Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +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 | |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 19 | import java.util.LinkedList; |
| 20 | import java.util.List; |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 22 | import org.onosproject.yangutils.parser.Parsable; |
| 23 | import org.onosproject.yangutils.utils.YangConstructType; |
| 24 | |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 25 | /* |
| 26 | * Reference RFC 6020. |
| 27 | * |
| 28 | * The union built-in type represents a value that corresponds to one of |
| 29 | * its member types. |
| 30 | * |
| 31 | * When the type is "union", the "type" statement (Section 7.4) MUST be |
| 32 | * present. It is used to repeatedly specify each member type of the |
| 33 | * union. It takes as an argument a string that is the name of a member |
| 34 | * type. |
| 35 | * |
| 36 | * A member type can be of any built-in or derived type, except it MUST |
| 37 | * NOT be one of the built-in types "empty" or "leafref". |
| 38 | * |
| 39 | * When a string representing a union data type is validated, the string |
| 40 | * is validated against each member type, in the order they are |
| 41 | * specified in the "type" statement, until a match is found. |
| 42 | * |
| 43 | * Any default value or "units" property defined in the member types is |
| 44 | * not inherited by the union type. |
| 45 | */ |
| 46 | |
| 47 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 48 | * Represents data model node to maintain information defined in YANG union. |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 49 | */ |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 50 | public class YangUnion extends YangNode implements Parsable, HasType { |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 51 | |
| 52 | // List of YANG type. |
| 53 | private List<YangType<?>> typeList; |
| 54 | |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 55 | // Name of union. |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 56 | private String name; |
| 57 | |
| 58 | // Current child union number. |
| 59 | private int childUnionNumber; |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 60 | |
| 61 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 62 | * Creates a YANG union node. |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 63 | */ |
| 64 | public YangUnion() { |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 65 | super(YangNodeType.UNION_NODE); |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 66 | typeList = new LinkedList<>(); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 67 | childUnionNumber = 1; |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 68 | } |
| 69 | |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 70 | @Override |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 71 | public List<YangType<?>> getTypeList() { |
| 72 | return typeList; |
| 73 | } |
| 74 | |
| 75 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 76 | * Sets the list of YANG type. |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 77 | * |
| 78 | * @param typeList list of YANG type. |
| 79 | */ |
| 80 | public void setTypeList(List<YangType<?>> typeList) { |
| 81 | this.typeList = typeList; |
| 82 | } |
| 83 | |
| 84 | /** |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 85 | * Returns running child union number. |
| 86 | * |
| 87 | * @return running child union number |
| 88 | */ |
| 89 | public int getChildUnionNumber() { |
| 90 | return childUnionNumber; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Sets the running child union number. |
| 95 | * |
| 96 | * @param childUnionNumber running child union number |
| 97 | */ |
| 98 | public void setChildUnionNumber(int childUnionNumber) { |
| 99 | this.childUnionNumber = childUnionNumber; |
| 100 | } |
| 101 | |
| 102 | /** |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 103 | * Adds YANG type to type list. |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 104 | * |
| 105 | * @param yangType YANG type to be added to list |
| 106 | * @throws DataModelException union member type must not be one of the |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 107 | * built-in types "empty" or "leafref" |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 108 | */ |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 109 | public void addType(YangType<?> yangType) throws DataModelException { |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 110 | if (yangType.getDataType() == YangDataTypes.EMPTY || yangType.getDataType() == YangDataTypes.LEAFREF) { |
| 111 | throw new DataModelException("Union member type must not be one of the built-in types \"empty\" or " + |
| 112 | "\"leafref\""); |
| 113 | } |
| 114 | getTypeList().add(yangType); |
| 115 | } |
| 116 | |
| 117 | /** |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 118 | * Returns union name. |
| 119 | * |
| 120 | * @return the union name |
| 121 | */ |
| 122 | @Override |
| 123 | public String getName() { |
| 124 | return name; |
| 125 | } |
| 126 | |
| 127 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 128 | * Sets the union name. |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 129 | * |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 130 | * @param name union name |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 131 | */ |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 132 | @Override |
| 133 | public void setName(String name) { |
| 134 | this.name = name; |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public YangConstructType getYangConstructType() { |
| 139 | return YangConstructType.UNION_DATA; |
| 140 | } |
| 141 | |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 142 | /** |
| 143 | * Validates the data on entering the corresponding parse tree node. |
| 144 | * |
| 145 | * @throws DataModelException a violation of data model rules |
| 146 | */ |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 147 | @Override |
| 148 | public void validateDataOnEntry() throws DataModelException { |
| 149 | //TODO: implement the method. |
| 150 | } |
| 151 | |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 152 | /** |
| 153 | * Validates the data on exiting the corresponding parse tree node. |
| 154 | * |
| 155 | * @throws DataModelException a violation of data model rules |
| 156 | */ |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 157 | @Override |
| 158 | public void validateDataOnExit() throws DataModelException { |
| 159 | //TODO: implement the method. |
| 160 | } |
| 161 | } |