0f55ddfff15e0d98b73cd9cd70bb7061fa137fff
[sdc.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.openecomp.sdc.be.components.impl;
21
22 import static org.openecomp.sdc.be.dao.api.ActionStatus.SUBSTITUTION_FILTER_NOT_FOUND;
23 import static org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR;
24
25 import fj.data.Either;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.List;
29 import java.util.Optional;
30 import java.util.stream.Collectors;
31 import org.openecomp.sdc.be.components.impl.exceptions.BusinessLogicException;
32 import org.openecomp.sdc.be.components.impl.utils.NodeFilterConstraintAction;
33 import org.openecomp.sdc.be.components.validation.NodeFilterValidator;
34 import org.openecomp.sdc.be.dao.api.ActionStatus;
35 import org.openecomp.sdc.be.datatypes.elements.RequirementSubstitutionFilterPropertyDataDefinition;
36 import org.openecomp.sdc.be.datatypes.elements.SubstitutionFilterDataDefinition;
37 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
38 import org.openecomp.sdc.be.model.Component;
39 import org.openecomp.sdc.be.model.User;
40 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations;
41 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
42 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.SubstitutionFilterOperation;
43 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
44 import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
45 import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
46 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
47 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
48 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
49 import org.openecomp.sdc.be.user.Role;
50 import org.openecomp.sdc.common.log.wrappers.Logger;
51 import org.openecomp.sdc.exception.ResponseFormat;
52 import org.springframework.beans.factory.annotation.Autowired;
53
54 @org.springframework.stereotype.Component("componentSubstitutionFilterBusinessLogic")
55 public class ComponentSubstitutionFilterBusinessLogic extends BaseBusinessLogic {
56
57     private static final Logger LOGGER = Logger.getLogger(ComponentSubstitutionFilterBusinessLogic.class);
58
59     private final SubstitutionFilterOperation substitutionFilterOperation;
60     private final NodeFilterValidator nodeFilterValidator;
61
62     @Autowired
63     public ComponentSubstitutionFilterBusinessLogic(final IElementOperation elementDao,
64                                                     final IGroupOperation groupOperation,
65                                                     final IGroupInstanceOperation groupInstanceOperation,
66                                                     final IGroupTypeOperation groupTypeOperation,
67                                                     final InterfaceOperation interfaceOperation,
68                                                     final InterfaceLifecycleOperation interfaceLifecycleTypeOperation,
69                                                     final ArtifactsOperations artifactToscaOperation,
70                                                     final SubstitutionFilterOperation substitutionFilterOperation,
71                                                     final NodeFilterValidator nodeFilterValidator) {
72         super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation, interfaceOperation,
73             interfaceLifecycleTypeOperation, artifactToscaOperation);
74         this.substitutionFilterOperation = substitutionFilterOperation;
75         this.nodeFilterValidator = nodeFilterValidator;
76     }
77
78     public Optional<SubstitutionFilterDataDefinition> createSubstitutionFilterIfNotExist(final String componentId,
79                                                                                          final boolean shouldLock,
80                                                                                          final ComponentTypeEnum componentTypeEnum)
81             throws BusinessLogicException {
82
83         final Component component = getComponent(componentId);
84         Optional<SubstitutionFilterDataDefinition> substitutionFilterDataDefinition = Optional.ofNullable(component.getSubstitutionFilter());
85         if (substitutionFilterDataDefinition.isPresent()) {
86             return substitutionFilterDataDefinition;
87         }
88         boolean wasLocked = false;
89         try {
90             if (shouldLock) {
91                 lockComponent(component.getUniqueId(), component, "Create Substitution Filter on component");
92                 wasLocked = true;
93             }
94             final Either<SubstitutionFilterDataDefinition, StorageOperationStatus> result = substitutionFilterOperation
95                     .createSubstitutionFilter(componentId);
96             if (result.isRight()) {
97                 janusGraphDao.rollback();
98                 LOGGER.error(BUSINESS_PROCESS_ERROR,
99                         "Failed to Create Substitution filter on component with id {}", componentId);
100                 throw new BusinessLogicException(componentsUtils.getResponseFormatByResource(componentsUtils
101                         .convertFromStorageResponse(result.right().value()), component.getSystemName()));
102             }
103             substitutionFilterDataDefinition = Optional.ofNullable(result.left().value());
104             component.setSubstitutionFilter(substitutionFilterDataDefinition.get());
105             janusGraphDao.commit();
106             LOGGER.debug("Substitution filter successfully created in component {} . ", component.getSystemName());
107         } catch (final Exception e) {
108             janusGraphDao.rollback();
109             LOGGER.error(BUSINESS_PROCESS_ERROR,
110                     "Exception occurred during add Component Substitution filter property values: {}", e.getMessage(), e);
111             throw new BusinessLogicException(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
112
113         } finally {
114             if (wasLocked) {
115                 unlockComponent(component.getUniqueId(), componentTypeEnum);
116             }
117         }
118
119         return substitutionFilterDataDefinition;
120     }
121
122     public Optional<SubstitutionFilterDataDefinition> addSubstitutionFilter(final String componentId,
123                                                                             final String propertyName,
124                                                                             final String constraint,
125                                                                             final boolean shouldLock,
126                                                                             final ComponentTypeEnum componentTypeEnum)
127         throws BusinessLogicException {
128
129         final Component component = getComponent(componentId);
130
131         final Either<Boolean, ResponseFormat> response = nodeFilterValidator
132                 .validateComponentFilter(component, Collections.singletonList(constraint), NodeFilterConstraintAction.ADD);
133         if (response.isRight()) {
134             throw new BusinessLogicException(componentsUtils
135                     .getResponseFormat(ActionStatus.SUBSTITUTION_FILTER_NOT_FOUND, response.right().value().getFormattedMessage()));
136         }
137
138         boolean wasLocked = false;
139         try {
140             if (shouldLock) {
141                 lockComponent(component.getUniqueId(), component, "Add Substitution Filter on Component");
142                 wasLocked = true;
143             }
144             final RequirementSubstitutionFilterPropertyDataDefinition newProperty =
145                 new RequirementSubstitutionFilterPropertyDataDefinition();
146             newProperty.setName(propertyName);
147             newProperty.setConstraints(Collections.singletonList(constraint));
148             final Either<SubstitutionFilterDataDefinition, StorageOperationStatus> resultEither =
149                 substitutionFilterOperation
150                     .addPropertyFilter(componentId, component.getSubstitutionFilter(), newProperty);
151
152             if (resultEither.isRight()) {
153                 janusGraphDao.rollback();
154                 throw new BusinessLogicException(componentsUtils.getResponseFormatByResource(componentsUtils
155                     .convertFromStorageResponse(resultEither.right().value()), component.getSystemName()));
156             }
157
158             janusGraphDao.commit();
159             LOGGER.debug("Substitution filter successfully created in component {} . ", component.getSystemName());
160             return Optional.ofNullable(resultEither.left().value());
161         } catch (final Exception e) {
162             janusGraphDao.rollback();
163             LOGGER.error(BUSINESS_PROCESS_ERROR,
164                 "Exception occurred during add component substitution filter property values: {}", e.getMessage(), e);
165             throw new BusinessLogicException(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
166
167         } finally {
168             if (wasLocked) {
169                 unlockComponent(component.getUniqueId(), componentTypeEnum);
170             }
171         }
172
173     }
174
175     public Optional<SubstitutionFilterDataDefinition> updateSubstitutionFilter(final String componentId,
176                                                                                final List<String> constraints,
177                                                                                final boolean shouldLock,
178                                                                                final ComponentTypeEnum componentTypeEnum)
179         throws BusinessLogicException {
180
181         final Component component = getComponent(componentId);
182
183         final Either<Boolean, ResponseFormat> response = nodeFilterValidator
184                 .validateComponentFilter(component, constraints, NodeFilterConstraintAction.UPDATE);
185         if (response.isRight()) {
186             throw new BusinessLogicException(componentsUtils
187                     .getResponseFormat(ActionStatus.SUBSTITUTION_FILTER_NOT_FOUND, response.right().value().getFormattedMessage()));
188         }
189
190         SubstitutionFilterDataDefinition substitutionFilterDataDefinition = component.getSubstitutionFilter();
191         if (substitutionFilterDataDefinition == null) {
192             throw new BusinessLogicException(componentsUtils.getResponseFormat(SUBSTITUTION_FILTER_NOT_FOUND));
193         }
194         boolean wasLocked = false;
195         try {
196             if (shouldLock) {
197                 lockComponent(component.getUniqueId(), component, "Update Substitution Filter on Component");
198                 wasLocked = true;
199             }
200             final List<RequirementSubstitutionFilterPropertyDataDefinition> properties = constraints.stream()
201                     .map(this::getRequirementSubstitutionFilterPropertyDataDefinition).collect(Collectors.toList());
202             final Either<SubstitutionFilterDataDefinition, StorageOperationStatus> result = substitutionFilterOperation
203                     .updateProperties(componentId, substitutionFilterDataDefinition, properties);
204
205             if (result.isRight()) {
206                 janusGraphDao.rollback();
207                 throw new BusinessLogicException(componentsUtils.getResponseFormatByResource(componentsUtils
208                     .convertFromStorageResponse(result.right().value()), component.getSystemName()));
209             } else {
210                 substitutionFilterDataDefinition = result.left().value();
211             }
212             janusGraphDao.commit();
213             LOGGER.debug("Substitution filter successfully updated in component {} . ", component.getSystemName());
214
215         } catch (final Exception e) {
216             janusGraphDao.rollback();
217             LOGGER.error(BUSINESS_PROCESS_ERROR, this.getClass().getName(),
218                 "Exception occurred during update component substitution filter property values: {}", e);
219             throw new BusinessLogicException(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
220
221         } finally {
222             if (wasLocked) {
223                 unlockComponent(component.getUniqueId(), componentTypeEnum);
224             }
225         }
226         return Optional.ofNullable(substitutionFilterDataDefinition);
227     }
228
229     public Optional<SubstitutionFilterDataDefinition> deleteSubstitutionFilter(final String componentId,
230                                                                                final int position,
231                                                                                final boolean shouldLock,
232                                                                                final ComponentTypeEnum componentTypeEnum)
233         throws BusinessLogicException {
234
235         final Component component = getComponent(componentId);
236         SubstitutionFilterDataDefinition substitutionFilterDataDefinition = component.getSubstitutionFilter();
237         boolean wasLocked = false;
238         try {
239             if (shouldLock) {
240                 lockComponent(component.getUniqueId(), component,"Delete substitution Filter on Component");
241                 wasLocked = true;
242             }
243             final Either<SubstitutionFilterDataDefinition, StorageOperationStatus> result = substitutionFilterOperation
244                 .deleteConstraint(componentId, substitutionFilterDataDefinition, position);
245             if (result.isRight()) {
246                 janusGraphDao.rollback();
247                 throw new BusinessLogicException(componentsUtils.getResponseFormatByResource(componentsUtils
248                     .convertFromStorageResponse(result.right().value()), component.getSystemName()));
249             } else {
250                 substitutionFilterDataDefinition = result.left().value();
251             }
252             janusGraphDao.commit();
253             LOGGER.debug("Substitution filter successfully deleted in component {} . ", component.getSystemName());
254
255         } catch (final Exception e) {
256             janusGraphDao.rollback();
257             LOGGER.error(BUSINESS_PROCESS_ERROR,
258                 "Exception occurred during delete component substitution filter property values: {}",
259                 e.getMessage(), e);
260             throw new BusinessLogicException(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
261
262         } finally {
263             if (wasLocked) {
264                 unlockComponent(component.getUniqueId(), componentTypeEnum);
265             }
266         }
267         return Optional.ofNullable(substitutionFilterDataDefinition);
268     }
269
270     private void unlockComponent(final String componentUniqueId,
271                                  final ComponentTypeEnum componentType) {
272         graphLockOperation.unlockComponent(componentUniqueId, componentType.getNodeType());
273     }
274
275     public User validateUser(final String userId) {
276         final User user = userValidations.validateUserExists(userId);
277         userValidations.validateUserRole(user, Arrays.asList(Role.DESIGNER, Role.ADMIN));
278         return user;
279     }
280
281     private RequirementSubstitutionFilterPropertyDataDefinition getRequirementSubstitutionFilterPropertyDataDefinition(
282         final String constraint) {
283
284         final RequirementSubstitutionFilterPropertyDataDefinition requirementSubstitutionFilterPropertyDataDefinition =
285             new RequirementSubstitutionFilterPropertyDataDefinition();
286         requirementSubstitutionFilterPropertyDataDefinition.setConstraints(Arrays.asList(constraint));
287         return requirementSubstitutionFilterPropertyDataDefinition;
288     }
289 }