c46cc6a5f558712d2e979bf488547e9bac4822c2
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.sdc.tosca.datatypes.model;
18
19 import java.util.Map;
20 import java.util.Optional;
21 import java.util.Set;
22 import org.onap.sdc.tosca.error.ToscaRuntimeException;
23 import org.onap.sdc.tosca.services.CommonUtil;
24
25 public abstract class InterfaceDefinition extends Interface {
26
27     protected static final String CONVERT_INTERFACE_DEFINITION_OBJECT_ERROR =
28             "Could not create InterfaceDefinition from input object, input object -  ";
29
30     protected InterfaceDefinition convertObjToInterfaceDefinition(Object toscaInterfaceObj) {
31         try {
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();
38             } else {
39                 throw new ToscaRuntimeException(
40                         CONVERT_INTERFACE_DEFINITION_OBJECT_ERROR + toscaInterfaceObj.toString());
41             }
42         } catch (Exception exc) {
43             throw new ToscaRuntimeException(CONVERT_INTERFACE_DEFINITION_OBJECT_ERROR
44                                                     + toscaInterfaceObj.toString(), exc);
45         }
46
47     }
48
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);
57             operationDefinition
58                     .ifPresent(operation -> interfaceDefinition.addOperation(operation.getKey(), operation.getValue()));
59         }
60     }
61
62     public abstract void addOperation(String operationName, OperationDefinition operationDefinition);
63 }