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