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 | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 23 | import java.util.LinkedList; |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 24 | import java.util.List; |
| 25 | |
| 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 | /** |
| 49 | * Data model node to maintain information defined in YANG union. |
| 50 | */ |
| 51 | public class YangUnion implements Parsable { |
| 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 | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 57 | private String unionName; |
| 58 | |
| 59 | /** |
| 60 | * Create a YANG union node. |
| 61 | */ |
| 62 | public YangUnion() { |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 63 | typeList = new LinkedList<>(); |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns list of YANG type. |
| 68 | * |
| 69 | * @return the list of YANG type |
| 70 | */ |
| 71 | public List<YangType<?>> getTypeList() { |
| 72 | return typeList; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns union name. |
| 77 | * |
| 78 | * @return the union name |
| 79 | */ |
| 80 | public String getUnionName() { |
| 81 | return unionName; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Set the list of YANG type. |
| 86 | * |
| 87 | * @param typeList list of YANG type. |
| 88 | */ |
| 89 | public void setTypeList(List<YangType<?>> typeList) { |
| 90 | this.typeList = typeList; |
| 91 | } |
| 92 | |
| 93 | /** |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 94 | * Add YANG type to type list. |
| 95 | * |
| 96 | * @param yangType YANG type to be added to list |
| 97 | * @throws DataModelException union member type must not be one of the |
| 98 | * built-in types "empty" or "leafref" |
| 99 | */ |
| 100 | public void addToTypeList(YangType<?> yangType) throws DataModelException { |
| 101 | if (yangType.getDataType() == YangDataTypes.EMPTY || yangType.getDataType() == YangDataTypes.LEAFREF) { |
| 102 | throw new DataModelException("Union member type must not be one of the built-in types \"empty\" or " + |
| 103 | "\"leafref\""); |
| 104 | } |
| 105 | getTypeList().add(yangType); |
| 106 | } |
| 107 | |
| 108 | /** |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 109 | * Set the union name. |
| 110 | * |
| 111 | * @param unionName name of the union. |
| 112 | */ |
| 113 | public void setUnionName(String unionName) { |
| 114 | this.unionName = unionName; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public YangConstructType getYangConstructType() { |
| 119 | return YangConstructType.UNION_DATA; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void validateDataOnEntry() throws DataModelException { |
| 124 | //TODO: implement the method. |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void validateDataOnExit() throws DataModelException { |
| 129 | //TODO: implement the method. |
| 130 | } |
| 131 | } |