aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/InterfaceLifecycleTypeImportManager.java
blob: 7a1c96e1ef8af5d4ac49e68a48ddd7bcd276ab07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.be.components.impl;

import fj.data.Either;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.model.InterfaceDefinition;
import org.openecomp.sdc.be.model.Operation;
import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.common.log.wrappers.Logger;
import org.openecomp.sdc.exception.ResponseFormat;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

@Component("interfaceLifecycleTypeImportManager")
public class InterfaceLifecycleTypeImportManager {

    @Resource
    private IInterfaceLifecycleOperation interfaceLifecycleOperation;

    @Resource
    private ComponentsUtils componentsUtils;
    @Resource
    private CommonImportManager commonImportManager;

    private static final Logger log = Logger.getLogger(InterfaceLifecycleTypeImportManager.class);

    public Either<List<InterfaceDefinition>, ResponseFormat> createLifecycleTypes(String interfaceLifecycleTypesYml) {

        Either<List<InterfaceDefinition>, ActionStatus> interfaces = createLifecyclyTypeFromYml(interfaceLifecycleTypesYml);
        if (interfaces.isRight()) {
            ActionStatus status = interfaces.right().value();
            ResponseFormat responseFormat = componentsUtils.getResponseFormatByGroupType(status, null);
            return Either.right(responseFormat);
        }
        return createInterfacesByDao(interfaces.left().value());

    }

    private Either<List<InterfaceDefinition>, ActionStatus> createLifecyclyTypeFromYml(String interfaceLifecycleTypesYml) {
        return commonImportManager.createElementTypesFromYml(interfaceLifecycleTypesYml, this::createLifecycleType);

    }

    private Either<List<InterfaceDefinition>, ResponseFormat> createInterfacesByDao(List<InterfaceDefinition> interfacesToCreate) {
        List<InterfaceDefinition> createdInterfaces = new ArrayList<>();
        Either<List<InterfaceDefinition>, ResponseFormat> eitherResult = Either.left(createdInterfaces);
        Iterator<InterfaceDefinition> interfaceItr = interfacesToCreate.iterator();
        boolean stopDao = false;
        while (interfaceItr.hasNext() && !stopDao) {
            InterfaceDefinition interfaceDef = interfaceItr.next();

            log.info("send interfaceDefinition {} to dao for create", interfaceDef.getType());

            Either<InterfaceDefinition, StorageOperationStatus> dataModelResponse = interfaceLifecycleOperation.createInterfaceType(interfaceDef);
            if (dataModelResponse.isRight()) {
                log.info("failed to create interface : {}  error: {}", interfaceDef.getType(), dataModelResponse.right().value().name());
                if (dataModelResponse.right().value() != StorageOperationStatus.SCHEMA_VIOLATION) {
                    ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponseForLifecycleType(dataModelResponse.right().value()), interfaceDef.getType());
                    eitherResult = Either.right(responseFormat);
                    stopDao = true;
                }

            } else {
                createdInterfaces.add(dataModelResponse.left().value());
            }
            if (!interfaceItr.hasNext()) {
                log.info("lifecycle types were created successfully!!!");
            }
        }
        return eitherResult;
    }

    private InterfaceDefinition createLifecycleType(String interfaceDefinition, Map<String, Object> toscaJson) {
        InterfaceDefinition interfaceDef = new InterfaceDefinition();
        interfaceDef.setType(interfaceDefinition);

        Map<String, Operation> operations = new HashMap<>();

        for (Map.Entry<String, Object> entry : toscaJson.entrySet()) {
            Operation operation = new Operation();
            Map<String, Object> opProp = (Map<String, Object>) entry.getValue();

            operation.setDescription((String) opProp.get("description"));
            operations.put(entry.getKey(), operation);
        }
        interfaceDef.setOperationsMap(operations);
        return interfaceDef;
    }
}