re base code
[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.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;
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> TitanOperationStatus findAllResourceElementsDefinitionRecursively(String resourceId, List<ElementDefinition> elements, NodeElementFetcher<ElementDefinition> singleNodeFetcher) {
52
53         log.trace("Going to fetch elements under resource {}" , resourceId);
54         TitanOperationStatus resourceAttributesStatus = singleNodeFetcher.findAllNodeElements(resourceId, elements);
55
56         if (resourceAttributesStatus != TitanOperationStatus.OK) {
57             return resourceAttributesStatus;
58         }
59
60         Either<ImmutablePair<ResourceMetadataData, GraphEdge>, TitanOperationStatus> parentNodes = titanGenericDao.getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Resource), resourceId, GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.Resource, ResourceMetadataData.class);
61
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;
67             }
68         }
69
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);
74
75             if (addParentIntStatus != TitanOperationStatus.OK) {
76                 BeEcompErrorManager.getInstance().logInternalFlowError("findAllResourceElementsDefinitionRecursively", "Failed to find all resource elements of resource " + parentUniqueId, ErrorSeverity.ERROR);
77
78                 return addParentIntStatus;
79             }
80         }
81         return TitanOperationStatus.OK;
82     }
83
84
85     @Override
86     public ImmutablePair<TitanOperationStatus, String> findInputValue(String resourceInstanceId, String propertyId) {
87
88         log.debug("Going to check whether the property {} already added to resource instance {}", propertyId, resourceInstanceId);
89
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);
95         }
96
97         List<ComponentInstanceInput> list = getAllRes.left().value();
98         if (list != null) {
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);
106                 }
107             }
108         }
109
110         return new ImmutablePair<>(TitanOperationStatus.NOT_FOUND, null);
111     }
112
113     /**
114      * return all properties associated to resource instance. The result does contains the property unique id but not its type, default value...
115      *
116      * @param resourceInstanceUid
117      * @return
118      */
119     public Either<List<ComponentInstanceInput>, TitanOperationStatus> getAllInputsOfResourceInstanceOnlyInputDefId(String resourceInstanceUid) {
120
121         return getAllInputsOfResourceInstanceOnlyInputDefId(resourceInstanceUid, NodeTypeEnum.ResourceInstance);
122
123     }
124
125     public Either<List<ComponentInstanceInput>, TitanOperationStatus> getAllInputsOfResourceInstanceOnlyInputDefId(String resourceInstanceUid, NodeTypeEnum instanceNodeType) {
126
127         Either<ComponentInstanceData, TitanOperationStatus> findResInstanceRes = titanGenericDao.getNode(UniqueIdBuilder.getKeyByNodeType(instanceNodeType), resourceInstanceUid, ComponentInstanceData.class);
128
129         if (findResInstanceRes.isRight()) {
130             TitanOperationStatus status = findResInstanceRes.right().value();
131             if (status == TitanOperationStatus.NOT_FOUND) {
132                 status = TitanOperationStatus.INVALID_ID;
133             }
134             return Either.right(status);
135         }
136
137         Either<List<ImmutablePair<InputValueData, GraphEdge>>, TitanOperationStatus> propertyImplNodes = titanGenericDao.getChildrenNodes(UniqueIdBuilder.getKeyByNodeType(instanceNodeType), resourceInstanceUid, GraphEdgeLabels.INPUT_VALUE, NodeTypeEnum.InputValue, InputValueData.class);
138
139         if (propertyImplNodes.isRight()) {
140             TitanOperationStatus status = propertyImplNodes.right().value();
141             return Either.right(status);
142         }
143
144         List<ImmutablePair<InputValueData, GraphEdge>> list = propertyImplNodes.left().value();
145         if (list == null || list.isEmpty()) {
146             return Either.right(TitanOperationStatus.NOT_FOUND);
147         }
148
149         List<ComponentInstanceInput> result = new ArrayList<>();
150
151
152         for (ImmutablePair<InputValueData, GraphEdge> propertyValueDataPair : list) {
153
154             InputValueData propertyValueData = propertyValueDataPair.getLeft();
155             String propertyValueUid = propertyValueData.getUniqueId();
156             String value = propertyValueData.getValue();
157
158             Either<ImmutablePair<InputsData, GraphEdge>, TitanOperationStatus> inputNodes = titanGenericDao.getParentNode(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), propertyValueData.getUniqueId(), GraphEdgeLabels.GET_INPUT, NodeTypeEnum.Input, InputsData.class);
159
160             if (inputNodes.isRight()) {
161
162                 return Either.right(inputNodes.right().value());
163             }
164
165             InputsData input = inputNodes.left().value().left;
166             String inputId = input.getPropertyDataDefinition().getUniqueId();
167
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;
173                 }
174                 return Either.right(status);
175             }
176
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();
182
183                 return Either.right(status);
184             }
185             Edge edge = inputsEges.left().value();
186             String inputName = (String) titanGenericDao.getProperty(edge, GraphEdgePropertiesDictionary.NAME.getProperty());
187
188             ComponentInstanceInput resourceInstanceProperty = new ComponentInstanceInput(propertyData.getPropertyDataDefinition(), inputId, value, propertyValueUid);
189
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);
197
198             result.add(resourceInstanceProperty);
199         }
200
201
202         return Either.left(result);
203     }
204
205     @Override
206     public ComponentInstanceInput buildResourceInstanceInput(InputValueData propertyValueData, ComponentInstanceInput resourceInstanceInput) {
207
208         String value = propertyValueData.getValue();
209         String uid = propertyValueData.getUniqueId();
210         ComponentInstanceInput instanceProperty = new ComponentInstanceInput(resourceInstanceInput, value, uid);
211         instanceProperty.setPath(resourceInstanceInput.getPath());
212
213         return instanceProperty;
214     }
215
216
217 }