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.apache.commons.lang3.tuple.ImmutablePair;
25 import org.openecomp.sdc.be.components.impl.CommonImportManager.ElementTypeEnum;
26 import org.openecomp.sdc.be.components.impl.model.ToscaTypeImportData;
27 import org.openecomp.sdc.be.config.BeEcompErrorManager;
28 import org.openecomp.sdc.be.dao.api.ActionStatus;
29 import org.openecomp.sdc.be.impl.ComponentsUtils;
30 import org.openecomp.sdc.be.model.CapabilityDefinition;
31 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
32 import org.openecomp.sdc.be.model.GroupTypeDefinition;
33 import org.openecomp.sdc.be.model.PropertyDefinition;
34 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
35 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
36 import org.openecomp.sdc.be.model.operations.impl.GroupTypeOperation;
37 import org.openecomp.sdc.be.model.utils.TypeCompareUtils;
38 import org.openecomp.sdc.be.utils.TypeUtils;
39 import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum;
40 import org.openecomp.sdc.common.log.wrappers.Logger;
41 import org.openecomp.sdc.exception.ResponseFormat;
42 import org.springframework.stereotype.Component;
44 import java.util.Collections;
45 import java.util.List;
47 import java.util.stream.Collectors;
49 @Component("groupTypeImportManager")
50 public class GroupTypeImportManager {
52 private static final Logger log = Logger.getLogger(GroupTypeImportManager.class);
53 private final GroupTypeOperation groupTypeOperation;
54 private final ComponentsUtils componentsUtils;
55 private final ToscaOperationFacade toscaOperationFacade;
56 private final CommonImportManager commonImportManager;
58 public GroupTypeImportManager(GroupTypeOperation groupTypeOperation, ComponentsUtils componentsUtils, ToscaOperationFacade toscaOperationFacade, CommonImportManager commonImportManager) {
59 this.groupTypeOperation = groupTypeOperation;
60 this.componentsUtils = componentsUtils;
61 this.toscaOperationFacade = toscaOperationFacade;
62 this.commonImportManager = commonImportManager;
65 public Either<List<ImmutablePair<GroupTypeDefinition, Boolean>>, ResponseFormat> createGroupTypes(ToscaTypeImportData toscaTypeImportData) {
66 return commonImportManager.createElementTypes(toscaTypeImportData, this::createGroupTypesFromYml, this::upsertGroupTypesByDao);
69 private Either<List<GroupTypeDefinition>, ActionStatus> createGroupTypesFromYml(String groupTypesYml) {
70 return commonImportManager.createElementTypesFromYml(groupTypesYml, this::createGroupType);
73 private Either<List<ImmutablePair<GroupTypeDefinition, Boolean>>, ResponseFormat> upsertGroupTypesByDao(List<GroupTypeDefinition> groupTypesToCreate) {
74 return commonImportManager.createElementTypesByDao(groupTypesToCreate, this::validateGroupType, groupType -> new ImmutablePair<>(ElementTypeEnum.GROUP_TYPE, groupType.getType()),
75 groupTypeOperation::getLatestGroupTypeByType, groupTypeOperation::addGroupType, this::updateGroupType);
78 private Either<GroupTypeDefinition, StorageOperationStatus> updateGroupType(GroupTypeDefinition newGroupType, GroupTypeDefinition oldGroupType) {
79 Either<GroupTypeDefinition, StorageOperationStatus> validationRes = groupTypeOperation.validateUpdateProperties(newGroupType);
80 if (validationRes.isRight()) {
81 log.error("#updateGroupType - One or all properties of group type {} not valid. status is {}", newGroupType, validationRes.right().value());
85 if (TypeCompareUtils.isGroupTypesEquals(newGroupType, oldGroupType)) {
86 return TypeCompareUtils.typeAlreadyExists();
89 return groupTypeOperation.updateGroupType(newGroupType, oldGroupType);
92 private Either<ActionStatus, ResponseFormat> validateGroupType(GroupTypeDefinition groupType) {
93 Either<ActionStatus, ResponseFormat> result = Either.left(ActionStatus.OK);
94 if (groupType.getMembers() != null) {
95 if (groupType.getMembers().isEmpty()) {
96 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GROUP_MEMBER_EMPTY, groupType.getType());
97 result = Either.right(responseFormat);
99 for (String member : groupType.getMembers()) {
100 // Verify that such Resource exist
101 Either<org.openecomp.sdc.be.model.Resource, StorageOperationStatus> eitherMemberExist = toscaOperationFacade.getLatestByToscaResourceName(member);
102 if (eitherMemberExist.isRight()) {
103 StorageOperationStatus operationStatus = eitherMemberExist.right().value();
104 log.debug("Error when fetching parent resource {}, error: {}", member, operationStatus);
105 ActionStatus convertFromStorageResponse = componentsUtils.convertFromStorageResponse(operationStatus);
106 BeEcompErrorManager.getInstance().logBeComponentMissingError("Import GroupType", "resource", member);
107 result = Either.right(componentsUtils.getResponseFormat(convertFromStorageResponse, member));
117 private GroupTypeDefinition createGroupType(String groupTypeName, Map<String, Object> toscaJson) {
119 GroupTypeDefinition groupType = new GroupTypeDefinition();
121 if (toscaJson != null) {
123 commonImportManager.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.DESCRIPTION.getElementName(), groupType::setDescription);
125 commonImportManager.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.DERIVED_FROM.getElementName(), groupType::setDerivedFrom);
127 CommonImportManager.setProperties(toscaJson, groupType::setProperties);
129 commonImportManager.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.METADATA.getElementName(), groupType::setMetadata);
131 Map<String, CapabilityDefinition> capabilities = createCapabilities(toscaJson);
132 groupType.setCapabilities(capabilities);
134 commonImportManager.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.MEMBERS.getElementName(), groupType::setMembers);
136 groupType.setType(groupTypeName);
138 groupType.setHighestVersion(true);
140 groupType.setVersion(TypeUtils.getFirstCertifiedVersionVersion());
149 private Map<String, CapabilityDefinition> createCapabilities(Map<String, Object> toscaJson) {
150 CapabilityTypeToscaJsonHolder capabilityTypeToscaJsonHolder = new CapabilityTypeToscaJsonHolder();
151 commonImportManager.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.CAPABILITIES.getElementName(), capabilityTypeToscaJsonHolder::setCapabilityTypeToscaJson);
152 Map<String, CapabilityDefinition> capabilities;
153 if (capabilityTypeToscaJsonHolder.isEmpty()) {
154 capabilities = Collections.emptyMap();
157 capabilities = commonImportManager.createElementTypesMapFromToscaJsonMap(this::createCapability, capabilityTypeToscaJsonHolder.getCapabilityTypeToscaJson());
162 private class CapabilityTypeToscaJsonHolder {
163 private Map<String, Object> capabilityTypeToscaJson;
165 public Map<String, Object> getCapabilityTypeToscaJson() {
166 return capabilityTypeToscaJson;
169 public boolean isEmpty() {
170 return capabilityTypeToscaJson == null;
173 public void setCapabilityTypeToscaJson(Map<String, Object> capabilityTypeToscaJson) {
174 this.capabilityTypeToscaJson = capabilityTypeToscaJson;
178 private CapabilityDefinition createCapability(String capabilityName, Map<String, Object> toscaJson) {
179 CapabilityDefinition capability = new CapabilityDefinition();
181 capability.setName(capabilityName);
182 commonImportManager.setField(toscaJson, ToscaTagNamesEnum.TYPE.getElementName(), capability::setType);
184 CommonImportManager.setProperties(toscaJson, pl -> capability.setProperties(map(pl)));
193 private List<ComponentInstanceProperty> map(List<PropertyDefinition> pl) {
195 .map(ComponentInstanceProperty::new)
196 .collect(Collectors.toList());