Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / toscaparser / api / elements / InterfacesDef.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.elements;
22
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;
26
27 import java.util.ArrayList;
28 import java.util.LinkedHashMap;
29 import java.util.Map;
30
31 public class InterfacesDef extends StatefulEntityType {
32
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";
37
38     public static final String[] SECTIONS = {
39             LIFECYCLE, CONFIGURE, LIFECYCLE_SHORTNAME, CONFIGURE_SHORTNAME
40     };
41
42     public static final String IMPLEMENTATION = "implementation";
43     public static final String DESCRIPTION = "description";
44     public static final String INPUTS = "inputs";
45
46     public static final String[] INTERFACE_DEF_RESERVED_WORDS = {
47             "type", "inputs", "derived_from", "version", "description"};
48
49     private EntityType ntype;
50     private EntityTemplate nodeTemplate;
51
52     private String operationName;
53     private Object operationDef;
54     private Object implementation;
55     private LinkedHashMap<String, Object> inputs;
56     private String description;
57
58     @SuppressWarnings("unchecked")
59     public InterfacesDef(EntityType inodeType,
60                          String interfaceType,
61                          EntityTemplate inodeTemplate,
62                          String iname,
63                          Object ivalue) {
64         // void
65         super();
66
67         ntype = inodeType;
68         nodeTemplate = inodeTemplate;
69         type = interfaceType;
70         operationName = iname;
71         operationDef = ivalue;
72         implementation = null;
73         inputs = null;
74         defs = new LinkedHashMap<>();
75
76         if (interfaceType.equals(LIFECYCLE_SHORTNAME)) {
77             interfaceType = LIFECYCLE;
78         }
79         if (interfaceType.equals(CONFIGURE_SHORTNAME)) {
80             interfaceType = CONFIGURE;
81         }
82
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");
91             }
92         }
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);
98             } else {
99                 defs = (LinkedHashMap<String, Object>) TOSCA_DEF.get(interfaceType);
100             }
101         }
102
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();
112                     } else {
113                         ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE123", String.format(
114                                 "UnknownFieldError: \"interfaces\" of template \"%s\" contain unknown field \"%s\"",
115                                 nodeTemplate.getName(), me.getKey())));
116                     }
117                 }
118             }
119         }
120     }
121
122     public ArrayList<String> getLifecycleOps() {
123         if (defs != null) {
124             if (type.equals(LIFECYCLE)) {
125                 return ops();
126             }
127         }
128         return null;
129     }
130
131     public ArrayList<String> getInterfaceOps() {
132         if (defs != null) {
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]);
137             }
138             ops.removeAll(idrw);
139             return ops;
140         }
141         return null;
142     }
143
144     public ArrayList<String> getConfigureOps() {
145         if (defs != null) {
146             if (type.equals(CONFIGURE)) {
147                 return ops();
148             }
149         }
150         return null;
151     }
152
153     private ArrayList<String> ops() {
154         return new ArrayList<String>(defs.keySet());
155     }
156
157     // getters/setters
158
159     public LinkedHashMap<String, Object> getInputs() {
160         return inputs;
161     }
162
163     public void setInput(String name, Object value) {
164         inputs.put(name, value);
165     }
166
167     public Object getImplementation() {
168         return implementation;
169     }
170
171     public void setImplementation(Object implementation) {
172         this.implementation = implementation;
173     }
174
175     public String getDescription() {
176         return description;
177     }
178
179     public void setDescription(String description) {
180         this.description = description;
181     }
182
183     public String getOperationName() {
184         return operationName;
185     }
186
187     public void setOperationName(String operationName) {
188         this.operationName = operationName;
189     }
190 }
191
192
193
194 /*python
195
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
199 #
200 #         http://www.apache.org/licenses/LICENSE-2.0
201 #
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
206 #    under the License.
207
208 from toscaparser.common.exception import ValidationIssueCollector
209 from toscaparser.common.exception import UnknownFieldError
210 from toscaparser.elements.statefulentitytype import StatefulEntityType
211
212 SECTIONS = (LIFECYCLE, CONFIGURE, LIFECYCLE_SHORTNAME,
213             CONFIGURE_SHORTNAME) = \
214            ('tosca.interfaces.node.lifecycle.Standard',
215             'tosca.interfaces.relationship.Configure',
216             'Standard', 'Configure')
217
218 INTERFACEVALUE = (IMPLEMENTATION, INPUTS) = ('implementation', 'inputs')
219
220 INTERFACE_DEF_RESERVED_WORDS = ['type', 'inputs', 'derived_from', 'version',
221                                 'description']
222
223
224 class InterfacesDef(StatefulEntityType):
225     '''TOSCA built-in interfaces type.'''
226
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
232         self.name = name
233         self.value = value
234         self.implementation = None
235         self.inputs = None
236         self.defs = {}
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']
245         if node_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]
249             else:
250                 self.defs = self.TOSCA_DEF[interfacetype]
251         if value:
252             if isinstance(self.value, dict):
253                 for i, j in self.value.items():
254                     if i == IMPLEMENTATION:
255                         self.implementation = j
256                     elif i == INPUTS:
257                         self.inputs = j
258                     else:
259                         what = ('"interfaces" of template "%s"' %
260                                 self.node_template.name)
261                         ValidationIssueCollector.appendException(
262                             UnknownFieldError(what=what, field=i))
263             else:
264                 self.implementation = value
265
266     @property
267     def lifecycle_ops(self):
268         if self.defs:
269             if self.type == LIFECYCLE:
270                 return self._ops()
271
272     @property
273     def configure_ops(self):
274         if self.defs:
275             if self.type == CONFIGURE:
276                 return self._ops()
277
278     def _ops(self):
279         ops = []
280         for name in list(self.defs.keys()):
281             ops.append(name)
282         return ops
283 */