2 * ============LICENSE_START=======================================================
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
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.model.cache.jobs;
23 import fj.data.Either;
24 import org.apache.commons.lang3.tuple.ImmutablePair;
25 import org.openecomp.sdc.be.dao.api.ActionStatus;
26 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
27 import org.openecomp.sdc.be.model.Component;
28 import org.openecomp.sdc.be.model.cache.DaoInfo;
29 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
30 import org.openecomp.sdc.be.resources.data.ComponentMetadataData;
31 import org.openecomp.sdc.common.log.wrappers.Logger;
33 import java.util.function.Function;
36 * Created by mlando on 9/7/2016.
38 public class CheckAndUpdateJob extends Job {
39 private static final Logger log = Logger.getLogger(CheckAndUpdateJob.class.getName());
41 public CheckAndUpdateJob(DaoInfo daoInfo, String componentId, NodeTypeEnum nodeTypeEnum, long timestamp) {
42 super(daoInfo, componentId, nodeTypeEnum, timestamp);
46 public Object doWork() {
47 log.trace("starting work on job.");
48 log.trace("update cache for componentId:{} of nodeTypeEnum:{} with timestamp:{}.", componentId, nodeTypeEnum,
54 Either<ImmutablePair<Component, Long>, ActionStatus> cacheResult = daoInfo.getComponentCache()
55 .getComponentAndTime(componentId, Function.identity());
56 // if error while getting from cache abort and update
57 if (cacheResult.isRight()) {
59 if (!ActionStatus.RESOURCE_NOT_FOUND.equals(cacheResult.right().value())
60 && !ActionStatus.INVALID_CONTENT.equals(cacheResult.right().value())) {
61 log.debug("failed to get component:{} from cache error:{}", componentId,
62 cacheResult.right().value());
65 // component not in cache put there
67 return updateCache(componentId, nodeTypeEnum, timestamp);
70 ImmutablePair<Component, Long> recored = cacheResult.left().value();
71 // the cache has allready been updated exit
72 if (this.timestamp < recored.getRight()) {
73 log.debug("job timestemp:{} is smaller then the cache timestamp:{} no update is needed.",
74 this.timestamp, recored.getRight());
77 return updateCache(componentId, nodeTypeEnum, timestamp);
79 } catch (Exception e) {
80 log.debug("an exception was encountered during CheckAndUpdateJob", e);
82 daoInfo.getToscaOperationFacade().commit();
92 private boolean updateCache(String componentId, NodeTypeEnum nodeTypeEnum, Long timestamp) {
93 // get component from cache
94 Either<ComponentMetadataData, StorageOperationStatus> metaDataRes = getComponentMetaData(componentId,
96 if (metaDataRes.isRight()) {
99 ComponentMetadataData metaData = metaDataRes.left().value();
100 // the job time is older then the one on graph nothing to do there is a
101 // job that will handle this.
102 Long graphTimestamp = metaData.getMetadataDataDefinition().getLastUpdateDate();
103 if (timestamp < graphTimestamp) {
105 "the job timestamp:{} is smaller then the graph timestamp:{}. exiting because another job will update the cache.",
106 timestamp, graphTimestamp);
110 // get component from grath
111 Either<Component, StorageOperationStatus> componentRes = daoInfo.getToscaOperationFacade().getToscaElement(componentId);
112 if (componentRes.isRight()) {
113 log.debug("failed to get full component:{} from graph status:{}", componentId,
114 componentRes.right().value());
117 Component component = componentRes.left().value();
119 if (!this.daoInfo.getComponentCache().setComponent(component, nodeTypeEnum)) {
120 log.debug("failed to store componentId:{} nodeTypeEnum:", componentId, nodeTypeEnum);
124 log.debug("cache successfully updated for componentId:{} nodeTypeEnum:{} timestemp:{}.", componentId,
125 nodeTypeEnum, timestamp);