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.dao.graph.datatype.GraphEdge;
27 import org.openecomp.sdc.be.dao.graph.datatype.GraphRelation;
28 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao;
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.GraphPropertiesDictionary;
32 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
33 import org.openecomp.sdc.be.model.HeatParameterDefinition;
34 import org.openecomp.sdc.be.model.heat.HeatParameterType;
35 import org.openecomp.sdc.be.model.operations.api.IHeatParametersOperation;
36 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
37 import org.openecomp.sdc.be.model.tosca.converters.PropertyValueConverter;
38 import org.openecomp.sdc.be.model.tosca.validators.PropertyTypeValidator;
39 import org.openecomp.sdc.be.resources.data.HeatParameterData;
40 import org.openecomp.sdc.be.resources.data.HeatParameterValueData;
41 import org.openecomp.sdc.be.resources.data.UniqueIdData;
42 import org.openecomp.sdc.common.log.wrappers.Logger;
43 import org.springframework.stereotype.Component;
45 import java.util.ArrayList;
46 import java.util.HashMap;
47 import java.util.List;
50 @Component("heat-parameter-operation")
51 public class HeatParametersOperation implements IHeatParametersOperation {
53 public static final String EMPTY_VALUE = null;
55 private static final Logger log = Logger.getLogger(HeatParametersOperation.class.getName());
57 @javax.annotation.Resource
58 private JanusGraphGenericDao janusGraphGenericDao;
60 public JanusGraphGenericDao getJanusGraphGenericDao() {
61 return janusGraphGenericDao;
64 public void setJanusGraphGenericDao(JanusGraphGenericDao janusGraphGenericDao) {
65 this.janusGraphGenericDao = janusGraphGenericDao;
68 public StorageOperationStatus getHeatParametersOfNode(NodeTypeEnum nodeType, String uniqueId, List<HeatParameterDefinition> properties) {
70 Either<List<ImmutablePair<HeatParameterData, GraphEdge>>, JanusGraphOperationStatus> childrenNodes = janusGraphGenericDao
71 .getChildrenNodes(UniqueIdBuilder.getKeyByNodeType(nodeType), uniqueId, GraphEdgeLabels.HEAT_PARAMETER, NodeTypeEnum.HeatParameter,
72 HeatParameterData.class);
74 if (childrenNodes.isRight()) {
75 JanusGraphOperationStatus status = childrenNodes.right().value();
76 if (status == JanusGraphOperationStatus.NOT_FOUND) {
77 status = JanusGraphOperationStatus.OK;
79 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
82 List<ImmutablePair<HeatParameterData, GraphEdge>> values = childrenNodes.left().value();
85 for (ImmutablePair<HeatParameterData, GraphEdge> immutablePair : values) {
86 GraphEdge edge = immutablePair.getValue();
87 String propertyName = (String) edge.getProperties().get(GraphPropertiesDictionary.NAME.getProperty());
88 if (log.isDebugEnabled())
89 log.debug("Property {} is associated to node {}", propertyName, uniqueId);
90 HeatParameterData propertyData = immutablePair.getKey();
91 HeatParameterDefinition propertyDefinition = convertParameterDataToParameterDefinition(propertyData, propertyName, uniqueId);
93 properties.add(propertyDefinition);
95 if (log.isTraceEnabled()) {
96 log.trace("getHeatParametersOfNode - property {} associated to node {}", propertyDefinition, uniqueId);
102 return StorageOperationStatus.OK;
105 public StorageOperationStatus getParametersValueNodes(NodeTypeEnum parentNodeType, String parentUniqueId, List<HeatParameterValueData> heatValues) {
107 Either<List<ImmutablePair<HeatParameterValueData, GraphEdge>>, JanusGraphOperationStatus> childrenNodes = janusGraphGenericDao
108 .getChildrenNodes(UniqueIdBuilder.getKeyByNodeType(parentNodeType), parentUniqueId, GraphEdgeLabels.PARAMETER_VALUE,
109 NodeTypeEnum.HeatParameterValue, HeatParameterValueData.class);
111 if (childrenNodes.isRight()) {
112 JanusGraphOperationStatus status = childrenNodes.right().value();
113 if (status == JanusGraphOperationStatus.NOT_FOUND) {
114 status = JanusGraphOperationStatus.OK;
116 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
119 List<ImmutablePair<HeatParameterValueData, GraphEdge>> values = childrenNodes.left().value();
120 if (values != null) {
122 for (ImmutablePair<HeatParameterValueData, GraphEdge> immutablePair : values) {
123 GraphEdge edge = immutablePair.getValue();
124 String propertyName = (String) edge.getProperties().get(GraphPropertiesDictionary.NAME.getProperty());
125 log.trace("Heat value {} is associated to node {}", propertyName,parentUniqueId);
126 HeatParameterValueData propertyData = immutablePair.getKey();
128 heatValues.add(propertyData);
133 return StorageOperationStatus.OK;
137 public Either<List<HeatParameterDefinition>, StorageOperationStatus> deleteAllHeatParametersAssociatedToNode(NodeTypeEnum nodeType, String uniqueId) {
139 List<HeatParameterDefinition> heatParams = new ArrayList<>();
140 StorageOperationStatus propertiesOfNodeRes = getHeatParametersOfNode(nodeType, uniqueId, heatParams);
142 if (!propertiesOfNodeRes.equals(StorageOperationStatus.OK) && !propertiesOfNodeRes.equals(StorageOperationStatus.NOT_FOUND)) {
143 return Either.right(propertiesOfNodeRes);
146 for (HeatParameterDefinition propertyDefinition : heatParams) {
148 String propertyUid = propertyDefinition.getUniqueId();
149 Either<HeatParameterData, JanusGraphOperationStatus> deletePropertyRes = deleteHeatParameterFromGraph(propertyUid);
150 if (deletePropertyRes.isRight()) {
151 log.error("Failed to delete heat parameter with id {}", propertyUid);
152 JanusGraphOperationStatus status = deletePropertyRes.right().value();
153 if (status == JanusGraphOperationStatus.NOT_FOUND) {
154 status = JanusGraphOperationStatus.INVALID_ID;
156 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
161 log.debug("The heat parameters deleted from node {} are {}", uniqueId, heatParams);
162 return Either.left(heatParams);
166 public StorageOperationStatus deleteAllHeatValuesAssociatedToNode(NodeTypeEnum parentNodeType, String parentUniqueId) {
168 List<HeatParameterValueData> heatValues = new ArrayList<>();
169 StorageOperationStatus propertiesOfNodeRes = getParametersValueNodes(parentNodeType, parentUniqueId, heatValues);
171 if (!propertiesOfNodeRes.equals(StorageOperationStatus.OK) && !propertiesOfNodeRes.equals(StorageOperationStatus.NOT_FOUND)) {
172 return propertiesOfNodeRes;
175 for (HeatParameterValueData propertyDefinition : heatValues) {
177 String propertyUid = (String) propertyDefinition.getUniqueId();
178 Either<HeatParameterValueData, JanusGraphOperationStatus> deletePropertyRes = deleteHeatParameterValueFromGraph(propertyUid);
179 if (deletePropertyRes.isRight()) {
180 log.error("Failed to delete heat parameter value with id {}", propertyUid);
181 JanusGraphOperationStatus status = deletePropertyRes.right().value();
182 if (status == JanusGraphOperationStatus.NOT_FOUND) {
183 status = JanusGraphOperationStatus.INVALID_ID;
185 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
190 log.debug("The heat values deleted from node {} are {}" , parentUniqueId, heatValues);
191 return StorageOperationStatus.OK;
194 private Either<HeatParameterData, JanusGraphOperationStatus> deleteHeatParameterFromGraph(String propertyId) {
195 log.debug("Before deleting heat parameter from graph {}" , propertyId);
196 return janusGraphGenericDao
197 .deleteNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.HeatParameter), propertyId, HeatParameterData.class);
200 private Either<HeatParameterValueData, JanusGraphOperationStatus> deleteHeatParameterValueFromGraph(String propertyId) {
201 log.debug("Before deleting heat parameter from graph {}" , propertyId);
202 return janusGraphGenericDao.deleteNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.HeatParameterValue), propertyId, HeatParameterValueData.class);
206 public StorageOperationStatus addPropertiesToGraph(List<HeatParameterDefinition> properties, String parentId, NodeTypeEnum nodeType) {
208 if (properties != null) {
209 for (HeatParameterDefinition propertyDefinition : properties) {
211 String propertyName = propertyDefinition.getName();
213 Either<HeatParameterData, JanusGraphOperationStatus> addPropertyToGraph = addPropertyToGraph(propertyName, propertyDefinition, parentId, nodeType);
215 if (addPropertyToGraph.isRight()) {
216 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(addPropertyToGraph.right().value());
221 return StorageOperationStatus.OK;
226 public StorageOperationStatus updateHeatParameters(List<HeatParameterDefinition> properties) {
228 if (properties == null) {
229 return StorageOperationStatus.OK;
231 for (HeatParameterDefinition property : properties) {
233 HeatParameterData heatParameterData = new HeatParameterData(property);
234 Either<HeatParameterData, JanusGraphOperationStatus> updateNode = janusGraphGenericDao
235 .updateNode(heatParameterData, HeatParameterData.class);
236 if (updateNode.isRight()) {
237 log.debug("failed to update heat parameter in graph. id = {}", property.getUniqueId());
238 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(updateNode.right().value());
242 return StorageOperationStatus.OK;
245 public Either<HeatParameterData, JanusGraphOperationStatus> addPropertyToGraph(String propertyName, HeatParameterDefinition propertyDefinition, String parentId, NodeTypeEnum nodeType) {
247 UniqueIdData parentNode = new UniqueIdData(nodeType, parentId);
249 propertyDefinition.setUniqueId(UniqueIdBuilder.buildHeatParameterUniqueId(parentId, propertyName));
250 HeatParameterData propertyData = new HeatParameterData(propertyDefinition);
252 log.debug("Before adding property to graph {}" , propertyData);
253 Either<HeatParameterData, JanusGraphOperationStatus> createNodeResult = janusGraphGenericDao
254 .createNode(propertyData, HeatParameterData.class);
255 log.debug("After adding property to graph {}" , propertyData);
256 if (createNodeResult.isRight()) {
257 JanusGraphOperationStatus operationStatus = createNodeResult.right().value();
258 log.error("Failed to add property {} to graph. status is {}", propertyName, operationStatus);
259 return Either.right(operationStatus);
262 Map<String, Object> props = new HashMap<>();
263 props.put(GraphPropertiesDictionary.NAME.getProperty(), propertyName);
264 Either<GraphRelation, JanusGraphOperationStatus> createRelResult = janusGraphGenericDao
265 .createRelation(parentNode, propertyData, GraphEdgeLabels.HEAT_PARAMETER, props);
266 if (createRelResult.isRight()) {
267 JanusGraphOperationStatus operationStatus = createRelResult.right().value();
268 log.error("Failed to associate {} {} to heat parameter {} in graph. status is {}", nodeType.getName(), parentId, propertyName, operationStatus);
269 return Either.right(operationStatus);
272 return Either.left(createNodeResult.left().value());
276 public StorageOperationStatus validateAndUpdateProperty(HeatParameterDefinition propertyDefinition) {
278 log.trace("Going to validate property type and value. {}" , propertyDefinition);
280 String propertyType = propertyDefinition.getType();
281 HeatParameterType type = getType(propertyType);
284 log.info("The type {} of heat parameter is invalid", type);
286 return StorageOperationStatus.INVALID_TYPE;
288 propertyDefinition.setType(type.getType());
290 log.trace("After validating property type {}", propertyType);
292 // validate default value
293 String defaultValue = propertyDefinition.getDefaultValue();
294 boolean isValidProperty = isValidValue(type, defaultValue);
295 if (!isValidProperty) {
296 log.info("The value {} of property from type {} is invalid", defaultValue, type);
297 return StorageOperationStatus.INVALID_VALUE;
300 PropertyValueConverter converter = type.getConverter();
302 if (isEmptyValue(defaultValue)) {
303 log.debug("Default value was not sent for property {}. Set default value to {}", propertyDefinition.getName() , EMPTY_VALUE);
305 propertyDefinition.setDefaultValue(EMPTY_VALUE);
306 } else if (!isEmptyValue(defaultValue)) {
307 String convertedValue = converter.convert(defaultValue, null, null);
308 propertyDefinition.setDefaultValue(convertedValue);
311 // validate current value
312 String value = propertyDefinition.getCurrentValue();
313 isValidProperty = isValidValue(type, value);
314 if (!isValidProperty) {
315 log.info("The value {} of property from type {} is invalid", value, type);
316 return StorageOperationStatus.INVALID_VALUE;
319 if (isEmptyValue(value)) {
320 log.debug("Value was not sent for property {}. Set value to {}", propertyDefinition.getName(), EMPTY_VALUE);
322 propertyDefinition.setCurrentValue(EMPTY_VALUE);
323 } else if (!value.equals("")) {
324 String convertedValue = converter.convert(value, null, null);
325 propertyDefinition.setCurrentValue(convertedValue);
328 return StorageOperationStatus.OK;
331 public HeatParameterDefinition convertParameterDataToParameterDefinition(HeatParameterData propertyDataResult, String propertyName, String resourceId) {
332 log.debug("convert to HeatParamereDefinition {}", propertyDataResult);
334 HeatParameterDefinition propertyDefResult = new HeatParameterDefinition(propertyDataResult.getHeatDataDefinition());
336 propertyDefResult.setName(propertyName);
338 return propertyDefResult;
341 private HeatParameterType getType(String propertyType) {
343 return HeatParameterType.isValidType(propertyType);
347 protected boolean isValidValue(HeatParameterType type, String value) {
348 if (isEmptyValue(value)) {
352 PropertyTypeValidator validator = type.getValidator();
354 boolean isValid = validator.isValid(value, null, null);
363 public boolean isEmptyValue(String value) {
370 public boolean isNullParam(String value) {
378 public Either<HeatParameterValueData, StorageOperationStatus> updateHeatParameterValue(HeatParameterDefinition heatParam, String artifactId, String resourceInstanceId, String artifactLabel) {
379 String heatEnvId = UniqueIdBuilder.buildHeatParameterValueUniqueId(resourceInstanceId, artifactLabel, heatParam.getName());
380 Either<HeatParameterValueData, JanusGraphOperationStatus> getNode = janusGraphGenericDao
381 .getNode(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), heatEnvId, HeatParameterValueData.class);
382 if (getNode.isRight() || getNode.left().value() == null) {
383 if (heatParam.getCurrentValue() == null || (heatParam.getDefaultValue() != null && heatParam.getCurrentValue().equals(heatParam.getDefaultValue()))) {
384 log.debug("Updated heat parameter value equals default value. No need to create heat parameter value for heat parameter {}", heatParam.getUniqueId());
385 return Either.left(null);
387 return createHeatParameterValue(heatParam, artifactId, resourceInstanceId, artifactLabel);
389 heatParam.setUniqueId(heatEnvId);
390 return updateHeatParameterValue(heatParam);
394 public Either<HeatParameterValueData, StorageOperationStatus> updateHeatParameterValue(HeatParameterDefinition heatParam) {
395 HeatParameterValueData heatParameterValue = new HeatParameterValueData();
396 heatParameterValue.setUniqueId(heatParam.getUniqueId());
397 if (heatParam.getCurrentValue() == null || (heatParam.getDefaultValue() != null && heatParam.getCurrentValue().equals(heatParam.getDefaultValue()))) {
398 Either<GraphRelation, JanusGraphOperationStatus> deleteParameterValueIncomingRelation = janusGraphGenericDao
399 .deleteIncomingRelationByCriteria(heatParameterValue, GraphEdgeLabels.PARAMETER_VALUE, null);
400 if (deleteParameterValueIncomingRelation.isRight()) {
401 log.debug("Failed to delete heat parameter value incoming relation on graph. id = {}", heatParameterValue.getUniqueId());
402 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(deleteParameterValueIncomingRelation.right().value()));
404 Either<Edge, JanusGraphOperationStatus> getOutgoingRelation = janusGraphGenericDao
405 .getOutgoingEdgeByCriteria(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), (String) heatParameterValue.getUniqueId(), GraphEdgeLabels.PARAMETER_IMPL, null);
406 if (getOutgoingRelation.isRight()) {
407 log.debug("Failed to get heat parameter value outgoing relation from graph. id = {}", heatParameterValue.getUniqueId());
408 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getOutgoingRelation.right().value()));
410 Edge edge = getOutgoingRelation.left().value();
412 log.debug("Failed to get heat parameter value outgoing relation from graph. id = {}", heatParameterValue.getUniqueId());
413 return Either.right(StorageOperationStatus.GENERAL_ERROR);
417 Either<HeatParameterValueData, JanusGraphOperationStatus> deleteNode = janusGraphGenericDao
418 .deleteNode(heatParameterValue, HeatParameterValueData.class);
419 if (deleteNode.isRight()) {
420 log.debug("Failed to delete heat parameter value on graph. id = {}", heatParameterValue.getUniqueId());
421 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(deleteNode.right().value()));
423 return Either.left(deleteNode.left().value());
425 heatParameterValue.setValue(heatParam.getCurrentValue());
426 Either<HeatParameterValueData, JanusGraphOperationStatus> updateNode = janusGraphGenericDao
427 .updateNode(heatParameterValue, HeatParameterValueData.class);
428 if (updateNode.isRight()) {
429 log.debug("Failed to update heat parameter value in graph. id = {}", heatParameterValue.getUniqueId());
430 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(updateNode.right().value()));
432 return Either.left(updateNode.left().value());
435 public Either<HeatParameterValueData, StorageOperationStatus> createHeatParameterValue(HeatParameterDefinition heatParam, String artifactId, String resourceInstanceId, String artifactLabel) {
437 Either<HeatParameterValueData, JanusGraphOperationStatus> addHeatValueToGraph = addHeatValueToGraph(heatParam, artifactLabel, artifactId, resourceInstanceId);
438 if (addHeatValueToGraph.isRight()) {
439 log.debug("Failed to create heat parameters value on graph for artifact {}", artifactId);
440 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(addHeatValueToGraph.right().value()));
442 return Either.left(addHeatValueToGraph.left().value());
445 public Either<HeatParameterValueData, JanusGraphOperationStatus> addHeatValueToGraph(HeatParameterDefinition heatParameter, String artifactLabel, String artifactId, String resourceInstanceId) {
447 UniqueIdData heatEnvNode = new UniqueIdData(NodeTypeEnum.ArtifactRef, artifactId);
448 HeatParameterValueData heatValueData = new HeatParameterValueData();
449 heatValueData.setUniqueId(UniqueIdBuilder.buildHeatParameterValueUniqueId(resourceInstanceId, artifactLabel, heatParameter.getName()));
450 heatValueData.setValue(heatParameter.getCurrentValue());
452 log.debug("Before adding property to graph {}", heatValueData);
453 Either<HeatParameterValueData, JanusGraphOperationStatus> createNodeResult = janusGraphGenericDao
454 .createNode(heatValueData, HeatParameterValueData.class);
455 log.debug("After adding property to graph {}", heatValueData);
456 if (createNodeResult.isRight()) {
457 JanusGraphOperationStatus operationStatus = createNodeResult.right().value();
458 log.error("Failed to add heat value {} to graph. status is {}", heatValueData.getUniqueId(), operationStatus);
459 return Either.right(operationStatus);
462 Map<String, Object> props = new HashMap<>();
463 props.put(GraphPropertiesDictionary.NAME.getProperty(), heatParameter.getName());
464 Either<GraphRelation, JanusGraphOperationStatus> createRelResult = janusGraphGenericDao
465 .createRelation(heatEnvNode, heatValueData, GraphEdgeLabels.PARAMETER_VALUE, props);
466 if (createRelResult.isRight()) {
467 JanusGraphOperationStatus operationStatus = createRelResult.right().value();
468 log.error("Failed to associate heat value {} to heat env artifact {} in graph. status is {}", heatValueData.getUniqueId(), artifactId, operationStatus);
469 return Either.right(operationStatus);
471 UniqueIdData heatParameterNode = new UniqueIdData(NodeTypeEnum.HeatParameter, heatParameter.getUniqueId());
472 Either<GraphRelation, JanusGraphOperationStatus> createRel2Result = janusGraphGenericDao
473 .createRelation(heatValueData, heatParameterNode, GraphEdgeLabels.PARAMETER_IMPL, null);
474 if (createRel2Result.isRight()) {
475 JanusGraphOperationStatus operationStatus = createRel2Result.right().value();
476 log.error("Failed to associate heat value {} to heat parameter {} in graph. status is {}", heatValueData.getUniqueId(), heatParameter.getName(), operationStatus);
477 return Either.right(operationStatus);
480 return Either.left(createNodeResult.left().value());