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