summaryrefslogtreecommitdiffstats
path: root/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/GraphLockOperation.java
blob: 89f8f71e2aaebc1ba925fed7dd87c24e121f27ff (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.be.model.operations.impl;

import org.openecomp.sdc.be.dao.titan.TitanGenericDao;
import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition;
import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
import org.openecomp.sdc.be.model.Resource;
import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
import org.openecomp.sdc.be.model.operations.api.ICacheMangerOperation;
import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.be.resources.data.ComponentMetadataData;
import org.openecomp.sdc.be.resources.data.ResourceMetadataData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import fj.data.Either;

@Component("graph-lock-operation")
public class GraphLockOperation implements IGraphLockOperation {
	private static Logger log = LoggerFactory.getLogger(ResourceOperation.class.getName());

	@javax.annotation.Resource
	private TitanGenericDao titanGenericDao;
	
	@Autowired
	ToscaOperationFacade toscaOperationFacade;

	@javax.annotation.Resource
	private ICacheMangerOperation cacheManagerOperation;

	public GraphLockOperation() {
		super();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.openecomp.sdc.be.model.operations.impl.IGraphLockOperation# lockResource(java.lang.String, org.openecomp.sdc.be.model.operations.api.IResourceOperation)
	 */
	@Override
	public StorageOperationStatus lockComponent(String componentId, NodeTypeEnum nodeType) {
		log.info("lock resource with id {}", componentId);
		TitanOperationStatus lockElementStatus = null;
		try {

			// SET LAST UPDATE DATE OF THE COMPONENT.
			// In this way we mark the component as updated one (and component
			// won't be fetched from cache since the component in cache has
			// different timestamp)
//			Either<ComponentMetadataData, TitanOperationStatus> updateTime = updateModificationTimeOfComponent(componentId, nodeType);
//			if (updateTime.isRight()) {
//				TitanOperationStatus operationStatus = updateTime.right().value();
//				if (operationStatus != TitanOperationStatus.OK) {
//					return DaoStatusConverter.convertTitanStatusToStorageStatus(operationStatus);
//				}
//			}

			lockElementStatus = titanGenericDao.lockElement(componentId, nodeType);

		} catch (Exception e) {
			lockElementStatus = TitanOperationStatus.ALREADY_LOCKED;

		}

		return DaoStatusConverter.convertTitanStatusToStorageStatus(lockElementStatus);

	}

	/**
	 * update the last update date of the component
	 * 
	 * @param componentId
	 * @param nodeType
	 * @return
	 */
	private Either<ComponentMetadataData, TitanOperationStatus> updateModificationTimeOfComponent(String componentId, NodeTypeEnum nodeType) {

		if (nodeType == NodeTypeEnum.Resource || nodeType == NodeTypeEnum.Service || nodeType == NodeTypeEnum.Product) {
			// We fetch all node since update only timestamp make problems since
			// there is default resource type (VFC) which changes component
			// resource type when we update only timestamp(ResourceMetadataData
			// contains default value VFC on resourceType field).
			Either<ComponentMetadataData, TitanOperationStatus> findComp = titanGenericDao.getNode(UniqueIdBuilder.getKeyByNodeType(nodeType), componentId, ComponentMetadataData.class);

			if (findComp.isRight()) {
				return Either.right(findComp.right().value());
			}
			ComponentMetadataData componentMetadataData = findComp.left().value();
			componentMetadataData.getMetadataDataDefinition().setLastUpdateDate(System.currentTimeMillis());
			Either<ComponentMetadataData, TitanOperationStatus> updateNode = titanGenericDao.updateNode(componentMetadataData, ComponentMetadataData.class);
			return updateNode;
		}
		return Either.right(TitanOperationStatus.OK);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.openecomp.sdc.be.model.operations.impl.IGraphLockOperation# unlockResource(java.lang.String, org.openecomp.sdc.be.model.operations.api.IResourceOperation)
	 */
	@Override
	public StorageOperationStatus unlockComponent(String componentId, NodeTypeEnum nodeType) {

//		Either<Long, StorageOperationStatus> addComponentToCachePart1 = addComponentToCachePart1WithoutCommit(componentId, nodeType);

		TitanOperationStatus lockElementStatus = titanGenericDao.releaseElement(componentId, nodeType);

//		if (addComponentToCachePart1.isLeft()) {
//			Long lastUpdateDate = addComponentToCachePart1.left().value();
//			addComponentToCachePart2(componentId, lastUpdateDate, nodeType);
//		}
//
		return DaoStatusConverter.convertTitanStatusToStorageStatus(lockElementStatus);
	}

	private ResourceMetadataData getResourceMetaDataFromResource(Resource resource) {
		ResourceMetadataData resourceData = new ResourceMetadataData((ResourceMetadataDataDefinition) resource.getComponentMetadataDefinition().getMetadataDataDefinition());
		return resourceData;
	}

	@Override
	public StorageOperationStatus unlockComponentByName(String name, String componentId, NodeTypeEnum nodeType) {

//		Either<Long, StorageOperationStatus> addComponentToCachePart1 = addComponentToCachePart1WithoutCommit(componentId, nodeType);

		TitanOperationStatus lockElementStatus = titanGenericDao.releaseElement(name, nodeType);
//
//		if (addComponentToCachePart1.isLeft()) {
//			Long lastUpdateDate = addComponentToCachePart1.left().value();
//			addComponentToCachePart2(componentId, lastUpdateDate, nodeType);
//		}

		return DaoStatusConverter.convertTitanStatusToStorageStatus(lockElementStatus);
	}

	/**
	 * We fetch the last update date of the component
	 * 
	 * @param componentId
	 * @param nodeType
	 * @return
	 */
	private Either<Long, StorageOperationStatus> addComponentToCachePart1WithoutCommit(String componentId, NodeTypeEnum nodeType) {
		if (componentId != null) { // In case of error, the componentId might be
									// empty.
			if (nodeType == NodeTypeEnum.Resource || nodeType == NodeTypeEnum.Service || nodeType == NodeTypeEnum.Product) {
				Long lastUpdateDate = null;
				Either<ComponentMetadataData, StorageOperationStatus> resResult = toscaOperationFacade.getComponentMetadata(componentId);
				if (resResult.isLeft()) {
					ComponentMetadataData resourceMetadataData = resResult.left().value();
					lastUpdateDate = resourceMetadataData.getMetadataDataDefinition().getLastUpdateDate();

					return Either.left(lastUpdateDate);
				} else {
					return Either.right(resResult.right().value());
				}
			}
		}
		return Either.right(StorageOperationStatus.OPERATION_NOT_SUPPORTED);
	}

	/**
	 * add the component to the cache
	 * 
	 * @param componentId
	 * @param lastUpdateDate
	 * @param nodeType
	 * @return
	 */
	private Either<Long, StorageOperationStatus> addComponentToCachePart2(String componentId, Long lastUpdateDate, NodeTypeEnum nodeType) {
		if (componentId != null) { // In case of error, the componentId might be
									// empty.
			if (nodeType == NodeTypeEnum.Resource || nodeType == NodeTypeEnum.Service || nodeType == NodeTypeEnum.Product) {
				// add task to Q
				log.debug("Going to add component {} of type {} to cache", componentId, nodeType.name().toLowerCase());
				cacheManagerOperation.updateComponentInCache(componentId, lastUpdateDate, nodeType);
			}
		}
		return Either.right(StorageOperationStatus.OPERATION_NOT_SUPPORTED);
	}

	@Override
	public StorageOperationStatus lockComponentByName(String name, NodeTypeEnum nodeType) {
		log.info("lock resource with name {}", name);
		TitanOperationStatus lockElementStatus = null;
		try {

			lockElementStatus = titanGenericDao.lockElement(name, nodeType);

		} catch (Exception e) {
			lockElementStatus = TitanOperationStatus.ALREADY_LOCKED;

		}

		return DaoStatusConverter.convertTitanStatusToStorageStatus(lockElementStatus);

	}
}