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;
37 @Component("interfaceLifecycleTypeImportManager")
38 public class InterfaceLifecycleTypeImportManager {
41 private IInterfaceLifecycleOperation interfaceLifecycleOperation;
44 private ComponentsUtils componentsUtils;
46 private CommonImportManager commonImportManager;
48 private static final Logger log = Logger.getLogger(InterfaceLifecycleTypeImportManager.class);
50 public Either<List<InterfaceDefinition>, ResponseFormat> createLifecycleTypes(String interfaceLifecycleTypesYml) {
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);
58 return createInterfacesByDao(interfaces.left().value());
62 private Either<List<InterfaceDefinition>, ActionStatus> createLifecyclyTypeFromYml(String interfaceLifecycleTypesYml) {
63 return commonImportManager.createElementTypesFromYml(interfaceLifecycleTypesYml, this::createLifecycleType);
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();
75 log.info("send interfaceDefinition {} to dao for create", interfaceDef.getType());
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);
87 createdInterfaces.add(dataModelResponse.left().value());
89 if (!interfaceItr.hasNext()) {
90 log.info("lifecycle types were created successfully!!!");
96 private InterfaceDefinition createLifecycleType(String interfaceDefinition, Map<String, Object> toscaJson) {
97 InterfaceDefinition interfaceDef = new InterfaceDefinition();
98 interfaceDef.setType(interfaceDefinition);
100 Map<String, Operation> operations = new HashMap<>();
102 for (Map.Entry<String, Object> entry : toscaJson.entrySet()) {
103 Operation operation = new Operation();
104 Map<String, Object> opProp = (Map<String, Object>) entry.getValue();
106 operation.setDescription((String) opProp.get("description"));
107 operations.put(entry.getKey(), operation);
109 interfaceDef.setOperationsMap(operations);