blob: f20d5cc9dbc0aa98f251cf51eea071b61e0c20e3 [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;
Bharat saraswal96dfef02016-06-16 00:29:12 +053021
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053022import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Bharat saraswal96dfef02016-06-16 00:29:12 +053023import org.onosproject.yangutils.datamodel.utils.Parsable;
24import org.onosproject.yangutils.datamodel.utils.YangConstructType;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053025import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053026
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053027/*
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 saraswald9822e92016-04-05 15:13:44 +053050 * Represents data model node to maintain information defined in YANG union.
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053051 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053052public class YangUnion extends YangNode implements Parsable, YangTypeHolder {
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053053
Bharat saraswal96dfef02016-06-16 00:29:12 +053054 private static final long serialVersionUID = 806201616L;
55
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053056 // List of YANG type.
57 private List<YangType<?>> typeList;
58
Gaurav Agrawalbd804472016-03-25 11:25:36 +053059 // Name of union.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053060 private String name;
61
62 // Current child union number.
Bharat saraswal96dfef02016-06-16 00:29:12 +053063 private transient int childUnionNumber;
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053064
65 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053066 * Creates a YANG union node.
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053067 */
68 public YangUnion() {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053069 super(YangNodeType.UNION_NODE);
Gaurav Agrawalbd804472016-03-25 11:25:36 +053070 typeList = new LinkedList<>();
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053071 childUnionNumber = 1;
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053072 }
73
Gaurav Agrawal338735b2016-04-18 18:53:11 +053074 @Override
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053075 public List<YangType<?>> getTypeList() {
76 return typeList;
77 }
78
79 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053080 * Sets the list of YANG type.
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053081 *
82 * @param typeList list of YANG type.
83 */
84 public void setTypeList(List<YangType<?>> typeList) {
85 this.typeList = typeList;
86 }
87
88 /**
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053089 * 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 Agrawal338735b2016-04-18 18:53:11 +0530107 * Adds YANG type to type list.
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530108 *
109 * @param yangType YANG type to be added to list
110 * @throws DataModelException union member type must not be one of the
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530111 * built-in types "empty" or "leafref"
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530112 */
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530113 public void addType(YangType<?> yangType) throws DataModelException {
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530114 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 Agrawal1c8f80c2016-04-12 13:30:16 +0530122 * Returns union name.
123 *
124 * @return the union name
125 */
126 @Override
127 public String getName() {
128 return name;
129 }
130
131 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530132 * Sets the union name.
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530133 *
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530134 * @param name union name
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530135 */
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530136 @Override
137 public void setName(String name) {
138 this.name = name;
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530139 }
140
141 @Override
142 public YangConstructType getYangConstructType() {
143 return YangConstructType.UNION_DATA;
144 }
145
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530146 /**
147 * Validates the data on entering the corresponding parse tree node.
148 *
149 * @throws DataModelException a violation of data model rules
150 */
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530151 @Override
152 public void validateDataOnEntry() throws DataModelException {
153 //TODO: implement the method.
154 }
155
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530156 /**
157 * Validates the data on exiting the corresponding parse tree node.
158 *
159 * @throws DataModelException a violation of data model rules
160 */
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530161 @Override
162 public void validateDataOnExit() throws DataModelException {
163 //TODO: implement the method.
164 }
165}