Implement Attributes/Outputs BE (part 1)
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / OutputsBusinessLogic.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2021, Nordix Foundation. 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 fj.data.Either;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Optional;
28 import org.openecomp.sdc.be.components.validation.ComponentValidations;
29 import org.openecomp.sdc.be.dao.api.ActionStatus;
30 import org.openecomp.sdc.be.model.Component;
31 import org.openecomp.sdc.be.model.ComponentInstanceOutput;
32 import org.openecomp.sdc.be.model.ComponentParametersView;
33 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations;
34 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
35 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
36 import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
37 import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
38 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
39 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
40 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
41 import org.openecomp.sdc.common.log.elements.LoggerSupportability;
42 import org.openecomp.sdc.common.log.enums.LoggerSupportabilityActions;
43 import org.openecomp.sdc.common.log.enums.StatusCode;
44 import org.openecomp.sdc.common.log.wrappers.Logger;
45 import org.openecomp.sdc.exception.ResponseFormat;
46 import org.springframework.beans.factory.annotation.Autowired;
47
48 @org.springframework.stereotype.Component("outputsBusinessLogic")
49 public class OutputsBusinessLogic extends BaseBusinessLogic {
50
51     private static final Logger log = Logger.getLogger(OutputsBusinessLogic.class);
52     private static final String FAILED_TO_FOUND_COMPONENT_ERROR = "Failed to found component {}, error: {}";
53     private static final LoggerSupportability loggerSupportability = LoggerSupportability.getLogger(OutputsBusinessLogic.class);
54     private static final String FAILED_TO_FOUND_COMPONENT_INSTANCE_OUTPUTS_COMPONENT_INSTANCE_ID = "Failed to found component instance outputs componentInstanceId: {}";
55     private static final String FAILED_TO_FOUND_COMPONENT_INSTANCE_OUTPUTS_ERROR = "Failed to found component instance outputs {}, error: {}";
56
57     @Autowired
58     public OutputsBusinessLogic(final IElementOperation elementDao,
59                                 final IGroupOperation groupOperation,
60                                 final IGroupInstanceOperation groupInstanceOperation,
61                                 final IGroupTypeOperation groupTypeOperation,
62                                 final InterfaceOperation interfaceOperation,
63                                 final InterfaceLifecycleOperation interfaceLifecycleTypeOperation,
64                                 final ArtifactsOperations artifactToscaOperation) {
65         super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation,
66             interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation);
67     }
68
69     public Either<List<ComponentInstanceOutput>, ResponseFormat> getComponentInstanceOutputs(final String userId,
70                                                                                              final String componentId,
71                                                                                              final String componentInstanceId) {
72
73         validateUserExists(userId);
74         final ComponentParametersView filters = new ComponentParametersView();
75         filters.disableAll();
76         filters.setIgnoreOutputs(false);
77         filters.setIgnoreComponentInstances(false);
78         filters.setIgnoreComponentInstancesOutputs(false);
79
80         final Either<Component, StorageOperationStatus> getComponentEither = toscaOperationFacade.getToscaElement(componentId, filters);
81         if (getComponentEither.isRight()) {
82             ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(getComponentEither.right().value());
83             log.debug(FAILED_TO_FOUND_COMPONENT_ERROR, componentId, actionStatus);
84             return Either.right(componentsUtils.getResponseFormat(actionStatus));
85
86         }
87         final Component component = getComponentEither.left().value();
88
89         if (!ComponentValidations.validateComponentInstanceExist(component, componentInstanceId)) {
90             final ActionStatus actionStatus = ActionStatus.COMPONENT_INSTANCE_NOT_FOUND;
91             log.debug(FAILED_TO_FOUND_COMPONENT_INSTANCE_OUTPUTS_ERROR, componentInstanceId, actionStatus);
92             loggerSupportability.log(LoggerSupportabilityActions.CREATE_INPUTS, component.getComponentMetadataForSupportLog(),
93                 StatusCode.ERROR, FAILED_TO_FOUND_COMPONENT_INSTANCE_OUTPUTS_COMPONENT_INSTANCE_ID, componentInstanceId);
94             return Either.right(componentsUtils.getResponseFormat(actionStatus));
95         }
96         final Map<String, List<ComponentInstanceOutput>> ciOutputs = Optional.ofNullable(component.getComponentInstancesOutputs())
97             .orElse(Collections.emptyMap());
98
99         return Either.left(ciOutputs.getOrDefault(componentInstanceId, Collections.emptyList()));
100     }
101
102 }