Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / toscaparser / api / Group.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.sdc.toscaparser.api;
22
23 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
24 import org.onap.sdc.toscaparser.api.elements.Metadata;
25 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
26 import org.onap.sdc.toscaparser.api.utils.ValidateUtils;
27
28 import java.util.ArrayList;
29 import java.util.LinkedHashMap;
30 import java.util.Map;
31
32 public class Group extends EntityTemplate {
33
34     private static final String TYPE = "type";
35     private static final String METADATA = "metadata";
36     private static final String DESCRIPTION = "description";
37     private static final String PROPERTIES = "properties";
38     private static final String MEMBERS = "members";
39     private static final String INTERFACES = "interfaces";
40     private static final String[] SECTIONS = {
41             TYPE, METADATA, DESCRIPTION, PROPERTIES, MEMBERS, INTERFACES};
42
43     private String name;
44     private LinkedHashMap<String, Object> tpl;
45     private ArrayList<NodeTemplate> memberNodes;
46     private LinkedHashMap<String, Object> customDef;
47     private Metadata metaData;
48
49
50     public Group(String name, LinkedHashMap<String, Object> templates,
51                  ArrayList<NodeTemplate> memberNodes,
52                  LinkedHashMap<String, Object> customDef) {
53         this(name, templates, memberNodes, customDef, null);
54     }
55
56     public Group(String name, LinkedHashMap<String, Object> templates,
57                  ArrayList<NodeTemplate> memberNodes,
58                  LinkedHashMap<String, Object> customDef, NodeTemplate parentNodeTemplate) {
59         super(name, templates, "group_type", customDef, parentNodeTemplate);
60
61         this.name = name;
62         tpl = templates;
63         if (tpl.get(METADATA) != null) {
64             Object metadataObject = tpl.get(METADATA);
65             ValidateUtils.validateMap(metadataObject);
66             metaData = new Metadata((Map<String, Object>) metadataObject);
67         }
68         this.memberNodes = memberNodes;
69         validateKeys();
70         getCapabilities();
71     }
72
73     public Metadata getMetadata() {
74         return metaData;
75     }
76
77     public ArrayList<String> getMembers() {
78         return (ArrayList<String>) entityTpl.get("members");
79     }
80
81     public String getDescription() {
82         return (String) entityTpl.get("description");
83
84     }
85
86     public ArrayList<NodeTemplate> getMemberNodes() {
87         return memberNodes;
88     }
89
90     private void validateKeys() {
91         for (String key : entityTpl.keySet()) {
92             boolean bFound = false;
93             for (String sect : SECTIONS) {
94                 if (key.equals(sect)) {
95                     bFound = true;
96                     break;
97                 }
98             }
99             if (!bFound) {
100                 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE183", String.format(
101                         "UnknownFieldError: Groups \"%s\" contains unknown field \"%s\"",
102                         name, key)));
103             }
104         }
105     }
106
107     @Override
108     public String toString() {
109         return "Group{"
110                 + "name='" + name + '\''
111                 + ", tpl=" + tpl
112                 + ", memberNodes=" + memberNodes
113                 + ", customDef=" + customDef
114                 + ", metaData=" + metaData
115                 + '}';
116     }
117
118     public int compareTo(Group other) {
119         if (this.equals(other)) {
120             return 0;
121         }
122         return this.getName().compareTo(other.getName()) == 0 ? this.getType().compareTo(other.getType()) : this.getName().compareTo(other.getName());
123     }
124 }
125
126 /*python
127
128 from toscaparser.common.exception import ValidationIssueCollector
129 from toscaparser.common.exception import UnknownFieldError
130 from toscaparser.entity_template import EntityTemplate
131 from toscaparser.utils import validateutils
132
133 SECTIONS = (TYPE, METADATA, DESCRIPTION, PROPERTIES, MEMBERS, INTERFACES) = \
134            ('type', 'metadata', 'description',
135             'properties', 'members', 'interfaces')
136
137
138 class Group(EntityTemplate):
139
140     def __init__(self, name, group_templates, member_nodes, custom_defs=None):
141         super(Group, self).__init__(name,
142                                     group_templates,
143                                     'group_type',
144                                     custom_defs)
145         self.name = name
146         self.tpl = group_templates
147         self.meta_data = None
148         if self.METADATA in self.tpl:
149             self.meta_data = self.tpl.get(self.METADATA)
150             validateutils.validate_map(self.meta_data)
151         self.member_nodes = member_nodes
152         self._validate_keys()
153
154     @property
155     def members(self):
156         return self.entity_tpl.get('members')
157
158     @property
159     def description(self):
160         return self.entity_tpl.get('description')
161
162     def get_member_nodes(self):
163         return self.member_nodes
164
165     def _validate_keys(self):
166         for key in self.entity_tpl.keys():
167             if key not in SECTIONS:
168                 ValidationIssueCollector.appendException(
169                     UnknownFieldError(what='Groups "%s"' % self.name,
170                                       field=key))
171 */