Reformat catalog-model
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / operations / impl / CommonTypeOperations.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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 package org.openecomp.sdc.be.model.operations.impl;
21
22 import static java.util.Collections.emptyList;
23
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Optional;
28 import java.util.function.Consumer;
29 import java.util.function.Function;
30 import java.util.stream.Collectors;
31 import org.openecomp.sdc.be.dao.graph.datatype.GraphNode;
32 import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphGenericDao;
33 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
34 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
35 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
36 import org.openecomp.sdc.be.model.PropertyDefinition;
37 import org.springframework.stereotype.Component;
38
39 @Component
40 public class CommonTypeOperations {
41
42     private final HealingJanusGraphGenericDao janusGraphGenericDao;
43     private final PropertyOperation propertyOperation;
44     private final OperationUtils operationUtils;
45
46     public CommonTypeOperations(HealingJanusGraphGenericDao janusGraphGenericDao, PropertyOperation propertyOperation,
47                                 OperationUtils operationUtils) {
48         this.janusGraphGenericDao = janusGraphGenericDao;
49         this.propertyOperation = propertyOperation;
50         this.operationUtils = operationUtils;
51     }
52
53     public <T extends GraphNode> void addType(T typeData, Class<T> clazz) {
54         janusGraphGenericDao.createNode(typeData, clazz).left().on(operationUtils::onJanusGraphOperationFailure);
55     }
56
57     public <T extends GraphNode> Optional<T> getType(String uniqueId, Class<T> clazz, NodeTypeEnum nodeType) {
58         T type = janusGraphGenericDao.getNode(UniqueIdBuilder.getKeyByNodeType(nodeType), uniqueId, clazz).left().on(err -> null);
59         return Optional.ofNullable(type);
60     }
61
62     public <T extends GraphNode> Optional<T> getLatestType(String type, Class<T> clazz, NodeTypeEnum nodeType) {
63         Map<String, Object> mapCriteria = new HashMap<>();
64         mapCriteria.put(GraphPropertiesDictionary.TYPE.getProperty(), type);
65         mapCriteria.put(GraphPropertiesDictionary.IS_HIGHEST_VERSION.getProperty(), true);
66         return janusGraphGenericDao.getByCriteria(nodeType, mapCriteria, clazz).left().on(err -> emptyList()).stream().findFirst();
67     }
68
69     public void addProperties(String uniqueId, NodeTypeEnum nodeType, List<PropertyDefinition> properties) {
70         propertyOperation.addPropertiesToElementType(uniqueId, nodeType, properties).left().on(operationUtils::onJanusGraphOperationFailure);
71     }
72
73     public void fillProperties(String uniqueId, NodeTypeEnum nodeType, Consumer<List<PropertyDefinition>> propertySetter) {
74         JanusGraphOperationStatus status = propertyOperation.fillPropertiesList(uniqueId, nodeType, propertySetter);
75         if (status != JanusGraphOperationStatus.OK) {
76             operationUtils.onJanusGraphOperationFailure(status);
77         }
78     }
79
80     /**
81      * Handle update of type without dervidedFrom attribute
82      */
83     public <T extends GraphNode> void updateType(T typeData, List<PropertyDefinition> properties, Class<T> clazz, NodeTypeEnum nodeType) {
84         janusGraphGenericDao.updateNode(typeData, clazz).left().on(operationUtils::onJanusGraphOperationFailure);
85         Map<String, PropertyDefinition> newProperties = properties.stream()
86             .collect(Collectors.toMap(PropertyDefinition::getName, Function.identity()));
87         propertyOperation.mergePropertiesAssociatedToNode(nodeType, typeData.getUniqueId(), newProperties).left()
88             .on(operationUtils::onJanusGraphOperationFailure);
89     }
90 }