re base code
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / cache / jobs / CheckAndUpdateJob.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
21 package org.openecomp.sdc.be.model.cache.jobs;
22
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;
32
33 import java.util.function.Function;
34
35 /**
36  * Created by mlando on 9/7/2016.
37  */
38 public class CheckAndUpdateJob extends Job {
39     private static final Logger log = Logger.getLogger(CheckAndUpdateJob.class.getName());
40
41     public CheckAndUpdateJob(DaoInfo daoInfo, String componentId, NodeTypeEnum nodeTypeEnum, long timestamp) {
42         super(daoInfo, componentId, nodeTypeEnum, timestamp);
43     }
44
45     @Override
46     public Object doWork() {
47         log.trace("starting work on job.");
48         log.trace("update cache for componentId:{} of nodeTypeEnum:{} with timestamp:{}.", componentId, nodeTypeEnum,
49                 timestamp);
50
51         try {
52
53             // get from cache
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()) {
58                 // genral error
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());
63                     return false;
64                 }
65                 // component not in cache put there
66                 else {
67                     return updateCache(componentId, nodeTypeEnum, timestamp);
68                 }
69             }
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());
75                 return false;
76             }
77             return updateCache(componentId, nodeTypeEnum, timestamp);
78
79         } catch (Exception e) {
80             log.debug("an exception was encountered during CheckAndUpdateJob", e);
81         } finally {
82             daoInfo.getToscaOperationFacade().commit();
83         }
84         return false;
85     }
86
87     /**
88      * @param componentId
89      * @param nodeTypeEnum
90      * @return
91      */
92     private boolean updateCache(String componentId, NodeTypeEnum nodeTypeEnum, Long timestamp) {
93         // get component from cache
94         Either<ComponentMetadataData, StorageOperationStatus> metaDataRes = getComponentMetaData(componentId,
95                 nodeTypeEnum);
96         if (metaDataRes.isRight()) {
97             return false;
98         }
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) {
104             log.debug(
105                     "the job timestamp:{} is smaller then the graph timestamp:{}. exiting because another job will update the cache.",
106                     timestamp, graphTimestamp);
107             return false;
108         } else {
109             // update cache
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());
115                 return false;
116             }
117             Component component = componentRes.left().value();
118             // store in cache
119             if (!this.daoInfo.getComponentCache().setComponent(component, nodeTypeEnum)) {
120                 log.debug("failed to store componentId:{} nodeTypeEnum:", componentId, nodeTypeEnum);
121                 return false;
122             }
123         }
124         log.debug("cache successfully updated for componentId:{} nodeTypeEnum:{} timestemp:{}.", componentId,
125                 nodeTypeEnum, timestamp);
126         return true;
127     }
128
129 }