Raise JUnit coverage common-be
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / GroupTypeImportManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.impl;
22
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28 import java.util.stream.Collectors;
29
30 import javax.annotation.Resource;
31
32 import org.apache.commons.lang3.tuple.ImmutablePair;
33 import org.openecomp.sdc.be.components.impl.CommonImportManager.ElementTypeEnum;
34 import org.openecomp.sdc.be.components.impl.ImportUtils.ToscaTagNamesEnum;
35 import org.openecomp.sdc.be.config.BeEcompErrorManager;
36 import org.openecomp.sdc.be.dao.api.ActionStatus;
37 import org.openecomp.sdc.be.impl.ComponentsUtils;
38 import org.openecomp.sdc.be.model.CapabilityTypeDefinition;
39 import org.openecomp.sdc.be.model.GroupTypeDefinition;
40 import org.openecomp.sdc.be.model.PropertyDefinition;
41 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
42 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
43 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
44 import org.openecomp.sdc.exception.ResponseFormat;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.springframework.stereotype.Component;
48
49 import fj.data.Either;
50
51 @Component("groupTypeImportManager")
52 public class GroupTypeImportManager {
53
54     public static void main(String[] args) {
55
56         List<PropertyDefinition> properties = new ArrayList<>();
57         PropertyDefinition propertyDefintion = new PropertyDefinition();
58         propertyDefintion.setName("aaa");
59         properties.add(propertyDefintion);
60
61         List<String> allParentsProps = new ArrayList<>();
62         allParentsProps.add("aaa");
63         allParentsProps.add("bbb");
64
65         Set<String> alreadyExistPropsCollection = properties.stream().filter(p -> allParentsProps.contains(p.getName())).map(p -> p.getName()).collect(Collectors.toSet());
66         System.out.println(alreadyExistPropsCollection);
67
68     }
69
70     private static final Logger log = LoggerFactory.getLogger(GroupTypeImportManager.class);
71     @Resource
72     private IGroupTypeOperation groupTypeOperation;
73     @Resource
74     private ComponentsUtils componentsUtils;
75     @Resource
76     private ToscaOperationFacade toscaOperationFacade;
77
78     @Resource
79     private CommonImportManager commonImportManager;
80
81     public Either<List<ImmutablePair<GroupTypeDefinition, Boolean>>, ResponseFormat> createGroupTypes(String groupTypesYml) {
82         return commonImportManager.createElementTypes(groupTypesYml, elementTypeYml -> createGroupTypesFromYml(elementTypeYml), groupTypesList -> createGroupTypesByDao(groupTypesList), ElementTypeEnum.GroupType);
83     }
84
85     private Either<List<GroupTypeDefinition>, ActionStatus> createGroupTypesFromYml(String groupTypesYml) {
86
87         return commonImportManager.createElementTypesFromYml(groupTypesYml, (groupTypeName, groupTypeJsonData) -> createGroupType(groupTypeName, groupTypeJsonData));
88     }
89
90     private Either<List<ImmutablePair<GroupTypeDefinition, Boolean>>, ResponseFormat> createGroupTypesByDao(List<GroupTypeDefinition> groupTypesToCreate) {
91         return commonImportManager.createElementTypesByDao(groupTypesToCreate, groupType -> validateGroupType(groupType), groupType -> new ImmutablePair<>(ElementTypeEnum.GroupType, groupType.getType()),
92                 groupTypeName -> groupTypeOperation.getLatestGroupTypeByType(groupTypeName), groupType -> groupTypeOperation.addGroupType(groupType), groupTypeOperation::upgradeGroupType);
93     }
94
95     private Either<ActionStatus, ResponseFormat> validateGroupType(GroupTypeDefinition groupType) {
96         Either<ActionStatus, ResponseFormat> result = Either.left(ActionStatus.OK);
97         if (groupType.getMembers() != null) {
98             if (groupType.getMembers().isEmpty()) {
99                 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GROUP_MEMBER_EMPTY, groupType.getType());
100                 result = Either.right(responseFormat);
101             } else {
102                 for (String member : groupType.getMembers()) {
103                     // Verify that such Resource exist
104                     Either<org.openecomp.sdc.be.model.Resource, StorageOperationStatus> eitherMemberExist = toscaOperationFacade.getLatestByToscaResourceName(member);
105                     if (eitherMemberExist.isRight()) {
106                         StorageOperationStatus operationStatus = eitherMemberExist.right().value();
107                         log.debug("Error when fetching parent resource {}, error: {}", member, operationStatus);
108                         ActionStatus convertFromStorageResponse = componentsUtils.convertFromStorageResponse(operationStatus);
109                         BeEcompErrorManager.getInstance().logBeComponentMissingError("Import GroupType", "resource", member);
110                         result = Either.right(componentsUtils.getResponseFormat(convertFromStorageResponse, member));
111                         break;
112                     }
113                 }
114
115             }
116         }
117         return result;
118     }
119
120     private GroupTypeDefinition createGroupType(String groupTypeName, Map<String, Object> toscaJson) {
121
122         GroupTypeDefinition groupType = new GroupTypeDefinition();
123
124         if (toscaJson != null) {
125             // Description
126             commonImportManager.setField(toscaJson, ToscaTagNamesEnum.DESCRIPTION.getElementName(), groupType::setDescription);
127             // Derived From
128             commonImportManager.setField(toscaJson, ToscaTagNamesEnum.DERIVED_FROM.getElementName(), groupType::setDerivedFrom);
129             // Properties
130             commonImportManager.setProperties(toscaJson, groupType::setProperties);
131             // Metadata
132             commonImportManager.setField(toscaJson, ToscaTagNamesEnum.METADATA.getElementName(), groupType::setMetadata);
133             // Capabilities
134             List<CapabilityTypeDefinition> capabilityTypes = createGroupCapabilityTypes(toscaJson);
135             groupType.setCapabilityTypes(capabilityTypes);
136             // Members
137             commonImportManager.setField(toscaJson, ToscaTagNamesEnum.MEMBERS.getElementName(), groupType::setMembers);
138
139             groupType.setType(groupTypeName);
140
141             groupType.setHighestVersion(true);
142
143             groupType.setVersion(ImportUtils.Constants.FIRST_CERTIFIED_VERSION_VERSION);
144         }
145         return groupType;
146     }
147
148     /**
149      * @param toscaJson
150      * @return
151      */
152     private List<CapabilityTypeDefinition> createGroupCapabilityTypes(Map<String, Object> toscaJson) {
153         CapabilityTypeToscaJsonHolder capabilityTypeToscaJsonHolder = new CapabilityTypeToscaJsonHolder();
154         commonImportManager.setField(toscaJson, ToscaTagNamesEnum.CAPABILITIES.getElementName(), capabilityTypeToscaJsonHolder::setCapabilityTypeToscaJson);
155         List<CapabilityTypeDefinition> capabilityTypes;
156         if (capabilityTypeToscaJsonHolder.isEmpty()) {
157             capabilityTypes = Collections.emptyList();
158         }
159         else {
160             capabilityTypes = commonImportManager.createElementTypesFromToscaJsonMap(this::createGroupCapabilityType, capabilityTypeToscaJsonHolder.getCapabilityTypeToscaJson());
161         }
162         return capabilityTypes;
163     }
164     
165     private class CapabilityTypeToscaJsonHolder {
166         private Map<String, Object> capabilityTypeToscaJson;
167
168         public Map<String, Object> getCapabilityTypeToscaJson() {
169             return capabilityTypeToscaJson;
170         }
171         
172         public boolean isEmpty() {
173             return capabilityTypeToscaJson == null;
174         }
175
176         public void setCapabilityTypeToscaJson(Map<String, Object> capabilityTypeToscaJson) {
177             this.capabilityTypeToscaJson = capabilityTypeToscaJson;
178         }
179     }
180     
181     private CapabilityTypeDefinition createGroupCapabilityType(String capabilityTypeName, Map<String, Object> toscaJson) {
182         CapabilityTypeDefinition capabilityType = new CapabilityTypeDefinition();
183
184         commonImportManager.setField(toscaJson, ToscaTagNamesEnum.TYPE.getElementName(), capabilityType::setType);
185         // Properties
186         commonImportManager.setPropertiesMap(toscaJson, capabilityType::setProperties);
187
188         return capabilityType;
189     }
190
191 }