24dab2fc5b7937cb16274f43244909295f00c84d
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2018 IBM.
8  * Modifications Copyright (c) 2019 Samsung
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  * 
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.so.adapters.requestsdb;
25
26 import java.util.ArrayList;
27 import java.util.Calendar;
28 import java.util.Date;
29 import java.util.List;
30 import org.onap.logging.filter.base.ONAPComponents;
31 import org.onap.logging.ref.slf4j.ONAPLogConstants;
32 import org.onap.so.db.request.beans.ArchivedInfraRequests;
33 import org.onap.so.db.request.beans.InfraActiveRequests;
34 import org.onap.so.db.request.data.repository.ArchivedInfraRequestsRepository;
35 import org.onap.so.db.request.data.repository.InfraActiveRequestsRepository;
36 import org.onap.so.logger.ErrorCode;
37 import org.onap.so.logger.LoggingAnchor;
38 import org.onap.so.logger.MessageEnum;
39 import org.onap.so.logger.ScheduledTasksMDCSetup;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.slf4j.MDC;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.beans.factory.annotation.Value;
45 import org.springframework.data.domain.PageRequest;
46 import org.springframework.scheduling.annotation.Scheduled;
47 import org.springframework.stereotype.Component;
48 import net.javacrumbs.shedlock.core.SchedulerLock;
49
50 @Component
51 public class ArchiveInfraRequestsScheduler {
52
53     private static Logger logger = LoggerFactory.getLogger(ArchiveInfraRequestsScheduler.class);
54
55     @Autowired
56     private InfraActiveRequestsRepository infraActiveRepo;
57     @Autowired
58     private ArchivedInfraRequestsRepository archivedInfraRepo;
59     @Autowired
60     private ScheduledTasksMDCSetup scheduledMDCSetup;
61
62
63     @Value("${mso.infra-requests.archived.period}")
64     private int archivedPeriod;
65
66     /**
67      * Runs the scheduler nightly [Seconds] [Minutes] [Hours] [Day of month] [Month] [Day of week] [Year]
68      */
69     @Scheduled(cron = "0 0 1 * * ?")
70     @SchedulerLock(name = "archiveInfraRequestsScheduler")
71     public void infraRequestsScheduledTask() {
72         scheduledMDCSetup.mdcSetup(ONAPComponents.REQUEST_DB, "infraRequestsScheduledTask");
73         logger.debug("Start of archiveInfraRequestsScheduler");
74
75         Date currentDate = new Date();
76         Calendar calendar = Calendar.getInstance();
77         calendar.setTime(currentDate);
78         calendar.add(Calendar.DATE, -archivedPeriod);
79         Date archivingDate = calendar.getTime();
80
81         logger.debug("Date before 6 months: " + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.DATE)
82                 + "-" + calendar.get(Calendar.YEAR));
83
84         List<InfraActiveRequests> requestsByEndTime = new ArrayList<>();
85         PageRequest pageRequest = new PageRequest(0, 100);
86         do {
87             requestsByEndTime = infraActiveRepo.findByEndTimeLessThan(archivingDate, pageRequest);
88             logger.debug("{} requests to be archived based on End Time", requestsByEndTime.size());
89             archiveInfraRequests(requestsByEndTime);
90         } while (!requestsByEndTime.isEmpty());
91
92         List<InfraActiveRequests> requestsByStartTime = new ArrayList<>();
93         do {
94             requestsByStartTime = infraActiveRepo.findByStartTimeLessThanAndEndTime(archivingDate, null, pageRequest);
95             logger.debug("{} requests to be archived based on Start Time", requestsByEndTime.size());
96             archiveInfraRequests(requestsByStartTime);
97         } while (!requestsByStartTime.isEmpty());
98
99         logger.debug("End of archiveInfraRequestsScheduler");
100         scheduledMDCSetup.exitAndClearMDC();
101     }
102
103     protected void archiveInfraRequests(List<InfraActiveRequests> requests) {
104         List<ArchivedInfraRequests> newArchivedReqs = new ArrayList<>();
105         List<InfraActiveRequests> oldInfraReqs = new ArrayList<>();
106
107         for (InfraActiveRequests iar : requests) {
108             ArchivedInfraRequests archivedInfra = new ArchivedInfraRequests();
109             try {
110                 archivedInfra.setCloudRegion(iar.getCloudRegion());
111                 archivedInfra.setCallBackUrl(iar.getCallBackUrl());
112                 archivedInfra.setConfigurationId(iar.getConfigurationId());
113                 archivedInfra.setConfigurationName(iar.getConfigurationName());
114                 archivedInfra.setCorrelator(iar.getCorrelator());
115                 archivedInfra.setEndTime(iar.getEndTime());
116                 archivedInfra.setLastModifiedBy(iar.getLastModifiedBy());
117                 archivedInfra.setNetworkId(iar.getNetworkId());
118                 archivedInfra.setNetworkName(iar.getNetworkName());
119                 archivedInfra.setNetworkType(iar.getNetworkType());
120                 archivedInfra.setOperationalEnvId(iar.getOperationalEnvId());
121                 archivedInfra.setOperationalEnvName(iar.getOperationalEnvName());
122                 archivedInfra.setRequestUrl(iar.getRequestUrl());
123                 archivedInfra.setProgress(iar.getProgress());
124                 archivedInfra.setRequestAction(iar.getRequestAction());
125                 archivedInfra.setRequestBody(iar.getRequestBody());
126                 archivedInfra.setRequestId(iar.getRequestId());
127                 archivedInfra.setRequestorId(iar.getRequestorId());
128                 archivedInfra.setRequestScope(iar.getRequestScope());
129                 archivedInfra.setRequestStatus(iar.getRequestStatus());
130                 archivedInfra.setResponseBody(iar.getResponseBody());
131                 archivedInfra.setServiceInstanceId(iar.getServiceInstanceId());
132                 archivedInfra.setServiceInstanceName(iar.getServiceInstanceName());
133                 archivedInfra.setServiceType(iar.getServiceType());
134                 archivedInfra.setSource(iar.getSource());
135                 archivedInfra.setStartTime(iar.getStartTime());
136                 archivedInfra.setStatusMessage(iar.getStatusMessage());
137                 archivedInfra.setTenantId(iar.getTenantId());
138                 archivedInfra.setVfModuleId(iar.getVfModuleId());
139                 archivedInfra.setVfModuleModelName(iar.getVfModuleModelName());
140                 archivedInfra.setVfModuleName(iar.getVfModuleName());
141                 archivedInfra.setVnfId(iar.getVnfId());
142                 archivedInfra.setVnfName(iar.getVnfName());
143                 archivedInfra.setVnfOutputs(iar.getVnfOutputs());
144                 archivedInfra.setVnfParams(iar.getVnfParams());
145                 archivedInfra.setVnfType(iar.getVnfType());
146                 archivedInfra.setVolumeGroupId(iar.getVolumeGroupId());
147                 archivedInfra.setVolumeGroupName(iar.getVolumeGroupName());
148                 archivedInfra.setProductFamilyName(iar.getProductFamilyName());
149                 archivedInfra.setTenantName(iar.getTenantName());
150
151                 newArchivedReqs.add(archivedInfra);
152                 oldInfraReqs.add(iar);
153             } catch (Exception e) {
154                 scheduledMDCSetup.errorMDCSetup(ErrorCode.UnknownError, e.getMessage());
155                 MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.ERROR.toString());
156                 logger.error(LoggingAnchor.TWO, MessageEnum.RA_GENERAL_EXCEPTION.toString(),
157                         ErrorCode.UnknownError.getValue(), e);
158             }
159         }
160
161         logger.info("Creating archivedInfraRequest records: {}", newArchivedReqs.size());
162         archivedInfraRepo.saveAll(newArchivedReqs);
163
164         logger.info("Deleting InfraActiveRequest records: {}", oldInfraReqs.size());
165         infraActiveRepo.deleteAll(oldInfraReqs);
166     }
167 }