2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.impl;
23 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
24 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
25 import org.openecomp.sdc.be.model.Component;
26 import org.openecomp.sdc.be.model.operations.StorageException;
27 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
28 import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation;
29 import org.openecomp.sdc.common.log.wrappers.Logger;
32 @org.springframework.stereotype.Component
33 public class ComponentLocker {
35 private static final Logger log = Logger.getLogger(ComponentLocker.class.getName());
36 private final GraphLockOperation graphLockOperation;
38 public ComponentLocker(GraphLockOperation graphLockOperation) {
39 this.graphLockOperation = graphLockOperation;
42 public void lock(String id, ComponentTypeEnum componentType) {
43 lock(id, componentType.getNodeType());
46 public void lock(Component component) {
47 doLockComponent(component.getUniqueId(), component.getComponentType().getNodeType());
50 public void lock(String id, NodeTypeEnum nodeTypeEnum) {
51 doLockComponent(id, nodeTypeEnum);
54 private void doLockComponent(String id, NodeTypeEnum nodeTypeEnum) {
55 log.debug("#doLockComponent - locking component {} of type {}", id, nodeTypeEnum);
56 StorageOperationStatus storageOperationStatus = graphLockOperation.lockComponent(id, nodeTypeEnum);
57 if (storageOperationStatus != StorageOperationStatus.OK) {
58 log.debug("#doLockComponent - failed to lock component {} with status {}", id, storageOperationStatus);
59 throw new StorageException(storageOperationStatus);
63 public void unlock(String id, ComponentTypeEnum componentType) {
64 unlock(id, componentType.getNodeType());
67 public void unlock(String id, NodeTypeEnum nodeTypeEnum) {
68 log.debug("#unlock - unlocking component {} of type {}", id, nodeTypeEnum);
69 StorageOperationStatus storageOperationStatus = graphLockOperation.unlockComponent(id, nodeTypeEnum);
70 if (storageOperationStatus != StorageOperationStatus.OK) {
71 log.debug("#unlock - failed to unlock component {} with status {}", id, storageOperationStatus);
72 throw new StorageException(storageOperationStatus, id);