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