summaryrefslogtreecommitdiffstats
path: root/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/OperationUtils.java
blob: 2266acf88fa72bb9a6ed4ce926057961dbfe4ddd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package org.openecomp.sdc.be.model.operations.impl;

import fj.data.Either;
import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
import org.openecomp.sdc.be.model.PropertyDefinition;
import org.openecomp.sdc.be.model.operations.StorageException;
import org.openecomp.sdc.common.log.wrappers.Logger;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class OperationUtils {

    private final JanusGraphDao janusGraphDao;

    private static final Logger logger = Logger.getLogger(OperationUtils.class.getName());

    public OperationUtils(JanusGraphDao janusGraphDao) {
        this.janusGraphDao = janusGraphDao;
    }

    public <T> T onJanusGraphOperationFailure(JanusGraphOperationStatus status) {
        janusGraphDao.rollback();
        throw new StorageException(status);
    }

    static Either<Map<String, PropertyDefinition>, JanusGraphOperationStatus> fillProperties(String uniqueId,
                                                                                             PropertyOperation propertyOperation,
                                                                                             NodeTypeEnum nodeTypeEnum) {

        Either<Map<String, PropertyDefinition>, JanusGraphOperationStatus> findPropertiesOfNode =
                propertyOperation.findPropertiesOfNode(nodeTypeEnum, uniqueId);
        if (findPropertiesOfNode.isRight()) {
            JanusGraphOperationStatus janusGraphOperationStatus = findPropertiesOfNode.right().value();
            logger.debug("After looking for properties of vertex {}. status is {}", uniqueId,
                janusGraphOperationStatus);
            if (JanusGraphOperationStatus.NOT_FOUND.equals(janusGraphOperationStatus)) {
                return Either.right(JanusGraphOperationStatus.OK);
            } else {
                return Either.right(janusGraphOperationStatus);
            }
        } else {
            return Either.left(findPropertiesOfNode.left().value());
        }
    }
}