Aharoni, Pavel (pa0916) | 8c70f07 | 2018-11-18 00:07:12 +0200 | [diff] [blame] | 1 | /*-
|
| 2 | * ============LICENSE_START=======================================================
|
| 3 | * OSAM
|
| 4 | * ================================================================================
|
| 5 | * Copyright (C) 2018 AT&T
|
| 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 | package org.onap.osam.job.command;
|
| 21 |
|
| 22 | import com.google.common.collect.ImmutableMap;
|
| 23 | import lombok.extern.slf4j.Slf4j;
|
| 24 | import org.onap.osam.job.dao.job.JobStatus;
|
| 25 | import org.onap.osam.job.IJobFactory;
|
| 26 | import org.onap.osam.job.IJobCommand;
|
| 27 | import org.onap.osam.job.IJobsDataAccessService;
|
| 28 | import org.onap.osam.job.NextCommand;
|
| 29 | import org.onap.osam.job.AsyncJobService;
|
| 30 | import org.onap.osam.job.impl.JobSharedData;
|
| 31 | import org.onap.osam.job.utils.TimeUtils;
|
| 32 |
|
| 33 | import java.time.ZonedDateTime;
|
| 34 | import java.time.format.DateTimeParseException;
|
| 35 | import java.util.Map;
|
| 36 |
|
| 37 | @Slf4j
|
| 38 | public abstract class BaseInProgressStatusCommand extends CommandBase implements IJobCommand {
|
| 39 |
|
| 40 | protected AsyncJobService asyncInstantiationBL;
|
| 41 |
|
| 42 | protected IJobsDataAccessService jobsDataAccessService;
|
| 43 |
|
| 44 | protected IJobFactory jobAdapter;
|
| 45 |
|
| 46 | /*
|
| 47 | @Inject
|
| 48 | protected RestMsoImplementation restMso;
|
| 49 |
|
| 50 | @Inject
|
| 51 | protected AuditService auditService;
|
| 52 | */
|
| 53 |
|
| 54 | protected String requestId;
|
| 55 |
|
| 56 | protected String instanceId;
|
| 57 |
|
| 58 |
|
| 59 | @Override
|
| 60 | public NextCommand call() {
|
| 61 |
|
| 62 | // try {
|
| 63 | String asyncRequestStatus = getAsyncRequestStatus();
|
| 64 | //asyncInstantiationBL.auditMsoStatus(getSharedData().getRootJobId(), asyncRequestStatus.get().request);
|
| 65 | JobStatus jobStatus = asyncInstantiationBL.calcStatus(asyncRequestStatus);
|
| 66 | ZonedDateTime jobStartTime = getZonedDateTime(asyncRequestStatus);
|
| 67 | jobStatus = isExpired(jobStartTime) ? JobStatus.FAILED : jobStatus;
|
| 68 | return processJobStatus(jobStatus);
|
| 69 |
|
| 70 | //TODO error handling
|
| 71 | /* } catch (javax.ws.rs.ProcessingException e) {
|
| 72 | // Retry when we can't connect MSO during getStatus
|
| 73 | LOGGER.error(EELFLoggerDelegate.errorLogger, "Cannot get orchestration status for {}, will retry: {}", requestId, e, e);
|
| 74 | return new NextCommand(JobStatus.IN_PROGRESS, this);
|
| 75 | } catch (BadResponseFromMso e) {
|
| 76 | return handleFailedMsoResponse(e.getMsoResponse());
|
| 77 | }
|
| 78 | catch (RuntimeException e) {
|
| 79 | LOGGER.error(EELFLoggerDelegate.errorLogger, "Cannot get orchestration status for {}, stopping: {}", requestId, e, e);
|
| 80 | return new NextCommand(JobStatus.STOPPED, this);
|
| 81 | }*/
|
| 82 | }
|
| 83 |
|
| 84 | abstract NextCommand processJobStatus(JobStatus jobStatus);
|
| 85 |
|
| 86 | abstract boolean isExpired(ZonedDateTime jobStartTime);
|
| 87 |
|
| 88 | private String getAsyncRequestStatus() {
|
| 89 | /*
|
| 90 | String path = asyncInstantiationBL.getOrchestrationRequestsPath()+"/"+requestId;
|
| 91 | RestObject<AsyncRequestStatus> msoResponse = restMso.GetForObject(path, AsyncRequestStatus.class);
|
| 92 | if (msoResponse.getStatusCode() >= 400 || msoResponse.get() == null) {
|
| 93 | throw new BadResponseFromMso(msoResponse);
|
| 94 | }
|
| 95 | */
|
| 96 | //TODO
|
| 97 | return "dummy";
|
| 98 | }
|
| 99 |
|
| 100 | /*private NextCommand handleFailedMsoResponse(RestObject<AsyncRequestStatus> msoResponse) {
|
| 101 | auditService.setFailedAuditStatusFromMso(getSharedData().getJobUuid(), requestId, msoResponse.getStatusCode(), msoResponse.getRaw());
|
| 102 | LOGGER.error(EELFLoggerDelegate.errorLogger,
|
| 103 | "Failed to get orchestration status for {}. Status code: {}, Body: {}",
|
| 104 | requestId, msoResponse.getStatusCode(), msoResponse.getRaw());
|
| 105 | return new NextCommand(JobStatus.IN_PROGRESS, this);
|
| 106 | }*/
|
| 107 |
|
| 108 | @Override
|
| 109 | public BaseInProgressStatusCommand init(JobSharedData sharedData, Map<String, Object> commandData) {
|
| 110 | return init(sharedData, (String) commandData.get("requestId"), (String) commandData.get("instanceId"));
|
| 111 | }
|
| 112 |
|
| 113 |
|
| 114 | protected BaseInProgressStatusCommand init(JobSharedData sharedData,
|
| 115 | String requestId,
|
| 116 | String instanceId) {
|
| 117 | init(sharedData);
|
| 118 | this.requestId = requestId;
|
| 119 | this.instanceId = instanceId;
|
| 120 | return this;
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | public Map<String, Object> getData() {
|
| 125 | return ImmutableMap.of(
|
| 126 | "requestId", requestId,
|
| 127 | "instanceId", instanceId
|
| 128 | );
|
| 129 | }
|
| 130 |
|
| 131 | private ZonedDateTime getZonedDateTime(String response) {
|
| 132 | ZonedDateTime jobStartTime;
|
| 133 | try {
|
| 134 | //TODO dummy time until real impl is provided
|
| 135 | jobStartTime = TimeUtils.parseZonedDateTime(ZonedDateTime.now().toString());
|
| 136 | } catch (DateTimeParseException | NullPointerException e) {
|
| 137 | log.error("Failed to parse start time for {}, body: {}. Current time will be used", requestId, response, e);
|
| 138 | jobStartTime = ZonedDateTime.now();
|
| 139 | }
|
| 140 | return jobStartTime;
|
| 141 | }
|
| 142 | }
|