Upgrade SDC from Titan to Janus Graph
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / operations / impl / InputsOperation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
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.model.operations.impl;
22
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;
40
41 import java.util.ArrayList;
42 import java.util.List;
43
44 @Component("input-operation")
45 public class InputsOperation extends AbstractOperation implements IInputsOperation {
46
47     private static final Logger log = Logger.getLogger(InputsOperation.class.getName());
48     @Autowired
49     PropertyOperation propertyOperation;
50
51     public <ElementDefinition> JanusGraphOperationStatus findAllResourceElementsDefinitionRecursively(String resourceId, List<ElementDefinition> elements, NodeElementFetcher<ElementDefinition> singleNodeFetcher) {
52
53         log.trace("Going to fetch elements under resource {}" , resourceId);
54         JanusGraphOperationStatus
55             resourceAttributesStatus = singleNodeFetcher.findAllNodeElements(resourceId, elements);
56
57         if (resourceAttributesStatus != JanusGraphOperationStatus.OK) {
58             return resourceAttributesStatus;
59         }
60
61         Either<ImmutablePair<ResourceMetadataData, GraphEdge>, JanusGraphOperationStatus> parentNodes = janusGraphGenericDao
62             .getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Resource), resourceId, GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.Resource, ResourceMetadataData.class);
63
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;
69             }
70         }
71
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
77             if (addParentIntStatus != JanusGraphOperationStatus.OK) {
78                 BeEcompErrorManager.getInstance().logInternalFlowError("findAllResourceElementsDefinitionRecursively", "Failed to find all resource elements of resource " + parentUniqueId, ErrorSeverity.ERROR);
79
80                 return addParentIntStatus;
81             }
82         }
83         return JanusGraphOperationStatus.OK;
84     }
85
86
87     @Override
88     public ImmutablePair<JanusGraphOperationStatus, String> findInputValue(String resourceInstanceId, String propertyId) {
89
90         log.debug("Going to check whether the property {} already added to resource instance {}", propertyId, resourceInstanceId);
91
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);
97         }
98
99         List<ComponentInstanceInput> list = getAllRes.left().value();
100         if (list != null) {
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);
108                 }
109             }
110         }
111
112         return new ImmutablePair<>(JanusGraphOperationStatus.NOT_FOUND, null);
113     }
114
115     /**
116      * return all properties associated to resource instance. The result does contains the property unique id but not its type, default value...
117      *
118      * @param resourceInstanceUid
119      * @return
120      */
121     public Either<List<ComponentInstanceInput>, JanusGraphOperationStatus> getAllInputsOfResourceInstanceOnlyInputDefId(String resourceInstanceUid) {
122
123         return getAllInputsOfResourceInstanceOnlyInputDefId(resourceInstanceUid, NodeTypeEnum.ResourceInstance);
124
125     }
126
127     public Either<List<ComponentInstanceInput>, JanusGraphOperationStatus> getAllInputsOfResourceInstanceOnlyInputDefId(String resourceInstanceUid, NodeTypeEnum instanceNodeType) {
128
129         Either<ComponentInstanceData, JanusGraphOperationStatus> findResInstanceRes = janusGraphGenericDao
130             .getNode(UniqueIdBuilder.getKeyByNodeType(instanceNodeType), resourceInstanceUid, ComponentInstanceData.class);
131
132         if (findResInstanceRes.isRight()) {
133             JanusGraphOperationStatus status = findResInstanceRes.right().value();
134             if (status == JanusGraphOperationStatus.NOT_FOUND) {
135                 status = JanusGraphOperationStatus.INVALID_ID;
136             }
137             return Either.right(status);
138         }
139
140         Either<List<ImmutablePair<InputValueData, GraphEdge>>, JanusGraphOperationStatus> propertyImplNodes = janusGraphGenericDao
141             .getChildrenNodes(UniqueIdBuilder.getKeyByNodeType(instanceNodeType), resourceInstanceUid, GraphEdgeLabels.INPUT_VALUE, NodeTypeEnum.InputValue, InputValueData.class);
142
143         if (propertyImplNodes.isRight()) {
144             JanusGraphOperationStatus status = propertyImplNodes.right().value();
145             return Either.right(status);
146         }
147
148         List<ImmutablePair<InputValueData, GraphEdge>> list = propertyImplNodes.left().value();
149         if (list == null || list.isEmpty()) {
150             return Either.right(JanusGraphOperationStatus.NOT_FOUND);
151         }
152
153         List<ComponentInstanceInput> result = new ArrayList<>();
154
155
156         for (ImmutablePair<InputValueData, GraphEdge> propertyValueDataPair : list) {
157
158             InputValueData propertyValueData = propertyValueDataPair.getLeft();
159             String propertyValueUid = propertyValueData.getUniqueId();
160             String value = propertyValueData.getValue();
161
162             Either<ImmutablePair<InputsData, GraphEdge>, JanusGraphOperationStatus> inputNodes = janusGraphGenericDao
163                 .getParentNode(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), propertyValueData.getUniqueId(), GraphEdgeLabels.GET_INPUT, NodeTypeEnum.Input, InputsData.class);
164
165             if (inputNodes.isRight()) {
166
167                 return Either.right(inputNodes.right().value());
168             }
169
170             InputsData input = inputNodes.left().value().left;
171             String inputId = input.getPropertyDataDefinition().getUniqueId();
172
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;
179                 }
180                 return Either.right(status);
181             }
182
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();
189
190                 return Either.right(status);
191             }
192             Edge edge = inputsEges.left().value();
193             String inputName = (String) janusGraphGenericDao
194                 .getProperty(edge, GraphEdgePropertiesDictionary.NAME.getProperty());
195
196             ComponentInstanceInput resourceInstanceProperty = new ComponentInstanceInput(propertyData.getPropertyDataDefinition(), inputId, value, propertyValueUid);
197
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);
205
206             result.add(resourceInstanceProperty);
207         }
208
209
210         return Either.left(result);
211     }
212
213     @Override
214     public ComponentInstanceInput buildResourceInstanceInput(InputValueData propertyValueData, ComponentInstanceInput resourceInstanceInput) {
215
216         String value = propertyValueData.getValue();
217         String uid = propertyValueData.getUniqueId();
218         ComponentInstanceInput instanceProperty = new ComponentInstanceInput(resourceInstanceInput, value, uid);
219         instanceProperty.setPath(resourceInstanceInput.getPath());
220
221         return instanceProperty;
222     }
223
224
225 }