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.openecomp.sdc.be.components.impl;
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;
34 import javax.annotation.Resource;
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.Iterator;
38 import java.util.List;
41 @Component("interfaceLifecycleTypeImportManager")
42 public class InterfaceLifecycleTypeImportManager {
45 private IInterfaceLifecycleOperation interfaceLifecycleOperation;
48 private ComponentsUtils componentsUtils;
50 private CommonImportManager commonImportManager;
52 private static final Logger log = Logger.getLogger(InterfaceLifecycleTypeImportManager.class);
54 public Either<List<InterfaceDefinition>, ResponseFormat> createLifecycleTypes(String interfaceLifecycleTypesYml) {
56 Either<List<InterfaceDefinition>, ActionStatus> interfaces = createLifecyclyTypeFromYml(interfaceLifecycleTypesYml);
57 if (interfaces.isRight()) {
58 ActionStatus status = interfaces.right().value();
59 ResponseFormat responseFormat = componentsUtils.getResponseFormatByGroupType(status, null);
60 return Either.right(responseFormat);
62 return createInterfacesByDao(interfaces.left().value());
66 private Either<List<InterfaceDefinition>, ActionStatus> createLifecyclyTypeFromYml(String interfaceLifecycleTypesYml) {
67 return commonImportManager.createElementTypesFromYml(interfaceLifecycleTypesYml, this::createLifecycleType);
71 private Either<List<InterfaceDefinition>, ResponseFormat> createInterfacesByDao(List<InterfaceDefinition> interfacesToCreate) {
72 List<InterfaceDefinition> createdInterfaces = new ArrayList<>();
73 Either<List<InterfaceDefinition>, ResponseFormat> eitherResult = Either.left(createdInterfaces);
74 Iterator<InterfaceDefinition> interfaceItr = interfacesToCreate.iterator();
75 boolean stopDao = false;
76 while (interfaceItr.hasNext() && !stopDao) {
77 InterfaceDefinition interfaceDef = interfaceItr.next();
79 log.info("send interfaceDefinition {} to dao for create", interfaceDef.getType());
81 Either<InterfaceDefinition, StorageOperationStatus> dataModelResponse = interfaceLifecycleOperation.createInterfaceType(interfaceDef);
82 if (dataModelResponse.isRight()) {
83 log.info("failed to create interface : {} error: {}", interfaceDef.getType(), dataModelResponse.right().value().name());
84 if (dataModelResponse.right().value() != StorageOperationStatus.SCHEMA_VIOLATION) {
85 ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponseForLifecycleType(dataModelResponse.right().value()), interfaceDef.getType());
86 eitherResult = Either.right(responseFormat);
91 createdInterfaces.add(dataModelResponse.left().value());
93 if (!interfaceItr.hasNext()) {
94 log.info("lifecycle types were created successfully!!!");
100 private InterfaceDefinition createLifecycleType(String interfaceDefinition, Map<String, Object> toscaJson) {
101 InterfaceDefinition interfaceDef = new InterfaceDefinition();
102 interfaceDef.setType(interfaceDefinition);
104 Map<String, Operation> operations = new HashMap<>();
106 for (Map.Entry<String, Object> entry : toscaJson.entrySet()) {
107 Operation operation = new Operation();
108 Map<String, Object> opProp = (Map<String, Object>) entry.getValue();
110 operation.setDescription((String) opProp.get("description"));
111 operations.put(entry.getKey(), operation);
113 interfaceDef.setOperationsMap(operations);