Added oparent to sdc main
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / operations / impl / OperationUtils.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
21 package org.openecomp.sdc.be.model.operations.impl;
22
23 import fj.data.Either;
24 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
25 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
26 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
27 import org.openecomp.sdc.be.model.PropertyDefinition;
28 import org.openecomp.sdc.be.model.operations.StorageException;
29 import org.openecomp.sdc.common.log.wrappers.Logger;
30 import org.springframework.stereotype.Component;
31
32 import java.util.Map;
33
34 @Component
35 public class OperationUtils {
36
37     private final JanusGraphDao janusGraphDao;
38
39     private static final Logger logger = Logger.getLogger(OperationUtils.class.getName());
40
41     public OperationUtils(JanusGraphDao janusGraphDao) {
42         this.janusGraphDao = janusGraphDao;
43     }
44
45     public <T> T onJanusGraphOperationFailure(JanusGraphOperationStatus status) {
46         janusGraphDao.rollback();
47         throw new StorageException(status);
48     }
49
50     static Either<Map<String, PropertyDefinition>, JanusGraphOperationStatus> fillProperties(String uniqueId,
51                                                                                              PropertyOperation propertyOperation,
52                                                                                              NodeTypeEnum nodeTypeEnum) {
53
54         Either<Map<String, PropertyDefinition>, JanusGraphOperationStatus> findPropertiesOfNode =
55                 propertyOperation.findPropertiesOfNode(nodeTypeEnum, uniqueId);
56         if (findPropertiesOfNode.isRight()) {
57             JanusGraphOperationStatus janusGraphOperationStatus = findPropertiesOfNode.right().value();
58             logger.debug("After looking for properties of vertex {}. status is {}", uniqueId,
59                 janusGraphOperationStatus);
60             if (JanusGraphOperationStatus.NOT_FOUND.equals(janusGraphOperationStatus)) {
61                 return Either.right(JanusGraphOperationStatus.OK);
62             } else {
63                 return Either.right(janusGraphOperationStatus);
64             }
65         } else {
66             return Either.left(findPropertiesOfNode.left().value());
67         }
68     }
69 }