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