Adding interfaces in documentation
[aai/sparky-be.git] / 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 org.apache.camel.Exchange;
25 import org.apache.camel.component.restlet.RestletConstants;
26 import org.onap.aai.cl.api.Logger;
27 import org.onap.aai.cl.eelf.LoggerFactory;
28 import org.onap.aai.cl.mdc.MdcContext;
29 import org.onap.aai.restclient.client.OperationResult;
30 import org.onap.aai.sparky.logging.AaiUiMsgs;
31 import org.onap.aai.sparky.util.NodeUtils;
32 import org.onap.aai.sparky.viewandinspect.entity.QueryRequest;
33 import org.onap.aai.sparky.viewandinspect.services.VisualizationService;
34
35 import org.restlet.Request;
36 import org.restlet.Response;
37 import org.restlet.data.ClientInfo;
38 import org.restlet.data.MediaType;
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                 String visualizationPayload="";
65                 QueryRequest hashId = null;
66                 OperationResult operationResult = null;
67                 Request request = null;
68                 Response response = null;
69                 Object xTransactionId = null;
70                 Object partnerName = null;
71
72                 xTransactionId = exchange.getIn().getHeader("X-TransactionId");
73             if (xTransactionId == null) {
74               xTransactionId = NodeUtils.getRandomTxnId();
75             }
76             partnerName = exchange.getIn().getHeader("X-FromAppId");
77             if (partnerName == null) {
78               partnerName = "Browser";
79             }
80
81              request = exchange.getIn().getHeader(RestletConstants.RESTLET_REQUEST, Request.class);
82              response = exchange.getIn().getHeader(RestletConstants.RESTLET_RESPONSE, Response.class);
83
84             /* Disables automatic Apache Camel Restlet component logging which prints out an undesirable log entry
85                which includes client (e.g. browser) information */
86             request.setLoggable(false);
87
88             ClientInfo clientInfo = request.getClientInfo();
89             MdcContext.initialize((String) xTransactionId, "AAI-UI", "", (String) partnerName, clientInfo.getAddress() + ":" + clientInfo.getPort());
90
91                 visualizationPayload = exchange.getIn().getBody(String.class);
92                 hashId = this.getVisualizationService().analyzeQueryRequestBody(visualizationPayload);
93
94                         if (hashId != null) {
95
96                           operationResult = this.getVisualizationService().buildVisualizationUsingGenericQuery(hashId);
97
98                           if(operationResult.getResultCode()== Status.SUCCESS_OK.getCode()){
99
100                                   response.setStatus(Status.SUCCESS_OK);
101                           }
102                           else{
103                                   response.setStatus(Status.SERVER_ERROR_INTERNAL);
104                                   LOG.error(AaiUiMsgs.FAILURE_TO_PROCESS_REQUEST,
105                                                 String.format("Failed to process Visualization Schema Payload = '%s'", visualizationPayload));
106                           }
107
108                         }else{
109                                 operationResult = new OperationResult();
110                         operationResult.setResult(String.format("Failed to analyze Visualization Schema Payload = '%s'", visualizationPayload));
111                         response.setStatus(Status.SERVER_ERROR_INTERNAL);
112                         LOG.error(AaiUiMsgs.FAILED_TO_ANALYZE,
113                                 String.format("Failed to analyze Visualization Schema Payload = '%s'", visualizationPayload));
114
115                         }
116
117
118                       response.setEntity(operationResult.getResult(), MediaType.APPLICATION_JSON);
119                       exchange.getOut().setBody(response);
120         }
121 }