Revert "Interface operation feature enhancements"
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / InterfaceLifecycleTypeImportManager.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.openecomp.sdc.be.components.impl;
22
23 import fj.data.Either;
24 import org.openecomp.sdc.be.dao.api.ActionStatus;
25 import org.openecomp.sdc.be.impl.ComponentsUtils;
26 import org.openecomp.sdc.be.model.InterfaceDefinition;
27 import org.openecomp.sdc.be.model.Operation;
28 import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation;
29 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
30 import org.openecomp.sdc.common.log.wrappers.Logger;
31 import org.openecomp.sdc.exception.ResponseFormat;
32 import org.springframework.stereotype.Component;
33
34 import javax.annotation.Resource;
35 import java.util.*;
36
37 @Component("interfaceLifecycleTypeImportManager")
38 public class InterfaceLifecycleTypeImportManager {
39
40     @Resource
41     private IInterfaceLifecycleOperation interfaceLifecycleOperation;
42
43     @Resource
44     private ComponentsUtils componentsUtils;
45     @Resource
46     private CommonImportManager commonImportManager;
47
48     private static final Logger log = Logger.getLogger(InterfaceLifecycleTypeImportManager.class);
49
50     public Either<List<InterfaceDefinition>, ResponseFormat> createLifecycleTypes(String interfaceLifecycleTypesYml) {
51
52         Either<List<InterfaceDefinition>, ActionStatus> interfaces = createLifecyclyTypeFromYml(interfaceLifecycleTypesYml);
53         if (interfaces.isRight()) {
54             ActionStatus status = interfaces.right().value();
55             ResponseFormat responseFormat = componentsUtils.getResponseFormatByGroupType(status, null);
56             return Either.right(responseFormat);
57         }
58         return createInterfacesByDao(interfaces.left().value());
59
60     }
61
62     private Either<List<InterfaceDefinition>, ActionStatus> createLifecyclyTypeFromYml(String interfaceLifecycleTypesYml) {
63         return commonImportManager.createElementTypesFromYml(interfaceLifecycleTypesYml, this::createLifecycleType);
64
65     }
66
67     private Either<List<InterfaceDefinition>, ResponseFormat> createInterfacesByDao(List<InterfaceDefinition> interfacesToCreate) {
68         List<InterfaceDefinition> createdInterfaces = new ArrayList<>();
69         Either<List<InterfaceDefinition>, ResponseFormat> eitherResult = Either.left(createdInterfaces);
70         Iterator<InterfaceDefinition> interfaceItr = interfacesToCreate.iterator();
71         boolean stopDao = false;
72         while (interfaceItr.hasNext() && !stopDao) {
73             InterfaceDefinition interfaceDef = interfaceItr.next();
74
75             log.info("send interfaceDefinition {} to dao for create", interfaceDef.getType());
76
77             Either<InterfaceDefinition, StorageOperationStatus> dataModelResponse = interfaceLifecycleOperation.createInterfaceType(interfaceDef);
78             if (dataModelResponse.isRight()) {
79                 log.info("failed to create interface : {}  error: {}", interfaceDef.getType(), dataModelResponse.right().value().name());
80                 if (dataModelResponse.right().value() != StorageOperationStatus.SCHEMA_VIOLATION) {
81                     ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponseForLifecycleType(dataModelResponse.right().value()), interfaceDef.getType());
82                     eitherResult = Either.right(responseFormat);
83                     stopDao = true;
84                 }
85
86             } else {
87                 createdInterfaces.add(dataModelResponse.left().value());
88             }
89             if (!interfaceItr.hasNext()) {
90                 log.info("lifecycle types were created successfully!!!");
91             }
92         }
93         return eitherResult;
94     }
95
96     private InterfaceDefinition createLifecycleType(String interfaceDefinition, Map<String, Object> toscaJson) {
97         InterfaceDefinition interfaceDef = new InterfaceDefinition();
98         interfaceDef.setType(interfaceDefinition);
99
100         Map<String, Operation> operations = new HashMap<>();
101
102         for (Map.Entry<String, Object> entry : toscaJson.entrySet()) {
103             Operation operation = new Operation();
104             Map<String, Object> opProp = (Map<String, Object>) entry.getValue();
105
106             operation.setDescription((String) opProp.get("description"));
107             operations.put(entry.getKey(), operation);
108         }
109         interfaceDef.setOperationsMap(operations);
110         return interfaceDef;
111     }
112 }