Upgrade to java 11
[sdc.git] / common / onap-tosca-datatype / src / main / java / org / onap / sdc / tosca / datatypes / model / NodeTemplate.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.tosca.datatypes.model;
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Optional;
28 import java.util.Set;
29
30 import lombok.EqualsAndHashCode;
31 import lombok.Getter;
32 import lombok.Setter;
33
34 import org.apache.commons.collections4.CollectionUtils;
35 import org.apache.commons.collections4.MapUtils;
36 import org.onap.sdc.tosca.error.ToscaRuntimeException;
37 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
38 import org.onap.sdc.tosca.services.YamlUtil;
39 import org.yaml.snakeyaml.constructor.ConstructorException;
40
41
42 @Getter
43 @Setter
44 @EqualsAndHashCode
45 public class NodeTemplate implements Template, Cloneable {
46
47     private static final String INVALID_TOSCA_REQUIREMENT_SECTION = "Invalid TOSCA requirement section";
48     private String type;
49     private String description;
50     private Map<String, String> metadata;
51     private List<String> directives;
52     private Map<String, Object> properties;
53     private Map<String, Object> attributes;
54     private List<Map<String, RequirementAssignment>> requirements;
55     private Map<String, CapabilityAssignment> capabilities;
56     private Map<String, Object> interfaces;
57     private Map<String, ArtifactDefinition> artifacts;
58     private NodeFilter node_filter;
59     private String copy;
60
61     public void setRequirements(List requirementAssignmentObj) {
62         this.requirements = convertToscaRequirementAssignment(requirementAssignmentObj); 
63     }
64
65     public static List<Map<String, RequirementAssignment>> convertToscaRequirementAssignment(List<?> requirementAssignmentObj) {
66
67         List<Map<String, RequirementAssignment>> convertedRequirements = new ArrayList<>();
68         if (CollectionUtils.isEmpty(requirementAssignmentObj)) {
69             return null;
70         }
71         for (Object requirementEntry : requirementAssignmentObj) {
72             convertToscaRequirementAssignmentEntry(convertedRequirements, requirementEntry);
73         }
74         return convertedRequirements;
75     }
76
77     private static void convertToscaRequirementAssignmentEntry(List<Map<String, RequirementAssignment>> convertedRequirements, Object requirementEntry) {
78         if (requirementEntry instanceof Map) {
79             try {
80                 Set<Map.Entry<String, RequirementAssignment>> requirementEntries = ((Map)requirementEntry).entrySet();
81                 for (Map.Entry<String, RequirementAssignment> toscaRequirements : requirementEntries) {
82                     String key = toscaRequirements.getKey();
83                     Object requirementValue = toscaRequirements.getValue();
84                     if (requirementValue instanceof Map) {
85                         RequirementAssignment requirementObject;
86                         try {
87                             YamlUtil yamlUtil = new YamlUtil();
88                             requirementObject = yamlUtil
89                                 .yamlToObject(yamlUtil.objectToYaml(requirementValue), RequirementAssignment.class);
90                         } catch (ConstructorException ex) {
91                             // The requirement might contains extended attribute, so try to parse it into RequirementAssignmentExt as well
92                             ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
93                             requirementObject = toscaExtensionYamlUtil
94                                     .yamlToObject(toscaExtensionYamlUtil.objectToYaml(requirementValue), RequirementAssignment.class);
95                         }
96                         Map<String, RequirementAssignment> convertedToscaRequirement = new HashMap<>();
97                         convertedToscaRequirement.put(key, requirementObject);
98                         convertedRequirements.add(convertedToscaRequirement);
99                     } else  if (requirementValue instanceof RequirementAssignment) {
100                         Map<String, RequirementAssignment> convertedToscaRequirement = new HashMap<>();
101                         convertedToscaRequirement.put(key, (RequirementAssignment)requirementValue);
102                         convertedRequirements.add(convertedToscaRequirement);
103                     }
104                 }
105             } catch (Exception ex) {
106                 throw new ToscaRuntimeException(INVALID_TOSCA_REQUIREMENT_SECTION, ex);
107             }
108         }
109     }
110
111     public void addRequirements(Map<String, RequirementAssignment> newRequirement) {
112         if (CollectionUtils.isEmpty(this.requirements)) {
113             this.requirements = new ArrayList<Map<String, RequirementAssignment>>();
114         }
115         this.requirements.add(newRequirement);
116     }
117
118     public Map<String, InterfaceDefinitionTemplate> getNormalizeInterfaces() {
119         if (MapUtils.isEmpty(interfaces)) {
120             return new HashMap<>();
121         }
122         Map<String, InterfaceDefinitionTemplate> normativeInterfaceDefinition = new HashMap<>();
123         for (Map.Entry<String, Object> interfaceEntry : interfaces.entrySet()) {
124             InterfaceDefinitionTemplate interfaceDefinitionTemplate =
125                     new InterfaceDefinitionTemplate(interfaceEntry.getValue());
126             normativeInterfaceDefinition.put(interfaceEntry.getKey(), interfaceDefinitionTemplate);
127         }
128         return normativeInterfaceDefinition;
129     }
130
131     public void addInterface(String interfaceKey, InterfaceDefinitionTemplate interfaceDefinitionTemplate) {
132         if (MapUtils.isEmpty(this.interfaces)) {
133             this.interfaces = new HashMap<>();
134         }
135
136         Optional<Object> toscaInterfaceObj = interfaceDefinitionTemplate.convertInterfaceDefTemplateToToscaObj();
137         if (!toscaInterfaceObj.isPresent()) {
138             throw new ToscaRuntimeException("Illegal Statement");
139         }
140         if (this.interfaces.containsKey(interfaceKey)) {
141             this.interfaces.remove(interfaceKey);
142         }
143         this.interfaces.put(interfaceKey, toscaInterfaceObj.get());
144
145     }
146
147
148     @Override
149     public NodeTemplate clone() {
150         YamlUtil yamlUtil = new YamlUtil();
151         return yamlUtil.yamlToObject(yamlUtil.objectToYaml(this), NodeTemplate.class);
152     }
153
154
155 }