blob: d4c03061bfb0de392118309e361de6dd2bb1dd75 [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 Agrawale3ed0d92016-03-23 19:04:17 +053025
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053026/*
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 saraswald9822e92016-04-05 15:13:44 +053049 * Represents data model node to maintain information defined in YANG union.
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053050 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053051public class YangUnion extends YangNode implements Parsable, YangTypeHolder {
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053052
Bharat saraswal96dfef02016-06-16 00:29:12 +053053 private static final long serialVersionUID = 806201616L;
54
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053055 // List of YANG type.
56 private List<YangType<?>> typeList;
57
Gaurav Agrawalbd804472016-03-25 11:25:36 +053058 // Name of union.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053059 private String name;
60
61 // Current child union number.
Bharat saraswal96dfef02016-06-16 00:29:12 +053062 private transient int childUnionNumber;
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053063
64 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053065 * Creates a YANG union node.
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053066 */
67 public YangUnion() {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053068 super(YangNodeType.UNION_NODE);
Gaurav Agrawalbd804472016-03-25 11:25:36 +053069 typeList = new LinkedList<>();
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053070 childUnionNumber = 1;
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053071 }
72
Gaurav Agrawal338735b2016-04-18 18:53:11 +053073 @Override
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053074 public List<YangType<?>> getTypeList() {
75 return typeList;
76 }
77
78 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053079 * Sets the list of YANG type.
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053080 *
81 * @param typeList list of YANG type.
82 */
83 public void setTypeList(List<YangType<?>> typeList) {
84 this.typeList = typeList;
85 }
86
87 /**
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053088 * Returns running child union number.
89 *
90 * @return running child union number
91 */
92 public int getChildUnionNumber() {
93 return childUnionNumber;
94 }
95
96 /**
97 * Sets the running child union number.
98 *
99 * @param childUnionNumber running child union number
100 */
101 public void setChildUnionNumber(int childUnionNumber) {
102 this.childUnionNumber = childUnionNumber;
103 }
104
105 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530106 * Adds YANG type to type list.
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530107 *
108 * @param yangType YANG type to be added to list
109 * @throws DataModelException union member type must not be one of the
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530110 * built-in types "empty" or "leafref"
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530111 */
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530112 public void addType(YangType<?> yangType) throws DataModelException {
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530113 if (yangType.getDataType() == YangDataTypes.EMPTY || yangType.getDataType() == YangDataTypes.LEAFREF) {
114 throw new DataModelException("Union member type must not be one of the built-in types \"empty\" or " +
115 "\"leafref\"");
116 }
117 getTypeList().add(yangType);
118 }
119
120 /**
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530121 * Returns union name.
122 *
123 * @return the union name
124 */
125 @Override
126 public String getName() {
127 return name;
128 }
129
130 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530131 * Sets the union name.
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530132 *
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530133 * @param name union name
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530134 */
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530135 @Override
136 public void setName(String name) {
137 this.name = name;
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530138 }
139
140 @Override
141 public YangConstructType getYangConstructType() {
142 return YangConstructType.UNION_DATA;
143 }
144
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530145 /**
146 * Validates the data on entering the corresponding parse tree node.
147 *
148 * @throws DataModelException a violation of data model rules
149 */
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530150 @Override
151 public void validateDataOnEntry() throws DataModelException {
152 //TODO: implement the method.
153 }
154
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530155 /**
156 * Validates the data on exiting the corresponding parse tree node.
157 *
158 * @throws DataModelException a violation of data model rules
159 */
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530160 @Override
161 public void validateDataOnExit() throws DataModelException {
162 //TODO: implement the method.
163 }
164}