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=========================================================
21 package org.openecomp.sdc.be.model.operations.impl;
23 import fj.data.Either;
24 import org.apache.commons.lang3.tuple.ImmutablePair;
25 import org.apache.tinkerpop.gremlin.structure.Edge;
26 import org.openecomp.sdc.be.config.BeEcompErrorManager;
27 import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
28 import org.openecomp.sdc.be.dao.graph.datatype.GraphEdge;
29 import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels;
30 import org.openecomp.sdc.be.dao.neo4j.GraphEdgePropertiesDictionary;
31 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
32 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
33 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
34 import org.openecomp.sdc.be.model.ComponentInstanceInput;
35 import org.openecomp.sdc.be.model.operations.api.IInputsOperation;
36 import org.openecomp.sdc.be.resources.data.*;
37 import org.openecomp.sdc.common.log.wrappers.Logger;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.stereotype.Component;
41 import java.util.ArrayList;
42 import java.util.List;
44 @Component("input-operation")
45 public class InputsOperation extends AbstractOperation implements IInputsOperation {
47 private static final Logger log = Logger.getLogger(InputsOperation.class.getName());
49 PropertyOperation propertyOperation;
51 public <ElementDefinition> TitanOperationStatus findAllResourceElementsDefinitionRecursively(String resourceId, List<ElementDefinition> elements, NodeElementFetcher<ElementDefinition> singleNodeFetcher) {
53 log.trace("Going to fetch elements under resource {}" , resourceId);
54 TitanOperationStatus resourceAttributesStatus = singleNodeFetcher.findAllNodeElements(resourceId, elements);
56 if (resourceAttributesStatus != TitanOperationStatus.OK) {
57 return resourceAttributesStatus;
60 Either<ImmutablePair<ResourceMetadataData, GraphEdge>, TitanOperationStatus> parentNodes = titanGenericDao.getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Resource), resourceId, GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.Resource, ResourceMetadataData.class);
62 if (parentNodes.isRight()) {
63 TitanOperationStatus parentNodesStatus = parentNodes.right().value();
64 if (parentNodesStatus != TitanOperationStatus.NOT_FOUND) {
65 BeEcompErrorManager.getInstance().logInternalFlowError("findAllResourceElementsDefinitionRecursively", "Failed to find parent elements of resource " + resourceId + ". status is " + parentNodesStatus, ErrorSeverity.ERROR);
66 return parentNodesStatus;
70 if (parentNodes.isLeft()) {
71 ImmutablePair<ResourceMetadataData, GraphEdge> parnetNodePair = parentNodes.left().value();
72 String parentUniqueId = parnetNodePair.getKey().getMetadataDataDefinition().getUniqueId();
73 TitanOperationStatus addParentIntStatus = findAllResourceElementsDefinitionRecursively(parentUniqueId, elements, singleNodeFetcher);
75 if (addParentIntStatus != TitanOperationStatus.OK) {
76 BeEcompErrorManager.getInstance().logInternalFlowError("findAllResourceElementsDefinitionRecursively", "Failed to find all resource elements of resource " + parentUniqueId, ErrorSeverity.ERROR);
78 return addParentIntStatus;
81 return TitanOperationStatus.OK;
86 public ImmutablePair<TitanOperationStatus, String> findInputValue(String resourceInstanceId, String propertyId) {
88 log.debug("Going to check whether the property {} already added to resource instance {}", propertyId, resourceInstanceId);
90 Either<List<ComponentInstanceInput>, TitanOperationStatus> getAllRes = getAllInputsOfResourceInstanceOnlyInputDefId(resourceInstanceId);
91 if (getAllRes.isRight()) {
92 TitanOperationStatus status = getAllRes.right().value();
93 log.trace("After fetching all properties of resource instance {}. Status is {}" ,resourceInstanceId, status);
94 return new ImmutablePair<>(status, null);
97 List<ComponentInstanceInput> list = getAllRes.left().value();
99 for (ComponentInstanceInput instanceProperty : list) {
100 String propertyUniqueId = instanceProperty.getUniqueId();
101 String valueUniqueUid = instanceProperty.getValueUniqueUid();
102 log.trace("Go over property {} under resource instance {}. valueUniqueId = {}" ,propertyUniqueId, resourceInstanceId, valueUniqueUid);
103 if (propertyId.equals(propertyUniqueId) && valueUniqueUid != null) {
104 log.debug("The property {} already created under resource instance {}", propertyId, resourceInstanceId);
105 return new ImmutablePair<>(TitanOperationStatus.ALREADY_EXIST, valueUniqueUid);
110 return new ImmutablePair<>(TitanOperationStatus.NOT_FOUND, null);
114 * return all properties associated to resource instance. The result does contains the property unique id but not its type, default value...
116 * @param resourceInstanceUid
119 public Either<List<ComponentInstanceInput>, TitanOperationStatus> getAllInputsOfResourceInstanceOnlyInputDefId(String resourceInstanceUid) {
121 return getAllInputsOfResourceInstanceOnlyInputDefId(resourceInstanceUid, NodeTypeEnum.ResourceInstance);
125 public Either<List<ComponentInstanceInput>, TitanOperationStatus> getAllInputsOfResourceInstanceOnlyInputDefId(String resourceInstanceUid, NodeTypeEnum instanceNodeType) {
127 Either<ComponentInstanceData, TitanOperationStatus> findResInstanceRes = titanGenericDao.getNode(UniqueIdBuilder.getKeyByNodeType(instanceNodeType), resourceInstanceUid, ComponentInstanceData.class);
129 if (findResInstanceRes.isRight()) {
130 TitanOperationStatus status = findResInstanceRes.right().value();
131 if (status == TitanOperationStatus.NOT_FOUND) {
132 status = TitanOperationStatus.INVALID_ID;
134 return Either.right(status);
137 Either<List<ImmutablePair<InputValueData, GraphEdge>>, TitanOperationStatus> propertyImplNodes = titanGenericDao.getChildrenNodes(UniqueIdBuilder.getKeyByNodeType(instanceNodeType), resourceInstanceUid, GraphEdgeLabels.INPUT_VALUE, NodeTypeEnum.InputValue, InputValueData.class);
139 if (propertyImplNodes.isRight()) {
140 TitanOperationStatus status = propertyImplNodes.right().value();
141 return Either.right(status);
144 List<ImmutablePair<InputValueData, GraphEdge>> list = propertyImplNodes.left().value();
145 if (list == null || list.isEmpty()) {
146 return Either.right(TitanOperationStatus.NOT_FOUND);
149 List<ComponentInstanceInput> result = new ArrayList<>();
152 for (ImmutablePair<InputValueData, GraphEdge> propertyValueDataPair : list) {
154 InputValueData propertyValueData = propertyValueDataPair.getLeft();
155 String propertyValueUid = propertyValueData.getUniqueId();
156 String value = propertyValueData.getValue();
158 Either<ImmutablePair<InputsData, GraphEdge>, TitanOperationStatus> inputNodes = titanGenericDao.getParentNode(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), propertyValueData.getUniqueId(), GraphEdgeLabels.GET_INPUT, NodeTypeEnum.Input, InputsData.class);
160 if (inputNodes.isRight()) {
162 return Either.right(inputNodes.right().value());
165 InputsData input = inputNodes.left().value().left;
166 String inputId = input.getPropertyDataDefinition().getUniqueId();
168 Either<ImmutablePair<PropertyData, GraphEdge>, TitanOperationStatus> propertyDefRes = titanGenericDao.getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.InputValue), propertyValueUid, GraphEdgeLabels.INPUT_IMPL, NodeTypeEnum.Property, PropertyData.class);
169 if (propertyDefRes.isRight()) {
170 TitanOperationStatus status = propertyDefRes.right().value();
171 if (status == TitanOperationStatus.NOT_FOUND) {
172 status = TitanOperationStatus.INVALID_ID;
174 return Either.right(status);
177 ImmutablePair<PropertyData, GraphEdge> propertyDefPair = propertyDefRes.left().value();
178 PropertyData propertyData = propertyDefPair.left;
179 Either<Edge, TitanOperationStatus> inputsEges = titanGenericDao.getIncomingEdgeByCriteria(propertyData, GraphEdgeLabels.INPUT, null);
180 if (inputsEges.isRight()) {
181 TitanOperationStatus status = inputsEges.right().value();
183 return Either.right(status);
185 Edge edge = inputsEges.left().value();
186 String inputName = (String) titanGenericDao.getProperty(edge, GraphEdgePropertiesDictionary.NAME.getProperty());
188 ComponentInstanceInput resourceInstanceProperty = new ComponentInstanceInput(propertyData.getPropertyDataDefinition(), inputId, value, propertyValueUid);
190 resourceInstanceProperty.setName(inputName);
191 resourceInstanceProperty.setParentUniqueId(inputId);
192 resourceInstanceProperty.setValue(value);
193 resourceInstanceProperty.setValueUniqueUid(propertyValueData.getUniqueId());
194 resourceInstanceProperty.setType(propertyData.getPropertyDataDefinition().getType());
195 resourceInstanceProperty.setSchema(propertyData.getPropertyDataDefinition().getSchema());
196 resourceInstanceProperty.setComponentInstanceId(resourceInstanceUid);
198 result.add(resourceInstanceProperty);
202 return Either.left(result);
206 public ComponentInstanceInput buildResourceInstanceInput(InputValueData propertyValueData, ComponentInstanceInput resourceInstanceInput) {
208 String value = propertyValueData.getValue();
209 String uid = propertyValueData.getUniqueId();
210 ComponentInstanceInput instanceProperty = new ComponentInstanceInput(resourceInstanceInput, value, uid);
211 instanceProperty.setPath(resourceInstanceInput.getPath());
213 return instanceProperty;