2 * ============LICENSE_START=======================================================
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
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.tosca.datatypes.model;
23 import java.util.HashMap;
25 import java.util.Objects;
26 import java.util.Optional;
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;
33 public class InterfaceType extends Interface {
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;
44 public InterfaceType() {
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()));
57 protected InterfaceType convertObjToInterfaceType(Object toscaInterfaceTypeObj) {
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();
65 throw new ToscaRuntimeException(CONVERT_INTERFACE_TYPE_OBJECT_ERROR + toscaInterfaceTypeObj.toString());
67 } catch (Exception exc) {
68 throw new ToscaRuntimeException(CONVERT_INTERFACE_TYPE_OBJECT_ERROR + toscaInterfaceTypeObj.toString(),exc);
73 private void updateInterfaceTypeOperations(Map<String, Object> interfaceAsMap, InterfaceType interfaceType) {
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);
80 .ifPresent(operation -> interfaceType.addOperation(operation.getKey(), operation.getValue()));
84 public String getDerived_from() {
88 public void setDerived_from(String derivedFrom) {
89 this.derived_from = derivedFrom;
92 public String getVersion() {
96 public void setVersion(String version) {
97 this.version = version;
100 public String getDescription() {
104 public Map<String, String> getMetadata() {
108 public Map<String, PropertyDefinition> getInputs() {
112 public void setInputs(Map<String, PropertyDefinition> inputs) {
113 this.inputs = inputs;
116 public Map<String, OperationDefinition> getOperations() {
120 public void setOperations(Map<String, OperationDefinition> operations) {
121 this.operations = operations;
124 public void addOperation(String operationName, OperationDefinition operationDefinition) {
125 if (MapUtils.isEmpty(this.operations)) {
126 this.operations = new HashMap<>();
128 this.operations.put(operationName, operationDefinition);
131 public void setMetadata(Map<String, String> metadata) {
132 this.metadata = metadata;
135 public void setDescription(String description) {
136 this.description = description;
140 public int hashCode() {
142 return Objects.hash(derived_from, version, metadata, description, inputs, operations);
146 public boolean equals(Object o) {
150 if (!(o instanceof InterfaceType)) {
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);
159 public Optional<Object> convertInterfaceTypeToToscaObj() {
160 return convertInterfaceToToscaInterfaceObj(this);