82f1581fa9e763e515fc01584010e7c6c9d7fa4d
[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.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Optional;
27 import lombok.EqualsAndHashCode;
28 import lombok.Getter;
29 import lombok.Setter;
30 import org.apache.commons.collections4.MapUtils;
31 import org.onap.sdc.tosca.error.ToscaRuntimeException;
32 import org.onap.sdc.tosca.services.YamlUtil;
33
34
35 @Getter
36 @Setter
37 @EqualsAndHashCode
38 public class NodeTemplate implements Template, Cloneable {
39
40     private String type;
41     private String description;
42     private Map<String, String> metadata;
43     private List<String> directives;
44     private Map<String, Object> properties;
45     private Map<String, Object> attributes;
46     private List<Map<String, RequirementAssignment>> requirements;
47     private Map<String, CapabilityAssignment> capabilities;
48     private Map<String, Object> interfaces;
49     private Map<String, ArtifactDefinition> artifacts;
50     private NodeFilter node_filter;
51     private String copy;
52
53
54     public Map<String, InterfaceDefinitionTemplate> getNormalizeInterfaces() {
55         if (MapUtils.isEmpty(interfaces)) {
56             return new HashMap<>();
57         }
58         Map<String, InterfaceDefinitionTemplate> normativeInterfaceDefinition = new HashMap<>();
59         for (Map.Entry<String, Object> interfaceEntry : interfaces.entrySet()) {
60             InterfaceDefinitionTemplate interfaceDefinitionTemplate =
61                     new InterfaceDefinitionTemplate(interfaceEntry.getValue());
62             normativeInterfaceDefinition.put(interfaceEntry.getKey(), interfaceDefinitionTemplate);
63         }
64         return normativeInterfaceDefinition;
65     }
66
67     public void addInterface(String interfaceKey, InterfaceDefinitionTemplate interfaceDefinitionTemplate) {
68         if (MapUtils.isEmpty(this.interfaces)) {
69             this.interfaces = new HashMap<>();
70         }
71
72         Optional<Object> toscaInterfaceObj = interfaceDefinitionTemplate.convertInterfaceDefTemplateToToscaObj();
73         if (!toscaInterfaceObj.isPresent()) {
74             throw new ToscaRuntimeException("Illegal Statement");
75         }
76         if (this.interfaces.containsKey(interfaceKey)) {
77             this.interfaces.remove(interfaceKey);
78         }
79         this.interfaces.put(interfaceKey, toscaInterfaceObj.get());
80
81     }
82
83
84     @Override
85     public NodeTemplate clone() {
86         YamlUtil yamlUtil = new YamlUtil();
87         return yamlUtil.yamlToObject(yamlUtil.objectToYaml(this), NodeTemplate.class);
88     }
89
90
91 }