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.janusgraph.JanusGraphOperationStatus;
30 import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels;
31 import org.openecomp.sdc.be.dao.neo4j.GraphEdgePropertiesDictionary;
32 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
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> JanusGraphOperationStatus findAllResourceElementsDefinitionRecursively(String resourceId, List<ElementDefinition> elements, NodeElementFetcher<ElementDefinition> singleNodeFetcher) {
53 log.trace("Going to fetch elements under resource {}" , resourceId);
54 JanusGraphOperationStatus
55 resourceAttributesStatus = singleNodeFetcher.findAllNodeElements(resourceId, elements);
57 if (resourceAttributesStatus != JanusGraphOperationStatus.OK) {
58 return resourceAttributesStatus;
61 Either<ImmutablePair<ResourceMetadataData, GraphEdge>, JanusGraphOperationStatus> parentNodes = janusGraphGenericDao
62 .getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Resource), resourceId, GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.Resource, ResourceMetadataData.class);
64 if (parentNodes.isRight()) {
65 JanusGraphOperationStatus parentNodesStatus = parentNodes.right().value();
66 if (parentNodesStatus != JanusGraphOperationStatus.NOT_FOUND) {
67 BeEcompErrorManager.getInstance().logInternalFlowError("findAllResourceElementsDefinitionRecursively", "Failed to find parent elements of resource " + resourceId + ". status is " + parentNodesStatus, ErrorSeverity.ERROR);
68 return parentNodesStatus;
72 if (parentNodes.isLeft()) {
73 ImmutablePair<ResourceMetadataData, GraphEdge> parnetNodePair = parentNodes.left().value();
74 String parentUniqueId = parnetNodePair.getKey().getMetadataDataDefinition().getUniqueId();
75 JanusGraphOperationStatus addParentIntStatus = findAllResourceElementsDefinitionRecursively(parentUniqueId, elements, singleNodeFetcher);
77 if (addParentIntStatus != JanusGraphOperationStatus.OK) {
78 BeEcompErrorManager.getInstance().logInternalFlowError("findAllResourceElementsDefinitionRecursively", "Failed to find all resource elements of resource " + parentUniqueId, ErrorSeverity.ERROR);
80 return addParentIntStatus;
83 return JanusGraphOperationStatus.OK;
88 public ImmutablePair<JanusGraphOperationStatus, String> findInputValue(String resourceInstanceId, String propertyId) {
90 log.debug("Going to check whether the property {} already added to resource instance {}", propertyId, resourceInstanceId);
92 Either<List<ComponentInstanceInput>, JanusGraphOperationStatus> getAllRes = getAllInputsOfResourceInstanceOnlyInputDefId(resourceInstanceId);
93 if (getAllRes.isRight()) {
94 JanusGraphOperationStatus status = getAllRes.right().value();
95 log.trace("After fetching all properties of resource instance {}. Status is {}" ,resourceInstanceId, status);
96 return new ImmutablePair<>(status, null);
99 List<ComponentInstanceInput> list = getAllRes.left().value();
101 for (ComponentInstanceInput instanceProperty : list) {
102 String propertyUniqueId = instanceProperty.getUniqueId();
103 String valueUniqueUid = instanceProperty.getValueUniqueUid();
104 log.trace("Go over property {} under resource instance {}. valueUniqueId = {}" ,propertyUniqueId, resourceInstanceId, valueUniqueUid);
105 if (propertyId.equals(propertyUniqueId) && valueUniqueUid != null) {
106 log.debug("The property {} already created under resource instance {}", propertyId, resourceInstanceId);
107 return new ImmutablePair<>(JanusGraphOperationStatus.ALREADY_EXIST, valueUniqueUid);
112 return new ImmutablePair<>(JanusGraphOperationStatus.NOT_FOUND, null);
116 * return all properties associated to resource instance. The result does contains the property unique id but not its type, default value...
118 * @param resourceInstanceUid
121 public Either<List<ComponentInstanceInput>, JanusGraphOperationStatus> getAllInputsOfResourceInstanceOnlyInputDefId(String resourceInstanceUid) {
123 return getAllInputsOfResourceInstanceOnlyInputDefId(resourceInstanceUid, NodeTypeEnum.ResourceInstance);
127 public Either<List<ComponentInstanceInput>, JanusGraphOperationStatus> getAllInputsOfResourceInstanceOnlyInputDefId(String resourceInstanceUid, NodeTypeEnum instanceNodeType) {
129 Either<ComponentInstanceData, JanusGraphOperationStatus> findResInstanceRes = janusGraphGenericDao
130 .getNode(UniqueIdBuilder.getKeyByNodeType(instanceNodeType), resourceInstanceUid, ComponentInstanceData.class);
132 if (findResInstanceRes.isRight()) {
133 JanusGraphOperationStatus status = findResInstanceRes.right().value();
134 if (status == JanusGraphOperationStatus.NOT_FOUND) {
135 status = JanusGraphOperationStatus.INVALID_ID;
137 return Either.right(status);
140 Either<List<ImmutablePair<InputValueData, GraphEdge>>, JanusGraphOperationStatus> propertyImplNodes = janusGraphGenericDao
141 .getChildrenNodes(UniqueIdBuilder.getKeyByNodeType(instanceNodeType), resourceInstanceUid, GraphEdgeLabels.INPUT_VALUE, NodeTypeEnum.InputValue, InputValueData.class);
143 if (propertyImplNodes.isRight()) {
144 JanusGraphOperationStatus status = propertyImplNodes.right().value();
145 return Either.right(status);
148 List<ImmutablePair<InputValueData, GraphEdge>> list = propertyImplNodes.left().value();
149 if (list == null || list.isEmpty()) {
150 return Either.right(JanusGraphOperationStatus.NOT_FOUND);
153 List<ComponentInstanceInput> result = new ArrayList<>();
156 for (ImmutablePair<InputValueData, GraphEdge> propertyValueDataPair : list) {
158 InputValueData propertyValueData = propertyValueDataPair.getLeft();
159 String propertyValueUid = propertyValueData.getUniqueId();
160 String value = propertyValueData.getValue();
162 Either<ImmutablePair<InputsData, GraphEdge>, JanusGraphOperationStatus> inputNodes = janusGraphGenericDao
163 .getParentNode(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), propertyValueData.getUniqueId(), GraphEdgeLabels.GET_INPUT, NodeTypeEnum.Input, InputsData.class);
165 if (inputNodes.isRight()) {
167 return Either.right(inputNodes.right().value());
170 InputsData input = inputNodes.left().value().left;
171 String inputId = input.getPropertyDataDefinition().getUniqueId();
173 Either<ImmutablePair<PropertyData, GraphEdge>, JanusGraphOperationStatus> propertyDefRes = janusGraphGenericDao
174 .getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.InputValue), propertyValueUid, GraphEdgeLabels.INPUT_IMPL, NodeTypeEnum.Property, PropertyData.class);
175 if (propertyDefRes.isRight()) {
176 JanusGraphOperationStatus status = propertyDefRes.right().value();
177 if (status == JanusGraphOperationStatus.NOT_FOUND) {
178 status = JanusGraphOperationStatus.INVALID_ID;
180 return Either.right(status);
183 ImmutablePair<PropertyData, GraphEdge> propertyDefPair = propertyDefRes.left().value();
184 PropertyData propertyData = propertyDefPair.left;
185 Either<Edge, JanusGraphOperationStatus> inputsEges = janusGraphGenericDao
186 .getIncomingEdgeByCriteria(propertyData, GraphEdgeLabels.INPUT, null);
187 if (inputsEges.isRight()) {
188 JanusGraphOperationStatus status = inputsEges.right().value();
190 return Either.right(status);
192 Edge edge = inputsEges.left().value();
193 String inputName = (String) janusGraphGenericDao
194 .getProperty(edge, GraphEdgePropertiesDictionary.NAME.getProperty());
196 ComponentInstanceInput resourceInstanceProperty = new ComponentInstanceInput(propertyData.getPropertyDataDefinition(), inputId, value, propertyValueUid);
198 resourceInstanceProperty.setName(inputName);
199 resourceInstanceProperty.setParentUniqueId(inputId);
200 resourceInstanceProperty.setValue(value);
201 resourceInstanceProperty.setValueUniqueUid(propertyValueData.getUniqueId());
202 resourceInstanceProperty.setType(propertyData.getPropertyDataDefinition().getType());
203 resourceInstanceProperty.setSchema(propertyData.getPropertyDataDefinition().getSchema());
204 resourceInstanceProperty.setComponentInstanceId(resourceInstanceUid);
206 result.add(resourceInstanceProperty);
210 return Either.left(result);
214 public ComponentInstanceInput buildResourceInstanceInput(InputValueData propertyValueData, ComponentInstanceInput resourceInstanceInput) {
216 String value = propertyValueData.getValue();
217 String uid = propertyValueData.getUniqueId();
218 ComponentInstanceInput instanceProperty = new ComponentInstanceInput(resourceInstanceInput, value, uid);
219 instanceProperty.setPath(resourceInstanceInput.getPath());
221 return instanceProperty;