Disable locking during deployment
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / operations / impl / GraphLockOperation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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 org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao;
23 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
24 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
25 import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
26 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
27 import org.openecomp.sdc.common.log.wrappers.Logger;
28 import org.springframework.stereotype.Component;
29
30 import lombok.NoArgsConstructor;
31
32 @Component("graph-lock-operation")
33 @NoArgsConstructor
34 public class GraphLockOperation implements IGraphLockOperation {
35
36     private static final Logger log = Logger.getLogger(GraphLockOperation.class.getName());
37     private boolean disable = false;
38     @javax.annotation.Resource
39     private JanusGraphGenericDao janusGraphGenericDao;
40
41     /*
42      * (non-Javadoc)
43      *
44      * @see org.openecomp.sdc.be.model.operations.impl.IGraphLockOperation# lockResource(java.lang.String, org.openecomp.sdc.be.model.operations.api.IResourceOperation)
45      */
46     @Override
47     public StorageOperationStatus lockComponent(String componentId, NodeTypeEnum nodeType) {
48         log.info("lock resource with id {}", componentId);
49         JanusGraphOperationStatus lockElementStatus = null;
50         try {
51             lockElementStatus = disable ? JanusGraphOperationStatus.OK : janusGraphGenericDao.lockElement(componentId, nodeType);
52         } catch (Exception e) {
53             lockElementStatus = JanusGraphOperationStatus.ALREADY_LOCKED;
54         }
55         return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(lockElementStatus);
56     }
57
58     /*
59      * (non-Javadoc)
60      *
61      * @see org.openecomp.sdc.be.model.operations.impl.IGraphLockOperation# unlockResource(java.lang.String, org.openecomp.sdc.be.model.operations.api.IResourceOperation)
62      */
63     @Override
64     public StorageOperationStatus unlockComponent(String componentId, NodeTypeEnum nodeType) {
65         JanusGraphOperationStatus lockElementStatus = disable ? JanusGraphOperationStatus.OK : janusGraphGenericDao.releaseElement(componentId, nodeType);
66         return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(lockElementStatus);
67     }
68
69     @Override
70     public StorageOperationStatus unlockComponentByName(String name, String componentId, NodeTypeEnum nodeType) {
71         JanusGraphOperationStatus lockElementStatus = disable ? JanusGraphOperationStatus.OK : janusGraphGenericDao.releaseElement(name, nodeType);
72         return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(lockElementStatus);
73     }
74
75     @Override
76     public StorageOperationStatus lockComponentByName(String name, NodeTypeEnum nodeType) {
77         log.info("lock resource with name {}", name);
78         JanusGraphOperationStatus lockElementStatus = null;
79         try {
80             lockElementStatus = disable ? JanusGraphOperationStatus.OK : janusGraphGenericDao.lockElement(name, nodeType);
81         } catch (Exception e) {
82             lockElementStatus = JanusGraphOperationStatus.ALREADY_LOCKED;
83         }
84         return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(lockElementStatus);
85     }
86
87     @Override
88     public boolean disableLocking(boolean disable) {
89         log.info("Toggling disable locking from {} to {}", this.disable, disable);
90         this.disable = disable;
91         return this.disable;
92     }
93 }