2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.sdc.toscaparser.api;
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;
28 import java.util.ArrayList;
29 import java.util.LinkedHashMap;
32 public class Group extends EntityTemplate {
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};
44 private LinkedHashMap<String, Object> tpl;
45 private ArrayList<NodeTemplate> memberNodes;
46 private LinkedHashMap<String, Object> customDef;
47 private Metadata metaData;
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);
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);
63 if (tpl.get(METADATA) != null) {
64 Object metadataObject = tpl.get(METADATA);
65 ValidateUtils.validateMap(metadataObject);
66 metaData = new Metadata((Map<String, Object>) metadataObject);
68 this.memberNodes = memberNodes;
73 public Metadata getMetadata() {
77 public ArrayList<String> getMembers() {
78 return (ArrayList<String>) entityTpl.get("members");
81 public String getDescription() {
82 return (String) entityTpl.get("description");
86 public ArrayList<NodeTemplate> getMemberNodes() {
90 private void validateKeys() {
91 for (String key : entityTpl.keySet()) {
92 boolean bFound = false;
93 for (String sect : SECTIONS) {
94 if (key.equals(sect)) {
100 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE183", String.format(
101 "UnknownFieldError: Groups \"%s\" contains unknown field \"%s\"",
108 public String toString() {
110 + "name='" + name + '\''
112 + ", memberNodes=" + memberNodes
113 + ", customDef=" + customDef
114 + ", metaData=" + metaData
118 public int compareTo(Group other) {
119 if (this.equals(other)) {
122 return this.getName().compareTo(other.getName()) == 0 ? this.getType().compareTo(other.getType()) : this.getName().compareTo(other.getName());
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
133 SECTIONS = (TYPE, METADATA, DESCRIPTION, PROPERTIES, MEMBERS, INTERFACES) = \
134 ('type', 'metadata', 'description',
135 'properties', 'members', 'interfaces')
138 class Group(EntityTemplate):
140 def __init__(self, name, group_templates, member_nodes, custom_defs=None):
141 super(Group, self).__init__(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()
156 return self.entity_tpl.get('members')
159 def description(self):
160 return self.entity_tpl.get('description')
162 def get_member_nodes(self):
163 return self.member_nodes
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,