c88746347eddba42651f926c00c9cc3a13009307
[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 package org.openecomp.sdc.be.components.impl;
20
21 import static org.openecomp.sdc.be.dao.api.ActionStatus.SUBSTITUTION_FILTER_NOT_FOUND;
22 import static org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR;
23
24 import fj.data.Either;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Optional;
29 import java.util.stream.Collectors;
30 import org.openecomp.sdc.be.components.impl.exceptions.BusinessLogicException;
31 import org.openecomp.sdc.be.components.impl.utils.NodeFilterConstraintAction;
32 import org.openecomp.sdc.be.components.validation.NodeFilterValidator;
33 import org.openecomp.sdc.be.dao.api.ActionStatus;
34 import org.openecomp.sdc.be.datatypes.elements.RequirementSubstitutionFilterPropertyDataDefinition;
35 import org.openecomp.sdc.be.datatypes.elements.SubstitutionFilterDataDefinition;
36 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
37 import org.openecomp.sdc.be.model.Component;
38 import org.openecomp.sdc.be.model.User;
39 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations;
40 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
41 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.SubstitutionFilterOperation;
42 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
43 import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
44 import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
45 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
46 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
47 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
48 import org.openecomp.sdc.be.user.Role;
49 import org.openecomp.sdc.common.log.wrappers.Logger;
50 import org.openecomp.sdc.exception.ResponseFormat;
51 import org.springframework.beans.factory.annotation.Autowired;
52
53 @org.springframework.stereotype.Component("componentSubstitutionFilterBusinessLogic")
54 public class ComponentSubstitutionFilterBusinessLogic extends BaseBusinessLogic {
55
56     private static final Logger LOGGER = Logger.getLogger(ComponentSubstitutionFilterBusinessLogic.class);
57     private final SubstitutionFilterOperation substitutionFilterOperation;
58     private final NodeFilterValidator nodeFilterValidator;
59
60     @Autowired
61     public ComponentSubstitutionFilterBusinessLogic(final IElementOperation elementDao, final IGroupOperation groupOperation,
62                                                     final IGroupInstanceOperation groupInstanceOperation,
63                                                     final IGroupTypeOperation groupTypeOperation, final InterfaceOperation interfaceOperation,
64                                                     final InterfaceLifecycleOperation interfaceLifecycleTypeOperation,
65                                                     final ArtifactsOperations artifactToscaOperation,
66                                                     final SubstitutionFilterOperation substitutionFilterOperation,
67                                                     final NodeFilterValidator nodeFilterValidator) {
68         super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation, interfaceOperation, interfaceLifecycleTypeOperation,
69             artifactToscaOperation);
70         this.substitutionFilterOperation = substitutionFilterOperation;
71         this.nodeFilterValidator = nodeFilterValidator;
72     }
73
74     public Optional<SubstitutionFilterDataDefinition> createSubstitutionFilterIfNotExist(final String componentId, final boolean shouldLock,
75                                                                                          final ComponentTypeEnum componentTypeEnum)
76         throws BusinessLogicException {
77         final Component component = getComponent(componentId);
78         Optional<SubstitutionFilterDataDefinition> substitutionFilterDataDefinition = Optional.ofNullable(component.getSubstitutionFilter());
79         if (substitutionFilterDataDefinition.isPresent()) {
80             return substitutionFilterDataDefinition;
81         }
82         boolean wasLocked = false;
83         try {
84             if (shouldLock) {
85                 lockComponent(component.getUniqueId(), component, "Create Substitution Filter on component");
86                 wasLocked = true;
87             }
88             final Either<SubstitutionFilterDataDefinition, StorageOperationStatus> result = substitutionFilterOperation
89                 .createSubstitutionFilter(componentId);
90             if (result.isRight()) {
91                 janusGraphDao.rollback();
92                 LOGGER.error(BUSINESS_PROCESS_ERROR, "Failed to Create Substitution filter on component with id {}", componentId);
93                 throw new BusinessLogicException(componentsUtils
94                     .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(result.right().value()), component.getSystemName()));
95             }
96             substitutionFilterDataDefinition = Optional.ofNullable(result.left().value());
97             component.setSubstitutionFilter(substitutionFilterDataDefinition.get());
98             janusGraphDao.commit();
99             LOGGER.debug("Substitution filter successfully created in component {} . ", component.getSystemName());
100         } catch (final Exception e) {
101             janusGraphDao.rollback();
102             LOGGER
103                 .error(BUSINESS_PROCESS_ERROR, "Exception occurred during add Component Substitution filter property values: {}", e.getMessage(), e);
104             throw new BusinessLogicException(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
105         } finally {
106             if (wasLocked) {
107                 unlockComponent(component.getUniqueId(), componentTypeEnum);
108             }
109         }
110         return substitutionFilterDataDefinition;
111     }
112
113     public Optional<SubstitutionFilterDataDefinition> addSubstitutionFilter(final String componentId, final String propertyName,
114                                                                             final String constraint, final boolean shouldLock,
115                                                                             final ComponentTypeEnum componentTypeEnum) throws BusinessLogicException {
116         final Component component = getComponent(componentId);
117         final Either<Boolean, ResponseFormat> response = nodeFilterValidator
118             .validateComponentFilter(component, Collections.singletonList(constraint), NodeFilterConstraintAction.ADD);
119         if (response.isRight()) {
120             throw new BusinessLogicException(
121                 componentsUtils.getResponseFormat(ActionStatus.SUBSTITUTION_FILTER_NOT_FOUND, response.right().value().getFormattedMessage()));
122         }
123         boolean wasLocked = false;
124         try {
125             if (shouldLock) {
126                 lockComponent(component.getUniqueId(), component, "Add Substitution Filter on Component");
127                 wasLocked = true;
128             }
129             final RequirementSubstitutionFilterPropertyDataDefinition newProperty = new RequirementSubstitutionFilterPropertyDataDefinition();
130             newProperty.setName(propertyName);
131             newProperty.setConstraints(Collections.singletonList(constraint));
132             final Either<SubstitutionFilterDataDefinition, StorageOperationStatus> resultEither = substitutionFilterOperation
133                 .addPropertyFilter(componentId, component.getSubstitutionFilter(), newProperty);
134             if (resultEither.isRight()) {
135                 janusGraphDao.rollback();
136                 throw new BusinessLogicException(componentsUtils
137                     .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(resultEither.right().value()),
138                         component.getSystemName()));
139             }
140             janusGraphDao.commit();
141             LOGGER.debug("Substitution filter successfully created in component {} . ", component.getSystemName());
142             return Optional.ofNullable(resultEither.left().value());
143         } catch (final Exception e) {
144             janusGraphDao.rollback();
145             LOGGER
146                 .error(BUSINESS_PROCESS_ERROR, "Exception occurred during add component substitution filter property values: {}", e.getMessage(), e);
147             throw new BusinessLogicException(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
148         } finally {
149             if (wasLocked) {
150                 unlockComponent(component.getUniqueId(), componentTypeEnum);
151             }
152         }
153     }
154
155     public Optional<SubstitutionFilterDataDefinition> updateSubstitutionFilter(final String componentId, final List<String> constraints,
156                                                                                final boolean shouldLock, final ComponentTypeEnum componentTypeEnum)
157         throws BusinessLogicException {
158         final Component component = getComponent(componentId);
159         final Either<Boolean, ResponseFormat> response = nodeFilterValidator
160             .validateComponentFilter(component, constraints, NodeFilterConstraintAction.UPDATE);
161         if (response.isRight()) {
162             throw new BusinessLogicException(
163                 componentsUtils.getResponseFormat(ActionStatus.SUBSTITUTION_FILTER_NOT_FOUND, response.right().value().getFormattedMessage()));
164         }
165         SubstitutionFilterDataDefinition substitutionFilterDataDefinition = component.getSubstitutionFilter();
166         if (substitutionFilterDataDefinition == null) {
167             throw new BusinessLogicException(componentsUtils.getResponseFormat(SUBSTITUTION_FILTER_NOT_FOUND));
168         }
169         boolean wasLocked = false;
170         try {
171             if (shouldLock) {
172                 lockComponent(component.getUniqueId(), component, "Update Substitution Filter on Component");
173                 wasLocked = true;
174             }
175             final List<RequirementSubstitutionFilterPropertyDataDefinition> properties = constraints.stream()
176                 .map(this::getRequirementSubstitutionFilterPropertyDataDefinition).collect(Collectors.toList());
177             final Either<SubstitutionFilterDataDefinition, StorageOperationStatus> result = substitutionFilterOperation
178                 .updateProperties(componentId, substitutionFilterDataDefinition, properties);
179             if (result.isRight()) {
180                 janusGraphDao.rollback();
181                 throw new BusinessLogicException(componentsUtils
182                     .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(result.right().value()), component.getSystemName()));
183             } else {
184                 substitutionFilterDataDefinition = result.left().value();
185             }
186             janusGraphDao.commit();
187             LOGGER.debug("Substitution filter successfully updated in component {} . ", component.getSystemName());
188         } catch (final Exception e) {
189             janusGraphDao.rollback();
190             LOGGER.error(BUSINESS_PROCESS_ERROR, this.getClass().getName(),
191                 "Exception occurred during update component substitution filter property values: {}", e);
192             throw new BusinessLogicException(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
193         } finally {
194             if (wasLocked) {
195                 unlockComponent(component.getUniqueId(), componentTypeEnum);
196             }
197         }
198         return Optional.ofNullable(substitutionFilterDataDefinition);
199     }
200
201     public Optional<SubstitutionFilterDataDefinition> deleteSubstitutionFilter(final String componentId, final int position, final boolean shouldLock,
202                                                                                final ComponentTypeEnum componentTypeEnum)
203         throws BusinessLogicException {
204         final Component component = getComponent(componentId);
205         SubstitutionFilterDataDefinition substitutionFilterDataDefinition = component.getSubstitutionFilter();
206         boolean wasLocked = false;
207         try {
208             if (shouldLock) {
209                 lockComponent(component.getUniqueId(), component, "Delete substitution Filter on Component");
210                 wasLocked = true;
211             }
212             final Either<SubstitutionFilterDataDefinition, StorageOperationStatus> result = substitutionFilterOperation
213                 .deleteConstraint(componentId, substitutionFilterDataDefinition, position);
214             if (result.isRight()) {
215                 janusGraphDao.rollback();
216                 throw new BusinessLogicException(componentsUtils
217                     .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(result.right().value()), component.getSystemName()));
218             } else {
219                 substitutionFilterDataDefinition = result.left().value();
220             }
221             janusGraphDao.commit();
222             LOGGER.debug("Substitution filter successfully deleted in component {} . ", component.getSystemName());
223         } catch (final Exception e) {
224             janusGraphDao.rollback();
225             LOGGER.error(BUSINESS_PROCESS_ERROR, "Exception occurred during delete component substitution filter property values: {}", e.getMessage(),
226                 e);
227             throw new BusinessLogicException(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
228         } finally {
229             if (wasLocked) {
230                 unlockComponent(component.getUniqueId(), componentTypeEnum);
231             }
232         }
233         return Optional.ofNullable(substitutionFilterDataDefinition);
234     }
235
236     private void unlockComponent(final String componentUniqueId, final ComponentTypeEnum componentType) {
237         graphLockOperation.unlockComponent(componentUniqueId, componentType.getNodeType());
238     }
239
240     public User validateUser(final String userId) {
241         final User user = userValidations.validateUserExists(userId);
242         userValidations.validateUserRole(user, Arrays.asList(Role.DESIGNER, Role.ADMIN));
243         return user;
244     }
245
246     private RequirementSubstitutionFilterPropertyDataDefinition getRequirementSubstitutionFilterPropertyDataDefinition(final String constraint) {
247         final RequirementSubstitutionFilterPropertyDataDefinition requirementSubstitutionFilterPropertyDataDefinition = new RequirementSubstitutionFilterPropertyDataDefinition();
248         requirementSubstitutionFilterPropertyDataDefinition.setConstraints(Arrays.asList(constraint));
249         return requirementSubstitutionFilterPropertyDataDefinition;
250     }
251 }