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