2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.sdc.tosca.datatypes.model;
20 import java.util.Optional;
22 import org.onap.sdc.tosca.error.ToscaRuntimeException;
23 import org.onap.sdc.tosca.services.CommonUtil;
25 public abstract class InterfaceDefinition extends Interface {
27 protected static final String CONVERT_INTERFACE_DEFINITION_OBJECT_ERROR =
28 "Could not create InterfaceDefinition from input object, input object - ";
30 protected InterfaceDefinition convertObjToInterfaceDefinition(Object toscaInterfaceObj) {
32 Optional<? extends InterfaceDefinition> interfaceDefinition =
33 CommonUtil.createObjectUsingSetters(toscaInterfaceObj, this.getClass());
34 if (interfaceDefinition.isPresent()) {
35 updateInterfaceDefinitionOperations(CommonUtil.getObjectAsMap(toscaInterfaceObj),
36 interfaceDefinition.get());
37 return interfaceDefinition.get();
39 throw new ToscaRuntimeException(
40 CONVERT_INTERFACE_DEFINITION_OBJECT_ERROR + toscaInterfaceObj.toString());
42 } catch (Exception exc) {
43 throw new ToscaRuntimeException(CONVERT_INTERFACE_DEFINITION_OBJECT_ERROR
44 + toscaInterfaceObj.toString(), exc);
49 private <T extends OperationDefinition> void updateInterfaceDefinitionOperations(Map<String, Object> interfaceAsMap,
50 InterfaceDefinition interfaceDefinition) {
51 Set<String> fieldNames = CommonUtil.getClassFieldNames(interfaceDefinition.getClass());
52 for (Map.Entry<String, Object> entry : interfaceAsMap.entrySet()) {
53 Optional<Map.Entry<String, ? extends OperationDefinition>> operationDefinition =
54 createOperation(entry.getKey(), entry.getValue(), fieldNames,
55 interfaceDefinition instanceof InterfaceDefinitionType ? OperationDefinitionType.class :
56 OperationDefinitionTemplate.class);
58 .ifPresent(operation -> interfaceDefinition.addOperation(operation.getKey(), operation.getValue()));
62 public abstract void addOperation(String operationName, OperationDefinition operationDefinition);