57906140cf0a7aafdca646b2dca7d5d67e615fc6
[sdc.git] / common / onap-tosca-datatype / src / main / java / org / onap / sdc / tosca / datatypes / model / InterfaceType.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.Map;
25 import java.util.Objects;
26 import java.util.Optional;
27 import java.util.Set;
28 import org.apache.commons.collections4.MapUtils;
29 import org.onap.sdc.tosca.error.ToscaRuntimeException;
30 import org.onap.sdc.tosca.services.CommonUtil;
31 import org.onap.sdc.tosca.services.DataModelCloneUtil;
32
33 public class InterfaceType extends Interface {
34
35     protected static final String CONVERT_INTERFACE_TYPE_OBJECT_ERROR =
36             "Could not create InterfaceType from input object, input object -  ";
37     private String derived_from;
38     private String version;
39     private Map<String, String> metadata;
40     private String description;
41     private Map<String, PropertyDefinition> inputs;
42     private Map<String, OperationDefinition> operations;
43
44     public InterfaceType() {
45     }
46
47     public InterfaceType(Object toscaInterfaceTypeObj) {
48         InterfaceType interfaceType = convertObjToInterfaceType(toscaInterfaceTypeObj);
49         this.setDerived_from(interfaceType.getDerived_from());
50         this.setVersion(interfaceType.getVersion());
51         this.setDescription(interfaceType.getDescription());
52         this.setMetadata(DataModelCloneUtil.cloneStringStringMap(interfaceType.getMetadata()));
53         this.setInputs(DataModelCloneUtil.cloneStringPropertyDefinitionMap(interfaceType.getInputs()));
54         this.setOperations(DataModelCloneUtil.cloneStringOperationDefinitionMap(interfaceType.getOperations()));
55     }
56
57     protected InterfaceType convertObjToInterfaceType(Object toscaInterfaceTypeObj) {
58         try {
59             Optional<InterfaceType> interfaceType =
60                     CommonUtil.createObjectUsingSetters(toscaInterfaceTypeObj, this.getClass());
61             if (interfaceType.isPresent()) {
62                 updateInterfaceTypeOperations(CommonUtil.getObjectAsMap(toscaInterfaceTypeObj), interfaceType.get());
63                 return interfaceType.get();
64             } else {
65                 throw new ToscaRuntimeException(CONVERT_INTERFACE_TYPE_OBJECT_ERROR + toscaInterfaceTypeObj.toString());
66             }
67         } catch (Exception exc) {
68             throw new ToscaRuntimeException(CONVERT_INTERFACE_TYPE_OBJECT_ERROR + toscaInterfaceTypeObj.toString(),exc);
69         }
70
71     }
72
73     private void updateInterfaceTypeOperations(Map<String, Object> interfaceAsMap, InterfaceType interfaceType) {
74
75         Set<String> fieldNames = CommonUtil.getClassFieldNames(interfaceType.getClass());
76         for (Map.Entry<String, Object> entry : interfaceAsMap.entrySet()) {
77             Optional<Map.Entry<String, ? extends OperationDefinition>> operationDefinition =
78                     createOperation(entry.getKey(), entry.getValue(), fieldNames, OperationDefinitionType.class);
79             operationDefinition
80                     .ifPresent(operation -> interfaceType.addOperation(operation.getKey(), operation.getValue()));
81         }
82     }
83
84     public String getDerived_from() {
85         return derived_from;
86     }
87
88     public void setDerived_from(String derivedFrom) {
89         this.derived_from = derivedFrom;
90     }
91
92     public String getVersion() {
93         return version;
94     }
95
96     public void setVersion(String version) {
97         this.version = version;
98     }
99
100     public String getDescription() {
101         return description;
102     }
103
104     public Map<String, String> getMetadata() {
105         return metadata;
106     }
107
108     public Map<String, PropertyDefinition> getInputs() {
109         return inputs;
110     }
111
112     public void setInputs(Map<String, PropertyDefinition> inputs) {
113         this.inputs = inputs;
114     }
115
116     public Map<String, OperationDefinition> getOperations() {
117         return operations;
118     }
119
120     public void setOperations(Map<String, OperationDefinition> operations) {
121         this.operations = operations;
122     }
123
124     public void addOperation(String operationName, OperationDefinition operationDefinition) {
125         if (MapUtils.isEmpty(this.operations)) {
126             this.operations = new HashMap<>();
127         }
128         this.operations.put(operationName, operationDefinition);
129     }
130
131     public void setMetadata(Map<String, String> metadata) {
132         this.metadata = metadata;
133     }
134
135     public void setDescription(String description) {
136         this.description = description;
137     }
138
139     @Override
140     public int hashCode() {
141
142         return Objects.hash(derived_from, version, metadata, description, inputs, operations);
143     }
144
145     @Override
146     public boolean equals(Object o) {
147         if (this == o) {
148             return true;
149         }
150         if (!(o instanceof InterfaceType)) {
151             return false;
152         }
153         InterfaceType that = (InterfaceType) o;
154         return Objects.equals(derived_from, that.derived_from) && Objects.equals(version, that.version)
155                        && Objects.equals(metadata, that.metadata) && Objects.equals(description, that.description)
156                        && Objects.equals(inputs, that.inputs) && Objects.equals(operations, that.operations);
157     }
158
159     public Optional<Object> convertInterfaceTypeToToscaObj() {
160         return convertInterfaceToToscaInterfaceObj(this);
161     }
162
163
164 }