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.elements;
23 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
24 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
25 import org.onap.sdc.toscaparser.api.EntityTemplate;
27 import java.util.ArrayList;
28 import java.util.LinkedHashMap;
31 public class InterfacesDef extends StatefulEntityType {
33 public static final String LIFECYCLE = "tosca.interfaces.node.lifecycle.Standard";
34 public static final String CONFIGURE = "tosca.interfaces.relationship.Configure";
35 public static final String LIFECYCLE_SHORTNAME = "Standard";
36 public static final String CONFIGURE_SHORTNAME = "Configure";
38 public static final String[] SECTIONS = {
39 LIFECYCLE, CONFIGURE, LIFECYCLE_SHORTNAME, CONFIGURE_SHORTNAME
42 public static final String IMPLEMENTATION = "implementation";
43 public static final String DESCRIPTION = "description";
44 public static final String INPUTS = "inputs";
46 public static final String[] INTERFACE_DEF_RESERVED_WORDS = {
47 "type", "inputs", "derived_from", "version", "description"};
49 private EntityType ntype;
50 private EntityTemplate nodeTemplate;
52 private String operationName;
53 private Object operationDef;
54 private Object implementation;
55 private LinkedHashMap<String, Object> inputs;
56 private String description;
58 @SuppressWarnings("unchecked")
59 public InterfacesDef(EntityType inodeType,
61 EntityTemplate inodeTemplate,
68 nodeTemplate = inodeTemplate;
70 operationName = iname;
71 operationDef = ivalue;
72 implementation = null;
74 defs = new LinkedHashMap<>();
76 if (interfaceType.equals(LIFECYCLE_SHORTNAME)) {
77 interfaceType = LIFECYCLE;
79 if (interfaceType.equals(CONFIGURE_SHORTNAME)) {
80 interfaceType = CONFIGURE;
83 // only NodeType has getInterfaces "hasattr(ntype,interfaces)"
84 // while RelationshipType does not
85 if (ntype instanceof NodeType) {
86 if (((NodeType) ntype).getInterfaces() != null
87 && ((NodeType) ntype).getInterfaces().values().contains(interfaceType)) {
88 LinkedHashMap<String, Object> nii = (LinkedHashMap<String, Object>)
89 ((NodeType) ntype).getInterfaces().get(interfaceType);
90 interfaceType = (String) nii.get("type");
93 if (inodeType != null) {
94 if (nodeTemplate != null && nodeTemplate.getCustomDef() != null
95 && nodeTemplate.getCustomDef().containsKey(interfaceType)) {
96 defs = (LinkedHashMap<String, Object>)
97 nodeTemplate.getCustomDef().get(interfaceType);
99 defs = (LinkedHashMap<String, Object>) TOSCA_DEF.get(interfaceType);
103 if (ivalue != null) {
104 if (ivalue instanceof LinkedHashMap) {
105 for (Map.Entry<String, Object> me : ((LinkedHashMap<String, Object>) ivalue).entrySet()) {
106 if (me.getKey().equals(IMPLEMENTATION)) {
107 implementation = me.getValue();
108 } else if (me.getKey().equals(INPUTS)) {
109 inputs = (LinkedHashMap<String, Object>) me.getValue();
110 } else if (me.getKey().equals(DESCRIPTION)) {
111 description = (String) me.getValue();
113 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE123", String.format(
114 "UnknownFieldError: \"interfaces\" of template \"%s\" contain unknown field \"%s\"",
115 nodeTemplate.getName(), me.getKey())));
122 public ArrayList<String> getLifecycleOps() {
124 if (type.equals(LIFECYCLE)) {
131 public ArrayList<String> getInterfaceOps() {
133 ArrayList<String> ops = ops();
134 ArrayList<String> idrw = new ArrayList<>();
135 for (int i = 0; i < InterfacesDef.INTERFACE_DEF_RESERVED_WORDS.length; i++) {
136 idrw.add(InterfacesDef.INTERFACE_DEF_RESERVED_WORDS[i]);
144 public ArrayList<String> getConfigureOps() {
146 if (type.equals(CONFIGURE)) {
153 private ArrayList<String> ops() {
154 return new ArrayList<String>(defs.keySet());
159 public LinkedHashMap<String, Object> getInputs() {
163 public void setInput(String name, Object value) {
164 inputs.put(name, value);
167 public Object getImplementation() {
168 return implementation;
171 public void setImplementation(Object implementation) {
172 this.implementation = implementation;
175 public String getDescription() {
179 public void setDescription(String description) {
180 this.description = description;
183 public String getOperationName() {
184 return operationName;
187 public void setOperationName(String operationName) {
188 this.operationName = operationName;
196 # Licensed under the Apache License, Version 2.0 (the "License"); you may
197 # not use this file except in compliance with the License. You may obtain
198 # a copy of the License at
200 # http://www.apache.org/licenses/LICENSE-2.0
202 # Unless required by applicable law or agreed to in writing, software
203 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
204 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
205 # License for the specific language governing permissions and limitations
208 from toscaparser.common.exception import ValidationIssueCollector
209 from toscaparser.common.exception import UnknownFieldError
210 from toscaparser.elements.statefulentitytype import StatefulEntityType
212 SECTIONS = (LIFECYCLE, CONFIGURE, LIFECYCLE_SHORTNAME,
213 CONFIGURE_SHORTNAME) = \
214 ('tosca.interfaces.node.lifecycle.Standard',
215 'tosca.interfaces.relationship.Configure',
216 'Standard', 'Configure')
218 INTERFACEVALUE = (IMPLEMENTATION, INPUTS) = ('implementation', 'inputs')
220 INTERFACE_DEF_RESERVED_WORDS = ['type', 'inputs', 'derived_from', 'version',
224 class InterfacesDef(StatefulEntityType):
225 '''TOSCA built-in interfaces type.'''
227 def __init__(self, node_type, interfacetype,
228 node_template=None, name=None, value=None):
229 self.ntype = node_type
230 self.node_template = node_template
231 self.type = interfacetype
234 self.implementation = None
237 if interfacetype == LIFECYCLE_SHORTNAME:
238 interfacetype = LIFECYCLE
239 if interfacetype == CONFIGURE_SHORTNAME:
240 interfacetype = CONFIGURE
241 if hasattr(self.ntype, 'interfaces') \
242 and self.ntype.interfaces \
243 and interfacetype in self.ntype.interfaces:
244 interfacetype = self.ntype.interfaces[interfacetype]['type']
246 if self.node_template and self.node_template.custom_def \
247 and interfacetype in self.node_template.custom_def:
248 self.defs = self.node_template.custom_def[interfacetype]
250 self.defs = self.TOSCA_DEF[interfacetype]
252 if isinstance(self.value, dict):
253 for i, j in self.value.items():
254 if i == IMPLEMENTATION:
255 self.implementation = j
259 what = ('"interfaces" of template "%s"' %
260 self.node_template.name)
261 ValidationIssueCollector.appendException(
262 UnknownFieldError(what=what, field=i))
264 self.implementation = value
267 def lifecycle_ops(self):
269 if self.type == LIFECYCLE:
273 def configure_ops(self):
275 if self.type == CONFIGURE:
280 for name in list(self.defs.keys()):