36e7b7ced6b7e9aa666c5631a9db644f4837a747
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 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.onap.so.apihandlerinfra.infra.rest.handler;
22
23
24 import java.sql.Timestamp;
25 import java.util.HashMap;
26 import org.onap.so.apihandler.common.RequestClientParameter;
27 import org.onap.so.apihandlerinfra.Action;
28 import org.onap.so.apihandlerinfra.Constants;
29 import org.onap.so.apihandlerinfra.infra.rest.exception.NoRecipeException;
30 import org.onap.so.apihandlerinfra.infra.rest.exception.RequestConflictedException;
31 import org.onap.so.constants.Status;
32 import org.onap.so.db.catalog.beans.Recipe;
33 import org.onap.so.db.catalog.beans.ServiceRecipe;
34 import org.onap.so.db.request.beans.InfraActiveRequests;
35 import org.onap.so.logger.LogConstants;
36 import org.onap.so.serviceinstancebeans.ModelType;
37 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.slf4j.MDC;
41 import org.springframework.stereotype.Component;
42 import com.fasterxml.jackson.core.JsonProcessingException;
43 import com.fasterxml.jackson.databind.ObjectMapper;
44
45 @Component
46 public class ServiceInstanceRestHandler extends AbstractRestHandler {
47
48     private static final String DEFAULT_VF_MODULE_UUID = "d88da85c-d9e8-4f73-b837-3a72a431622b";
49
50     private static final Logger logger = LoggerFactory.getLogger(ServiceInstanceRestHandler.class);
51
52     public InfraActiveRequests mapInfraActiveRequestForDelete(String requestId, String serviceInstanceId,
53             String requestorId, String source, String requestURL) {
54         Timestamp startTimeStamp = new Timestamp(System.currentTimeMillis());
55         InfraActiveRequests deleteRequest = new InfraActiveRequests();
56         deleteRequest.setRequestAction(Action.deleteInstance.toString());
57         deleteRequest.setStartTime(startTimeStamp);
58         deleteRequest.setServiceInstanceId(serviceInstanceId);
59         deleteRequest.setRequestId(requestId);
60         deleteRequest.setRequestUrl(MDC.get(LogConstants.HTTP_URL));
61         deleteRequest.setRequestorId(requestorId);
62         deleteRequest.setSource(source);
63         deleteRequest.setRequestStatus(Status.IN_PROGRESS.toString());
64         deleteRequest.setLastModifiedBy(Constants.MODIFIED_BY_APIHANDLER);
65         deleteRequest.setRequestUrl(requestURL);
66         deleteRequest.setRequestScope(ModelType.service.toString());
67         return deleteRequest;
68     }
69
70     public InfraActiveRequests createInfraActiveRequestForDelete(String requestId, String serviceInstanceId,
71             String requestorId, String source, String requestURL) {
72         InfraActiveRequests request =
73                 mapInfraActiveRequestForDelete(requestId, serviceInstanceId, requestorId, source, requestURL);
74         infraActiveRequestsClient.save(request);
75         return request;
76     }
77
78     public RequestClientParameter buildRequestParams(ServiceInstancesRequest request, String requestURI,
79             String requestId, String serviceInstanceId) throws JsonProcessingException {
80         ObjectMapper mapper = new ObjectMapper();
81         return new RequestClientParameter.Builder().setRequestId(requestId).setServiceInstanceId(serviceInstanceId)
82                 .setALaCarte(true).setRequestDetails(mapper.writeValueAsString(request))
83                 .setRequestAction(Action.deleteInstance.toString()).setRequestUri(requestURI).setApiVersion("v8")
84                 .build();
85     }
86
87     public void saveInstanceName(ServiceInstancesRequest request, InfraActiveRequests currentRequest) {
88         try {
89             currentRequest.setServiceInstanceName(request.getRequestDetails().getRequestInfo().getInstanceName());
90             infraActiveRequestsClient.updateInfraActiveRequests(currentRequest);
91         } catch (Exception e) {
92             logger.warn("Could not update instance name", e);
93         }
94     }
95
96     public void checkDuplicateRequest(String serviceInstanceId, String instanceName, String requestId)
97             throws RequestConflictedException {
98         HashMap<String, String> instanceIdMap = new HashMap<>();
99         instanceIdMap.put("serviceInstanceId", serviceInstanceId);
100         checkDuplicateRequest(instanceIdMap, ModelType.service, instanceName, requestId);
101     }
102
103     public Recipe findServiceRecipe(String modelUUID, String action) throws NoRecipeException {
104         ServiceRecipe recipe = catalogDbClient.findServiceRecipeByActionAndServiceModelUUID(action, modelUUID);
105         if (recipe == null) {
106             recipe = catalogDbClient.findServiceRecipeByActionAndServiceModelUUID(action, DEFAULT_VF_MODULE_UUID);
107         }
108         if (recipe == null) {
109             throw new NoRecipeException(String.format(
110                     "Unable to locate custom or default recipe for, Action: %s, Model UUID: %s", action, modelUUID));
111         }
112         return recipe;
113     }
114
115 }