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=========================================================
20 package org.openecomp.sdc.be.components.impl;
22 import fj.data.Either;
23 import java.util.Collections;
24 import java.util.List;
26 import java.util.stream.Collectors;
27 import org.apache.commons.lang3.tuple.ImmutablePair;
28 import org.openecomp.sdc.be.components.impl.CommonImportManager.ElementTypeEnum;
29 import org.openecomp.sdc.be.components.impl.model.ToscaTypeImportData;
30 import org.openecomp.sdc.be.config.BeEcompErrorManager;
31 import org.openecomp.sdc.be.dao.api.ActionStatus;
32 import org.openecomp.sdc.be.impl.ComponentsUtils;
33 import org.openecomp.sdc.be.model.CapabilityDefinition;
34 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
35 import org.openecomp.sdc.be.model.GroupTypeDefinition;
36 import org.openecomp.sdc.be.model.PropertyDefinition;
37 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
38 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
39 import org.openecomp.sdc.be.model.operations.impl.GroupTypeOperation;
40 import org.openecomp.sdc.be.model.utils.TypeCompareUtils;
41 import org.openecomp.sdc.be.utils.TypeUtils;
42 import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum;
43 import org.openecomp.sdc.common.log.wrappers.Logger;
44 import org.openecomp.sdc.exception.ResponseFormat;
45 import org.springframework.stereotype.Component;
47 @Component("groupTypeImportManager")
48 public class GroupTypeImportManager {
50 private static final Logger log = Logger.getLogger(GroupTypeImportManager.class);
51 private final GroupTypeOperation groupTypeOperation;
52 private final ComponentsUtils componentsUtils;
53 private final ToscaOperationFacade toscaOperationFacade;
54 private final CommonImportManager commonImportManager;
56 public GroupTypeImportManager(GroupTypeOperation groupTypeOperation, ComponentsUtils componentsUtils, ToscaOperationFacade toscaOperationFacade,
57 CommonImportManager commonImportManager) {
58 this.groupTypeOperation = groupTypeOperation;
59 this.componentsUtils = componentsUtils;
60 this.toscaOperationFacade = toscaOperationFacade;
61 this.commonImportManager = commonImportManager;
64 public Either<List<ImmutablePair<GroupTypeDefinition, Boolean>>, ResponseFormat> createGroupTypes(ToscaTypeImportData toscaTypeImportData) {
65 return commonImportManager.createElementTypes(toscaTypeImportData, this::createGroupTypesFromYml, this::upsertGroupTypesByDao);
68 private Either<List<GroupTypeDefinition>, ActionStatus> createGroupTypesFromYml(String groupTypesYml) {
69 return commonImportManager.createElementTypesFromYml(groupTypesYml, this::createGroupType);
72 private Either<List<ImmutablePair<GroupTypeDefinition, Boolean>>, ResponseFormat> upsertGroupTypesByDao(
73 List<GroupTypeDefinition> groupTypesToCreate) {
74 return commonImportManager.createElementTypesByDao(groupTypesToCreate, this::validateGroupType,
75 groupType -> new ImmutablePair<>(ElementTypeEnum.GROUP_TYPE, groupType.getType()), groupTypeOperation::getLatestGroupTypeByType,
76 groupTypeOperation::addGroupType, this::updateGroupType);
79 private Either<GroupTypeDefinition, StorageOperationStatus> updateGroupType(GroupTypeDefinition newGroupType, GroupTypeDefinition oldGroupType) {
80 Either<GroupTypeDefinition, StorageOperationStatus> validationRes = groupTypeOperation.validateUpdateProperties(newGroupType);
81 if (validationRes.isRight()) {
82 log.error("#updateGroupType - One or all properties of group type {} not valid. status is {}", newGroupType,
83 validationRes.right().value());
86 if (TypeCompareUtils.isGroupTypesEquals(newGroupType, oldGroupType)) {
87 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
102 .getLatestByToscaResourceName(member);
103 if (eitherMemberExist.isRight()) {
104 StorageOperationStatus operationStatus = eitherMemberExist.right().value();
105 log.debug("Error when fetching parent resource {}, error: {}", member, operationStatus);
106 ActionStatus convertFromStorageResponse = componentsUtils.convertFromStorageResponse(operationStatus);
107 BeEcompErrorManager.getInstance().logBeComponentMissingError("Import GroupType", "resource", member);
108 result = Either.right(componentsUtils.getResponseFormat(convertFromStorageResponse, member));
117 private GroupTypeDefinition createGroupType(String groupTypeName, Map<String, Object> toscaJson) {
118 GroupTypeDefinition groupType = new GroupTypeDefinition();
119 if (toscaJson != null) {
121 commonImportManager.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.DESCRIPTION.getElementName(), groupType::setDescription);
123 commonImportManager.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.DERIVED_FROM.getElementName(), groupType::setDerivedFrom);
125 CommonImportManager.setProperties(toscaJson, groupType::setProperties);
127 commonImportManager.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.METADATA.getElementName(), groupType::setMetadata);
129 Map<String, CapabilityDefinition> capabilities = createCapabilities(toscaJson);
130 groupType.setCapabilities(capabilities);
132 commonImportManager.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.MEMBERS.getElementName(), groupType::setMembers);
133 groupType.setType(groupTypeName);
134 groupType.setHighestVersion(true);
135 groupType.setVersion(TypeUtils.getFirstCertifiedVersionVersion());
144 private Map<String, CapabilityDefinition> createCapabilities(Map<String, Object> toscaJson) {
145 CapabilityTypeToscaJsonHolder capabilityTypeToscaJsonHolder = new CapabilityTypeToscaJsonHolder();
146 commonImportManager.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.CAPABILITIES.getElementName(),
147 capabilityTypeToscaJsonHolder::setCapabilityTypeToscaJson);
148 Map<String, CapabilityDefinition> capabilities;
149 if (capabilityTypeToscaJsonHolder.isEmpty()) {
150 capabilities = Collections.emptyMap();
152 capabilities = commonImportManager
153 .createElementTypesMapFromToscaJsonMap(this::createCapability, capabilityTypeToscaJsonHolder.getCapabilityTypeToscaJson());
158 private CapabilityDefinition createCapability(String capabilityName, Map<String, Object> toscaJson) {
159 CapabilityDefinition capability = new CapabilityDefinition();
160 capability.setName(capabilityName);
161 commonImportManager.setField(toscaJson, ToscaTagNamesEnum.TYPE.getElementName(), capability::setType);
163 CommonImportManager.setProperties(toscaJson, pl -> capability.setProperties(map(pl)));
171 private List<ComponentInstanceProperty> map(List<PropertyDefinition> pl) {
172 return pl.stream().map(ComponentInstanceProperty::new).collect(Collectors.toList());
175 private class CapabilityTypeToscaJsonHolder {
177 private Map<String, Object> capabilityTypeToscaJson;
179 public Map<String, Object> getCapabilityTypeToscaJson() {
180 return capabilityTypeToscaJson;
183 public void setCapabilityTypeToscaJson(Map<String, Object> capabilityTypeToscaJson) {
184 this.capabilityTypeToscaJson = capabilityTypeToscaJson;
187 public boolean isEmpty() {
188 return capabilityTypeToscaJson == null;