re base code
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / cache / jobs / Job.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.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
25 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
26 import org.openecomp.sdc.be.model.Component;
27 import org.openecomp.sdc.be.model.cache.DaoInfo;
28 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
29 import org.openecomp.sdc.be.resources.data.ComponentMetadataData;
30 import org.openecomp.sdc.common.log.wrappers.Logger;
31
32 public abstract class Job<E> {
33     private static final Logger log = Logger.getLogger(Job.class.getName());
34     protected DaoInfo daoInfo;
35     protected String componentId;
36     protected long timestamp;
37     protected NodeTypeEnum nodeTypeEnum;
38
39     protected Job(DaoInfo daoInfo, String componentId, NodeTypeEnum nodeTypeEnum, long timestamp) {
40         this.daoInfo = daoInfo;
41         this.componentId = componentId;
42         this.timestamp = timestamp;
43         this.nodeTypeEnum = nodeTypeEnum;
44     }
45
46     protected Job(DaoInfo daoInfo, Component component, NodeTypeEnum nodeTypeEnum) {
47         this.daoInfo = daoInfo;
48         this.componentId = component.getUniqueId();
49         this.timestamp = component.getLastUpdateDate();
50         this.nodeTypeEnum = nodeTypeEnum;
51     }
52
53     public abstract E doWork();
54
55     protected Either<ComponentMetadataData, StorageOperationStatus> getComponentMetaData(String componentId,
56             NodeTypeEnum nodeTypeEnum) {
57         Either<ComponentMetadataData, StorageOperationStatus> metaDataRes = daoInfo.getToscaOperationFacade().getComponentMetadata(componentId);
58         if (metaDataRes.isRight()) {
59             // in case we cant find the component on graph exit
60             if (StorageOperationStatus.NOT_FOUND.equals(metaDataRes.right().value())) {
61                 log.debug("failed to locate component:{} on graph status:{}", componentId, metaDataRes.right().value());
62             } else {
63                 log.debug("failed to get component:{} from graph status:{}", componentId, metaDataRes.right().value());
64             }
65         }
66         return metaDataRes;
67     }
68
69     protected NodeTypeEnum getNodeTypeFromComponentType(ComponentTypeEnum type) {
70         NodeTypeEnum result = null;
71         switch (type) {
72         case PRODUCT:
73             result = NodeTypeEnum.Product;
74             break;
75         case RESOURCE:
76             result = NodeTypeEnum.Resource;
77             break;
78         case SERVICE:
79             result = NodeTypeEnum.Service;
80             break;
81         default:
82
83         }
84         return result;
85
86     }
87 }