Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / toscaparser / api / elements / ArtifactTypeDef.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 java.util.LinkedHashMap;
24
25 public class ArtifactTypeDef extends StatefulEntityType {
26
27     private String type;
28     private LinkedHashMap<String, Object> customDef;
29     private LinkedHashMap<String, Object> properties;
30     private LinkedHashMap<String, Object> parentArtifacts;
31
32
33     public ArtifactTypeDef(String type, LinkedHashMap<String, Object> customDef) {
34         super(type, ARTIFACT_PREFIX, customDef);
35
36         this.type = type;
37         this.customDef = customDef;
38         properties = defs != null ? (LinkedHashMap<String, Object>) defs.get(PROPERTIES) : null;
39         parentArtifacts = getParentArtifacts();
40     }
41
42     private LinkedHashMap<String, Object> getParentArtifacts() {
43         LinkedHashMap<String, Object> artifacts = new LinkedHashMap<>();
44         String parentArtif = null;
45         if (getParentType() != null) {
46             parentArtif = getParentType().getType();
47         }
48         if (parentArtif != null && !parentArtif.isEmpty()) {
49             while (!parentArtif.equals("tosca.artifacts.Root")) {
50                 Object ob = TOSCA_DEF.get(parentArtif);
51                 artifacts.put(parentArtif, ob);
52                 parentArtif =
53                         (String) ((LinkedHashMap<String, Object>) ob).get("derived_from");
54             }
55         }
56         return artifacts;
57     }
58
59     public ArtifactTypeDef getParentType() {
60         // Return a artifact entity from which this entity is derived
61         if (defs == null) {
62             return null;
63         }
64         String partifactEntity = derivedFrom(defs);
65         if (partifactEntity != null) {
66             return new ArtifactTypeDef(partifactEntity, customDef);
67         }
68         return null;
69     }
70
71     public Object getArtifact(String name) {
72         // Return the definition of an artifact field by name
73         if (defs != null) {
74             return defs.get(name);
75         }
76         return null;
77     }
78
79     public String getType() {
80         return type;
81     }
82
83 }
84
85 /*python
86 class ArtifactTypeDef(StatefulEntityType):
87     '''TOSCA built-in artifacts type.'''
88
89     def __init__(self, atype, custom_def=None):
90         super(ArtifactTypeDef, self).__init__(atype, self.ARTIFACT_PREFIX,
91                                               custom_def)
92         self.type = atype
93         self.custom_def = custom_def
94         self.properties = None
95         if self.PROPERTIES in self.defs:
96             self.properties = self.defs[self.PROPERTIES]
97         self.parent_artifacts = self._get_parent_artifacts()
98
99     def _get_parent_artifacts(self):
100         artifacts = {}
101         parent_artif = self.parent_type.type if self.parent_type else None
102         if parent_artif:
103             while parent_artif != 'tosca.artifacts.Root':
104                 artifacts[parent_artif] = self.TOSCA_DEF[parent_artif]
105                 parent_artif = artifacts[parent_artif]['derived_from']
106         return artifacts
107
108     @property
109     def parent_type(self):
110         '''Return a artifact entity from which this entity is derived.'''
111         if not hasattr(self, 'defs'):
112             return None
113         partifact_entity = self.derived_from(self.defs)
114         if partifact_entity:
115             return ArtifactTypeDef(partifact_entity, self.custom_def)
116
117     def get_artifact(self, name):
118         '''Return the definition of an artifact field by name.'''
119         if name in self.defs:
120             return self.defs[name]
121 */