2 * ============LICENSE_START=======================================================
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
24 package org.onap.so.adapters.requestsdb;
26 import java.util.ArrayList;
27 import java.util.Calendar;
28 import java.util.Date;
29 import java.util.List;
30 import org.onap.so.db.request.beans.ArchivedInfraRequests;
31 import org.onap.so.db.request.beans.InfraActiveRequests;
32 import org.onap.so.db.request.data.repository.ArchivedInfraRequestsRepository;
33 import org.onap.so.db.request.data.repository.InfraActiveRequestsRepository;
34 import org.onap.logging.filter.base.ErrorCode;
35 import org.onap.logging.filter.base.ScheduledLogging;
36 import org.onap.logging.filter.base.ScheduledTaskException;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.beans.factory.annotation.Value;
41 import org.springframework.data.domain.PageRequest;
42 import org.springframework.scheduling.annotation.Scheduled;
43 import org.springframework.stereotype.Component;
44 import net.javacrumbs.shedlock.core.SchedulerLock;
47 public class ArchiveInfraRequestsScheduler {
49 private static Logger logger = LoggerFactory.getLogger(ArchiveInfraRequestsScheduler.class);
52 private InfraActiveRequestsRepository infraActiveRepo;
54 private ArchivedInfraRequestsRepository archivedInfraRepo;
57 @Value("${mso.infra-requests.archived.period}")
58 private int archivedPeriod;
61 * Runs the scheduler nightly [Seconds] [Minutes] [Hours] [Day of month] [Month] [Day of week] [Year]
63 * @throws ScheduledTaskException
66 @Scheduled(cron = "0 0 1 * * ?")
67 @SchedulerLock(name = "archiveInfraRequestsScheduler")
68 public void infraRequestsScheduledTask() throws ScheduledTaskException {
69 logger.debug("Start of archiveInfraRequestsScheduler");
71 Date currentDate = new Date();
72 Calendar calendar = Calendar.getInstance();
73 calendar.setTime(currentDate);
74 calendar.add(Calendar.DATE, -archivedPeriod);
75 Date archivingDate = calendar.getTime();
77 logger.debug("Date before 6 months: " + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.DATE)
78 + "-" + calendar.get(Calendar.YEAR));
80 List<InfraActiveRequests> requestsByEndTime = new ArrayList<>();
82 PageRequest pageRequest = PageRequest.of(0, 100); // Could use sorting here
85 requestsByEndTime = infraActiveRepo.findByEndTimeLessThan(archivingDate, pageRequest);
86 logger.debug("{} requests to be archived based on End Time", requestsByEndTime.size());
87 archiveInfraRequests(requestsByEndTime);
88 } while (!requestsByEndTime.isEmpty());
90 List<InfraActiveRequests> requestsByStartTime = new ArrayList<>();
92 requestsByStartTime = infraActiveRepo.findByStartTimeLessThanAndEndTime(archivingDate, null, pageRequest);
93 logger.debug("{} requests to be archived based on Start Time", requestsByEndTime.size());
94 archiveInfraRequests(requestsByStartTime);
95 } while (!requestsByStartTime.isEmpty());
97 logger.debug("End of archiveInfraRequestsScheduler");
100 protected void archiveInfraRequests(List<InfraActiveRequests> requests) throws ScheduledTaskException {
101 List<ArchivedInfraRequests> newArchivedReqs = new ArrayList<>();
102 List<InfraActiveRequests> oldInfraReqs = new ArrayList<>();
104 for (InfraActiveRequests iar : requests) {
105 ArchivedInfraRequests archivedInfra = new ArchivedInfraRequests();
107 archivedInfra.setCloudRegion(iar.getCloudRegion());
108 archivedInfra.setCallBackUrl(iar.getCallBackUrl());
109 archivedInfra.setConfigurationId(iar.getConfigurationId());
110 archivedInfra.setConfigurationName(iar.getConfigurationName());
111 archivedInfra.setCorrelator(iar.getCorrelator());
112 archivedInfra.setEndTime(iar.getEndTime());
113 archivedInfra.setLastModifiedBy(iar.getLastModifiedBy());
114 archivedInfra.setNetworkId(iar.getNetworkId());
115 archivedInfra.setNetworkName(iar.getNetworkName());
116 archivedInfra.setNetworkType(iar.getNetworkType());
117 archivedInfra.setOperationalEnvId(iar.getOperationalEnvId());
118 archivedInfra.setOperationalEnvName(iar.getOperationalEnvName());
119 archivedInfra.setRequestUrl(iar.getRequestUrl());
120 archivedInfra.setProgress(iar.getProgress());
121 archivedInfra.setRequestAction(iar.getRequestAction());
122 archivedInfra.setRequestBody(iar.getRequestBody());
123 archivedInfra.setRequestId(iar.getRequestId());
124 archivedInfra.setRequestorId(iar.getRequestorId());
125 archivedInfra.setRequestScope(iar.getRequestScope());
126 archivedInfra.setRequestStatus(iar.getRequestStatus());
127 archivedInfra.setResponseBody(iar.getResponseBody());
128 archivedInfra.setServiceInstanceId(iar.getServiceInstanceId());
129 archivedInfra.setServiceInstanceName(iar.getServiceInstanceName());
130 archivedInfra.setServiceType(iar.getServiceType());
131 archivedInfra.setSource(iar.getSource());
132 archivedInfra.setStartTime(iar.getStartTime());
133 archivedInfra.setStatusMessage(iar.getStatusMessage());
134 archivedInfra.setTenantId(iar.getTenantId());
135 archivedInfra.setVfModuleId(iar.getVfModuleId());
136 archivedInfra.setVfModuleModelName(iar.getVfModuleModelName());
137 archivedInfra.setVfModuleName(iar.getVfModuleName());
138 archivedInfra.setVnfId(iar.getVnfId());
139 archivedInfra.setVnfName(iar.getVnfName());
140 archivedInfra.setVnfOutputs(iar.getVnfOutputs());
141 archivedInfra.setVnfParams(iar.getVnfParams());
142 archivedInfra.setVnfType(iar.getVnfType());
143 archivedInfra.setVolumeGroupId(iar.getVolumeGroupId());
144 archivedInfra.setVolumeGroupName(iar.getVolumeGroupName());
145 archivedInfra.setProductFamilyName(iar.getProductFamilyName());
146 archivedInfra.setTenantName(iar.getTenantName());
147 archivedInfra.setResourceStatusMessage(iar.getResourceStatusMessage());
148 archivedInfra.setWorkflowName(iar.getWorkflowName());
149 archivedInfra.setOperationName(iar.getOperationName());
151 newArchivedReqs.add(archivedInfra);
152 oldInfraReqs.add(iar);
153 } catch (Exception e) {
154 throw new ScheduledTaskException(ErrorCode.UnknownError, e.getMessage(), e);
158 logger.info("Creating archivedInfraRequest records: {}", newArchivedReqs.size());
159 archivedInfraRepo.saveAll(newArchivedReqs);
161 logger.info("Deleting InfraActiveRequest records: {}", oldInfraReqs.size());
162 infraActiveRepo.deleteAll(oldInfraReqs);