blob: 51708713092858c32e3ff976d3afd3873e7c748d [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
Bharat saraswald9822e92016-04-05 15:13:44 +053019import java.util.LinkedList;
20import java.util.List;
21
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053022import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
23import org.onosproject.yangutils.parser.Parsable;
24import org.onosproject.yangutils.utils.YangConstructType;
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 */
51public class YangUnion implements Parsable {
52
53 // List of YANG type.
54 private List<YangType<?>> typeList;
55
Gaurav Agrawalbd804472016-03-25 11:25:36 +053056 // Name of union.
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053057 private String unionName;
58
59 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053060 * Creates a YANG union node.
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053061 */
62 public YangUnion() {
Gaurav Agrawalbd804472016-03-25 11:25:36 +053063 typeList = new LinkedList<>();
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053064 }
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 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053085 * Sets the list of YANG type.
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053086 *
87 * @param typeList list of YANG type.
88 */
89 public void setTypeList(List<YangType<?>> typeList) {
90 this.typeList = typeList;
91 }
92
93 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053094 * Adds YANG type to type list.
Gaurav Agrawalbd804472016-03-25 11:25:36 +053095 *
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 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530109 * Sets the union name.
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530110 *
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}