Cleanup sparky-be pom
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / viewandinspect / SchemaVisualizationProcessor.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.sparky.viewandinspect;
22
23
24 import javax.servlet.http.HttpServletRequest;
25
26 import org.apache.camel.Exchange;
27 import org.onap.aai.cl.api.Logger;
28 import org.onap.aai.cl.eelf.LoggerFactory;
29 import org.onap.aai.restclient.client.OperationResult;
30 import org.onap.aai.sparky.logging.AaiUiMsgs;
31 import org.onap.aai.sparky.logging.util.ServletUtils;
32 import org.onap.aai.sparky.viewandinspect.entity.QueryRequest;
33 import org.restlet.data.Status;
34
35 public class SchemaVisualizationProcessor {
36
37
38         private static final Logger LOG =
39                       LoggerFactory.getInstance().getLogger(SchemaVisualizationProcessor.class);
40
41         private VisualizationService visualizationService; 
42
43         public SchemaVisualizationProcessor() throws Exception{}
44
45         protected String generateJsonErrorResponse(String message) {
46             return String.format("{ \"errorMessage\" : %s }", message);
47           }
48         
49         public void setVisualizationService(VisualizationService visualizationService){
50                 this.visualizationService = visualizationService; 
51         }
52         public VisualizationService getVisualizationService(){
53                 return visualizationService; 
54         }
55
56   public void processVisualizationRequest(Exchange exchange) {
57
58     HttpServletRequest request = exchange.getIn().getBody(HttpServletRequest.class);
59     ServletUtils.setUpMdcContext(exchange, request);
60
61     QueryRequest hashId = null;
62     OperationResult operationResult = null;
63     
64     String  visualizationPayload = exchange.getIn().getBody(String.class);
65     hashId = this.getVisualizationService().analyzeQueryRequestBody(visualizationPayload);
66
67     if (hashId != null) {
68
69       operationResult = this.getVisualizationService().buildVisualization(hashId);
70
71       if (operationResult.getResultCode() != Status.SUCCESS_OK.getCode()) {
72         exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, operationResult.getResultCode());
73         LOG.error(AaiUiMsgs.FAILURE_TO_PROCESS_REQUEST, String
74             .format("Failed to process Visualization Schema Payload = '%s'", visualizationPayload));
75         return;
76       }
77
78     } else {
79       operationResult = new OperationResult();
80       operationResult.setResult(String
81           .format("Failed to analyze Visualization Schema Payload = '%s'", visualizationPayload));
82       exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, operationResult.getResultCode());
83       LOG.error(AaiUiMsgs.FAILED_TO_ANALYZE, String
84           .format("Failed to analyze Visualization Schema Payload = '%s'", visualizationPayload));
85       return;
86
87     }
88
89     exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, operationResult.getResultCode());
90     exchange.getOut().setBody(operationResult.getResult());
91     
92   }
93 }