blob: 21aabc0a6bbae01fc5e1cb05434781feb82885c9 [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
19import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
20import org.onosproject.yangutils.parser.Parsable;
21import org.onosproject.yangutils.utils.YangConstructType;
22
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053023import java.util.LinkedList;
24import java.util.List;
25
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 */
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053051public class YangUnion extends YangNode implements Parsable {
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053052
53 // List of YANG type.
54 private List<YangType<?>> typeList;
55
Gaurav Agrawalbd804472016-03-25 11:25:36 +053056 // Name of union.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053057 private String name;
58
59 // Current child union number.
60 private int childUnionNumber;
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053061
62 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053063 * Creates a YANG union node.
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053064 */
65 public YangUnion() {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053066 super(YangNodeType.UNION_NODE);
Gaurav Agrawalbd804472016-03-25 11:25:36 +053067 typeList = new LinkedList<>();
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053068 childUnionNumber = 1;
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053069 }
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 saraswald9822e92016-04-05 15:13:44 +053081 * Sets the list of YANG type.
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053082 *
83 * @param typeList list of YANG type.
84 */
85 public void setTypeList(List<YangType<?>> typeList) {
86 this.typeList = typeList;
87 }
88
89 /**
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053090 * 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 Agrawalbd804472016-03-25 11:25:36 +0530109 *
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 Agrawal1c8f80c2016-04-12 13:30:16 +0530114 public void addType(YangType<?> yangType) throws DataModelException {
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530115 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 Agrawal1c8f80c2016-04-12 13:30:16 +0530123 * Returns union name.
124 *
125 * @return the union name
126 */
127 @Override
128 public String getName() {
129 return name;
130 }
131
132 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530133 * Sets the union name.
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530134 *
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530135 * @param name union name
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530136 */
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530137 @Override
138 public void setName(String name) {
139 this.name = name;
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530140 }
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}