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=========================================================
20 package org.openecomp.sdc.be.model.operations.impl;
22 import fj.data.Either;
23 import java.util.ArrayList;
24 import java.util.List;
25 import org.apache.commons.lang3.tuple.ImmutablePair;
26 import org.apache.tinkerpop.gremlin.structure.Edge;
27 import org.openecomp.sdc.be.config.BeEcompErrorManager;
28 import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
29 import org.openecomp.sdc.be.dao.graph.datatype.GraphEdge;
30 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
31 import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels;
32 import org.openecomp.sdc.be.dao.neo4j.GraphEdgePropertiesDictionary;
33 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
34 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
35 import org.openecomp.sdc.be.model.ComponentInstanceInput;
36 import org.openecomp.sdc.be.model.operations.api.IInputsOperation;
37 import org.openecomp.sdc.be.resources.data.ComponentInstanceData;
38 import org.openecomp.sdc.be.resources.data.InputValueData;
39 import org.openecomp.sdc.be.resources.data.InputsData;
40 import org.openecomp.sdc.be.resources.data.PropertyData;
41 import org.openecomp.sdc.be.resources.data.ResourceMetadataData;
42 import org.openecomp.sdc.common.log.wrappers.Logger;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.stereotype.Component;
46 @Component("input-operation")
47 public class InputsOperation extends AbstractOperation implements IInputsOperation {
49 private static final Logger log = Logger.getLogger(InputsOperation.class.getName());
51 PropertyOperation propertyOperation;
53 public <ElementDefinition> JanusGraphOperationStatus findAllResourceElementsDefinitionRecursively(String resourceId,
54 List<ElementDefinition> elements,
55 NodeElementFetcher<ElementDefinition> singleNodeFetcher) {
56 log.trace("Going to fetch elements under resource {}", resourceId);
57 JanusGraphOperationStatus resourceAttributesStatus = singleNodeFetcher.findAllNodeElements(resourceId, elements);
58 if (resourceAttributesStatus != JanusGraphOperationStatus.OK) {
59 return resourceAttributesStatus;
61 Either<ImmutablePair<ResourceMetadataData, GraphEdge>, JanusGraphOperationStatus> parentNodes = janusGraphGenericDao
62 .getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Resource), resourceId, GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.Resource,
63 ResourceMetadataData.class);
64 if (parentNodes.isRight()) {
65 JanusGraphOperationStatus parentNodesStatus = parentNodes.right().value();
66 if (parentNodesStatus != JanusGraphOperationStatus.NOT_FOUND) {
67 BeEcompErrorManager.getInstance().logInternalFlowError("findAllResourceElementsDefinitionRecursively",
68 "Failed to find parent elements of resource " + resourceId + ". status is " + parentNodesStatus, ErrorSeverity.ERROR);
69 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);
76 if (addParentIntStatus != JanusGraphOperationStatus.OK) {
77 BeEcompErrorManager.getInstance().logInternalFlowError("findAllResourceElementsDefinitionRecursively",
78 "Failed to find all resource elements of resource " + parentUniqueId, ErrorSeverity.ERROR);
79 return addParentIntStatus;
82 return JanusGraphOperationStatus.OK;
86 public ImmutablePair<JanusGraphOperationStatus, String> findInputValue(String resourceInstanceId, String propertyId) {
87 log.debug("Going to check whether the property {} already added to resource instance {}", propertyId, resourceInstanceId);
88 Either<List<ComponentInstanceInput>, JanusGraphOperationStatus> getAllRes = getAllInputsOfResourceInstanceOnlyInputDefId(resourceInstanceId);
89 if (getAllRes.isRight()) {
90 JanusGraphOperationStatus status = getAllRes.right().value();
91 log.trace("After fetching all properties of resource instance {}. Status is {}", resourceInstanceId, status);
92 return new ImmutablePair<>(status, null);
94 List<ComponentInstanceInput> list = getAllRes.left().value();
96 for (ComponentInstanceInput instanceProperty : list) {
97 String propertyUniqueId = instanceProperty.getUniqueId();
98 String valueUniqueUid = instanceProperty.getValueUniqueUid();
99 log.trace("Go over property {} under resource instance {}. valueUniqueId = {}", propertyUniqueId, resourceInstanceId, valueUniqueUid);
100 if (propertyId.equals(propertyUniqueId) && valueUniqueUid != null) {
101 log.debug("The property {} already created under resource instance {}", propertyId, resourceInstanceId);
102 return new ImmutablePair<>(JanusGraphOperationStatus.ALREADY_EXIST, valueUniqueUid);
106 return new ImmutablePair<>(JanusGraphOperationStatus.NOT_FOUND, null);
110 * return all properties associated to resource instance. The result does contains the property unique id but not its type, default value...
112 * @param resourceInstanceUid
115 public Either<List<ComponentInstanceInput>, JanusGraphOperationStatus> getAllInputsOfResourceInstanceOnlyInputDefId(String resourceInstanceUid) {
116 return getAllInputsOfResourceInstanceOnlyInputDefId(resourceInstanceUid, NodeTypeEnum.ResourceInstance);
119 public Either<List<ComponentInstanceInput>, JanusGraphOperationStatus> getAllInputsOfResourceInstanceOnlyInputDefId(String resourceInstanceUid,
120 NodeTypeEnum instanceNodeType) {
121 Either<ComponentInstanceData, JanusGraphOperationStatus> findResInstanceRes = janusGraphGenericDao
122 .getNode(UniqueIdBuilder.getKeyByNodeType(instanceNodeType), resourceInstanceUid, ComponentInstanceData.class);
123 if (findResInstanceRes.isRight()) {
124 JanusGraphOperationStatus status = findResInstanceRes.right().value();
125 if (status == JanusGraphOperationStatus.NOT_FOUND) {
126 status = JanusGraphOperationStatus.INVALID_ID;
128 return Either.right(status);
130 Either<List<ImmutablePair<InputValueData, GraphEdge>>, JanusGraphOperationStatus> propertyImplNodes = janusGraphGenericDao
131 .getChildrenNodes(UniqueIdBuilder.getKeyByNodeType(instanceNodeType), resourceInstanceUid, GraphEdgeLabels.INPUT_VALUE,
132 NodeTypeEnum.InputValue, InputValueData.class);
133 if (propertyImplNodes.isRight()) {
134 JanusGraphOperationStatus status = propertyImplNodes.right().value();
135 return Either.right(status);
137 List<ImmutablePair<InputValueData, GraphEdge>> list = propertyImplNodes.left().value();
138 if (list == null || list.isEmpty()) {
139 return Either.right(JanusGraphOperationStatus.NOT_FOUND);
141 List<ComponentInstanceInput> result = new ArrayList<>();
142 for (ImmutablePair<InputValueData, GraphEdge> propertyValueDataPair : list) {
143 InputValueData propertyValueData = propertyValueDataPair.getLeft();
144 String propertyValueUid = propertyValueData.getUniqueId();
145 String value = propertyValueData.getValue();
146 Either<ImmutablePair<InputsData, GraphEdge>, JanusGraphOperationStatus> inputNodes = janusGraphGenericDao
147 .getParentNode(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), propertyValueData.getUniqueId(), GraphEdgeLabels.GET_INPUT,
148 NodeTypeEnum.Input, InputsData.class);
149 if (inputNodes.isRight()) {
150 return Either.right(inputNodes.right().value());
152 InputsData input = inputNodes.left().value().left;
153 String inputId = input.getPropertyDataDefinition().getUniqueId();
154 Either<ImmutablePair<PropertyData, GraphEdge>, JanusGraphOperationStatus> propertyDefRes = janusGraphGenericDao
155 .getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.InputValue), propertyValueUid, GraphEdgeLabels.INPUT_IMPL,
156 NodeTypeEnum.Property, PropertyData.class);
157 if (propertyDefRes.isRight()) {
158 JanusGraphOperationStatus status = propertyDefRes.right().value();
159 if (status == JanusGraphOperationStatus.NOT_FOUND) {
160 status = JanusGraphOperationStatus.INVALID_ID;
162 return Either.right(status);
164 ImmutablePair<PropertyData, GraphEdge> propertyDefPair = propertyDefRes.left().value();
165 PropertyData propertyData = propertyDefPair.left;
166 Either<Edge, JanusGraphOperationStatus> inputsEges = janusGraphGenericDao
167 .getIncomingEdgeByCriteria(propertyData, GraphEdgeLabels.INPUT, null);
168 if (inputsEges.isRight()) {
169 JanusGraphOperationStatus status = inputsEges.right().value();
170 return Either.right(status);
172 Edge edge = inputsEges.left().value();
173 String inputName = (String) janusGraphGenericDao.getProperty(edge, GraphEdgePropertiesDictionary.NAME.getProperty());
174 ComponentInstanceInput resourceInstanceProperty = new ComponentInstanceInput(propertyData.getPropertyDataDefinition(), inputId, value,
176 resourceInstanceProperty.setName(inputName);
177 resourceInstanceProperty.setParentUniqueId(inputId);
178 resourceInstanceProperty.setValue(value);
179 resourceInstanceProperty.setValueUniqueUid(propertyValueData.getUniqueId());
180 resourceInstanceProperty.setType(propertyData.getPropertyDataDefinition().getType());
181 resourceInstanceProperty.setSchema(propertyData.getPropertyDataDefinition().getSchema());
182 resourceInstanceProperty.setComponentInstanceId(resourceInstanceUid);
183 result.add(resourceInstanceProperty);
185 return Either.left(result);
189 public ComponentInstanceInput buildResourceInstanceInput(InputValueData propertyValueData, ComponentInstanceInput resourceInstanceInput) {
190 String value = propertyValueData.getValue();
191 String uid = propertyValueData.getUniqueId();
192 ComponentInstanceInput instanceProperty = new ComponentInstanceInput(resourceInstanceInput, value, uid);
193 instanceProperty.setPath(resourceInstanceInput.getPath());
194 return instanceProperty;