c160a32441ac11fda1bf5d61e9121154b69e3204
[sdc/sdc-tosca.git] / jtosca / 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 import lombok.Getter;
25
26 @Getter
27 public class ArtifactTypeDef extends StatefulEntityType {
28
29     private String type;
30     private LinkedHashMap<String, Object> customDef;
31     private LinkedHashMap<String, Object> properties;
32     private LinkedHashMap<String, Object> parentArtifacts;
33
34     public ArtifactTypeDef(String type, LinkedHashMap<String, Object> customDef) {
35         super(type, ARTIFACT_PREFIX, customDef);
36
37         this.type = type;
38         this.customDef = customDef;
39         properties = defs != null ? (LinkedHashMap<String, Object>) defs.get(PROPERTIES) : null;
40         parentArtifacts = getParentArtifacts();
41     }
42
43     private LinkedHashMap<String, Object> getParentArtifacts() {
44         LinkedHashMap<String, Object> artifacts = new LinkedHashMap<>();
45         String parentArtif = null;
46         if (getParentType() != null) {
47             parentArtif = getParentType().getType();
48         }
49         if (parentArtif != null && !parentArtif.isEmpty()) {
50             while (!parentArtif.equals("tosca.artifacts.Root")) {
51                 Object ob = TOSCA_DEF.get(parentArtif);
52                 artifacts.put(parentArtif, ob);
53                 parentArtif =
54                         (String) ((LinkedHashMap<String, Object>) ob).get("derived_from");
55             }
56         }
57         return artifacts;
58     }
59
60     public ArtifactTypeDef getParentType() {
61         // Return a artifact entity from which this entity is derived
62         if (defs == null) {
63             return null;
64         }
65         String partifactEntity = derivedFrom(defs);
66         if (partifactEntity != null) {
67             return new ArtifactTypeDef(partifactEntity, customDef);
68         }
69         return null;
70     }
71
72     public Object getArtifact(String name) {
73         // Return the definition of an artifact field by name
74         if (defs != null) {
75             return defs.get(name);
76         }
77         return null;
78     }
79
80 }