Added oparent to sdc main
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / dao / janusgraph / transactions / SimpleJanusGraphTransactionManager.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.dao.janusgraph.transactions;
22
23 import org.janusgraph.core.JanusGraphException;
24 import org.janusgraph.core.JanusGraph;
25 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphClient;
26 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
27 import org.openecomp.sdc.common.log.wrappers.Logger;
28 import org.springframework.stereotype.Component;
29 import org.springframework.transaction.PlatformTransactionManager;
30 import org.springframework.transaction.TransactionDefinition;
31 import org.springframework.transaction.TransactionStatus;
32 import org.springframework.transaction.TransactionSystemException;
33 import org.springframework.transaction.support.SimpleTransactionStatus;
34
35 import javax.annotation.PostConstruct;
36
37 /**
38  * Simple transaction manager for the janusgraph database.
39  * This manager does not deal with transactions propagation and relies on the fact that transactions are automatically created with the first operation on the graph
40  */
41 @Component
42 public class SimpleJanusGraphTransactionManager implements PlatformTransactionManager {
43
44     private static final Logger log = Logger.getLogger(SimpleJanusGraphTransactionManager.class.getName());
45     private final JanusGraphClient janusGraphClient;
46     private JanusGraph janusGraph;
47
48     public SimpleJanusGraphTransactionManager(JanusGraphClient janusGraphClient) {
49         this.janusGraphClient = janusGraphClient;
50     }
51
52     @PostConstruct
53     public void onInit() {
54         janusGraph = janusGraphClient.getGraph().left().on(this::onFailingToStartJanusGraph);
55     }
56
57     @Override
58     public TransactionStatus getTransaction(TransactionDefinition transactionDefinition) {
59         log.debug("#getTransaction - returning simple transaction status");
60         return new SimpleTransactionStatus();
61     }
62
63     @Override
64     public void commit(TransactionStatus transactionStatus) {
65         log.debug("#commit - committing transaction");
66         try {
67             janusGraph.tx().commit();
68         } catch (JanusGraphException e) {
69             log.debug("#commit - failed to commit transaction", e);
70             throw new TransactionSystemException("failed to commit transaction", e);
71         }
72     }
73
74     @Override
75     public void rollback(TransactionStatus transactionStatus) {
76         log.debug("#rollback - committing transaction");
77         try {
78             janusGraph.tx().rollback();
79         } catch (JanusGraphException e) {
80             log.debug("#rollback - failed to rollback transaction", e);
81             throw new TransactionSystemException("failed to rollback transaction", e);
82         }
83     }
84
85     private JanusGraph onFailingToStartJanusGraph(JanusGraphOperationStatus err) {
86         log.debug("#onFailingToStartJanusGraph - could not open janusgraph client");
87         throw new IllegalStateException("janusgraph could not be initialized: " + err);
88     }
89
90 }