blob: 1e03d4f767828941bc4aee42d1c647cd004722dc [file] [log] [blame]
Aharoni, Pavel (pa0916)ca3cb012018-10-22 15:29:57 +03001/*-
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
21
22package org.onap.osam.job.command;
23
24import com.fasterxml.jackson.databind.ObjectMapper;
25import com.google.common.collect.ImmutableMap;
26import io.joshworks.restclient.http.HttpResponse;
27import org.onap.osam.services.IAsyncInstantiationBusinessLogic;
28import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
29import org.onap.osam.aai.exceptions.InvalidAAIResponseException;
30import org.onap.osam.exceptions.MaxRetriesException;
31import org.onap.osam.job.Job;
32import org.onap.osam.job.JobCommand;
33import org.onap.osam.job.NextCommand;
34import org.onap.osam.model.RequestReferencesContainer;
35import org.onap.osam.model.serviceInstantiation.ServiceInstantiation;
36import org.onap.osam.mso.MsoInterface;
37import org.onap.osam.mso.rest.RequestDetailsWrapper;
38import org.onap.osam.services.IAuditService;
39import org.springframework.beans.factory.config.ConfigurableBeanFactory;
40import org.springframework.context.annotation.Scope;
41import org.springframework.stereotype.Component;
42
43import javax.inject.Inject;
44import java.util.Map;
45import java.util.UUID;
46
47
48@Component
49@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
50public class ServiceInstantiationCommand implements JobCommand {
51
52 private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
53
54 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(ServiceInstantiationCommand.class);
55
56 @Inject
57 private IAsyncInstantiationBusinessLogic asyncInstantiationBL;
58
59 @Inject
60 private IAuditService auditService;
61
62 @Inject
63 private MsoInterface restMso;
64
65 private UUID uuid;
66 private ServiceInstantiation serviceInstantiationRequest;
67 private String userId;
68
69 public ServiceInstantiationCommand() {
70 }
71
72 public ServiceInstantiationCommand(UUID uuid, ServiceInstantiation serviceInstantiationRequest, String userId) {
73 init(uuid, serviceInstantiationRequest, userId);
74 }
75
76 @Override
77 public NextCommand call() {
78 RequestDetailsWrapper requestDetailsWrapper = new RequestDetailsWrapper();
79 try {
80 /*requestDetailsWrapper = asyncInstantiationBL.generateServiceInstantiationRequest(
81 uuid, serviceInstantiationRequest, userId
82 );*/
83 }
84
85 //Aai return bad response while checking names uniqueness
86 catch (InvalidAAIResponseException exception) {
87 LOGGER.error("Failed to check name uniqueness in AAI. VID will try again later", exception);
88 //put the job in_progress so we will keep trying to check name uniqueness in AAI
89 //And then send the request to MSO
90 return new NextCommand(Job.JobStatus.IN_PROGRESS, this);
91 }
92
93 //Vid reached to max retries while trying to find unique name in AAI
94 catch (MaxRetriesException exception) {
95 LOGGER.error("Failed to find unused name in AAI. Set the job to FAILED ", exception);
96 return handleCommandFailed();
97 }
98
99 String path = asyncInstantiationBL.getServiceInstantiationPath(serviceInstantiationRequest);
100
101 HttpResponse<RequestReferencesContainer> msoResponse = restMso.post(path,
102 requestDetailsWrapper, RequestReferencesContainer.class);
103
104
105 if (msoResponse.getStatus() >= 200 && msoResponse.getStatus() < 400) {
106 final Job.JobStatus jobStatus = Job.JobStatus.IN_PROGRESS;
107 final String requestId = msoResponse.getBody().getRequestReferences().getRequestId();
108 final String instanceId = msoResponse.getBody().getRequestReferences().getInstanceId();
109 asyncInstantiationBL.auditVidStatus(uuid, jobStatus);
110 setInitialRequestAuditStatusFromMso(requestId);
111 asyncInstantiationBL.updateServiceInfo(uuid, x-> {
112 x.setJobStatus(jobStatus);
113 x.setServiceInstanceId(instanceId);
114 });
115
116 return new NextCommand(jobStatus, new InProgressStatusCommand(uuid, requestId));
117 } else {
118 auditService.setFailedAuditStatusFromMso(uuid,null, msoResponse.getStatus(),
119 msoResponse.getBody().toString());
120 return handleCommandFailed();
121 }
122
123 }
124
125 private void setInitialRequestAuditStatusFromMso(String requestId){
126 final String initialMsoRequestStatus = "REQUESTED";
127 asyncInstantiationBL.auditMsoStatus(uuid,initialMsoRequestStatus,requestId,null);
128 }
129
130 protected NextCommand handleCommandFailed() {
131 asyncInstantiationBL.handleFailedInstantiation(uuid);
132 return new NextCommand(Job.JobStatus.FAILED);
133 }
134
135 @Override
136 public ServiceInstantiationCommand init(UUID jobUuid, Map<String, Object> data) {
137 final Object request = data.get("request");
138
139 return init(
140 jobUuid,
141 OBJECT_MAPPER.convertValue(request, ServiceInstantiation.class),
142 (String) data.get("userId")
143 );
144 }
145
146 private ServiceInstantiationCommand init(UUID jobUuid, ServiceInstantiation serviceInstantiationRequest, String userId) {
147 this.uuid = jobUuid;
148 this.serviceInstantiationRequest = serviceInstantiationRequest;
149 this.userId = userId;
150
151 return this;
152 }
153
154 @Override
155 public Map<String, Object> getData() {
156 return ImmutableMap.of(
157 "uuid", uuid,
158 "request", serviceInstantiationRequest,
159 "userId", userId
160 );
161 }
162}