blob: 34283b4b58918075b480fa8a9ea13060d65ae936 [file] [log] [blame]
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +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
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053019import java.util.LinkedList;
20import java.util.List;
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053021import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
22import org.onosproject.yangutils.parser.Parsable;
23import org.onosproject.yangutils.utils.YangConstructType;
24
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053025/*
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 saraswald9822e92016-04-05 15:13:44 +053048 * Represents data model node to maintain information defined in YANG union.
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053049 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053050public class YangUnion extends YangNode implements Parsable, YangTypeHolder {
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053051
52 // List of YANG type.
53 private List<YangType<?>> typeList;
54
Gaurav Agrawalbd804472016-03-25 11:25:36 +053055 // Name of union.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053056 private String name;
57
58 // Current child union number.
59 private int childUnionNumber;
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053060
61 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053062 * Creates a YANG union node.
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053063 */
64 public YangUnion() {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053065 super(YangNodeType.UNION_NODE);
Gaurav Agrawalbd804472016-03-25 11:25:36 +053066 typeList = new LinkedList<>();
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053067 childUnionNumber = 1;
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053068 }
69
Gaurav Agrawal338735b2016-04-18 18:53:11 +053070 @Override
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053071 public List<YangType<?>> getTypeList() {
72 return typeList;
73 }
74
75 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053076 * Sets the list of YANG type.
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053077 *
78 * @param typeList list of YANG type.
79 */
80 public void setTypeList(List<YangType<?>> typeList) {
81 this.typeList = typeList;
82 }
83
84 /**
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053085 * 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 Agrawal338735b2016-04-18 18:53:11 +0530103 * Adds YANG type to type list.
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530104 *
105 * @param yangType YANG type to be added to list
106 * @throws DataModelException union member type must not be one of the
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530107 * built-in types "empty" or "leafref"
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530108 */
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530109 public void addType(YangType<?> yangType) throws DataModelException {
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530110 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 Agrawal1c8f80c2016-04-12 13:30:16 +0530118 * Returns union name.
119 *
120 * @return the union name
121 */
122 @Override
123 public String getName() {
124 return name;
125 }
126
127 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530128 * Sets the union name.
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530129 *
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530130 * @param name union name
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530131 */
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530132 @Override
133 public void setName(String name) {
134 this.name = name;
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530135 }
136
137 @Override
138 public YangConstructType getYangConstructType() {
139 return YangConstructType.UNION_DATA;
140 }
141
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530142 /**
143 * Validates the data on entering the corresponding parse tree node.
144 *
145 * @throws DataModelException a violation of data model rules
146 */
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530147 @Override
148 public void validateDataOnEntry() throws DataModelException {
149 //TODO: implement the method.
150 }
151
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530152 /**
153 * Validates the data on exiting the corresponding parse tree node.
154 *
155 * @throws DataModelException a violation of data model rules
156 */
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530157 @Override
158 public void validateDataOnExit() throws DataModelException {
159 //TODO: implement the method.
160 }
161}