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 java.util.LinkedHashMap;
25 public class ArtifactTypeDef extends StatefulEntityType {
28 private LinkedHashMap<String, Object> customDef;
29 private LinkedHashMap<String, Object> properties;
30 private LinkedHashMap<String, Object> parentArtifacts;
33 public ArtifactTypeDef(String type, LinkedHashMap<String, Object> customDef) {
34 super(type, ARTIFACT_PREFIX, customDef);
37 this.customDef = customDef;
38 properties = defs != null ? (LinkedHashMap<String, Object>) defs.get(PROPERTIES) : null;
39 parentArtifacts = getParentArtifacts();
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();
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);
53 (String) ((LinkedHashMap<String, Object>) ob).get("derived_from");
59 public ArtifactTypeDef getParentType() {
60 // Return a artifact entity from which this entity is derived
64 String partifactEntity = derivedFrom(defs);
65 if (partifactEntity != null) {
66 return new ArtifactTypeDef(partifactEntity, customDef);
71 public Object getArtifact(String name) {
72 // Return the definition of an artifact field by name
74 return defs.get(name);
79 public String getType() {
86 class ArtifactTypeDef(StatefulEntityType):
87 '''TOSCA built-in artifacts type.'''
89 def __init__(self, atype, custom_def=None):
90 super(ArtifactTypeDef, self).__init__(atype, self.ARTIFACT_PREFIX,
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()
99 def _get_parent_artifacts(self):
101 parent_artif = self.parent_type.type if self.parent_type else None
103 while parent_artif != 'tosca.artifacts.Root':
104 artifacts[parent_artif] = self.TOSCA_DEF[parent_artif]
105 parent_artif = artifacts[parent_artif]['derived_from']
109 def parent_type(self):
110 '''Return a artifact entity from which this entity is derived.'''
111 if not hasattr(self, 'defs'):
113 partifact_entity = self.derived_from(self.defs)
115 return ArtifactTypeDef(partifact_entity, self.custom_def)
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]