Increasing test coverage
[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 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.viewandinspect;
24
25
26 import org.apache.camel.Exchange;
27 import org.apache.camel.component.restlet.RestletConstants;
28 import org.onap.aai.cl.api.Logger;
29 import org.onap.aai.cl.eelf.LoggerFactory;
30 import org.onap.aai.cl.mdc.MdcContext;
31 import org.onap.aai.restclient.client.OperationResult;
32 import org.onap.aai.sparky.logging.AaiUiMsgs;
33 import org.onap.aai.sparky.util.NodeUtils;
34 import org.onap.aai.sparky.viewandinspect.entity.QueryRequest;
35 import org.onap.aai.sparky.viewandinspect.services.VisualizationService;
36
37 import org.restlet.Request;
38 import org.restlet.Response;
39 import org.restlet.data.ClientInfo;
40 import org.restlet.data.MediaType;
41 import org.restlet.data.Status;
42
43 public class SchemaVisualizationProcessor {
44
45
46         private static final Logger LOG =
47                       LoggerFactory.getInstance().getLogger(SchemaVisualizationProcessor.class);
48
49         private VisualizationService visualizationService; 
50
51         public SchemaVisualizationProcessor()throws Exception{}
52
53         protected String generateJsonErrorResponse(String message) {
54             return String.format("{ \"errorMessage\" : %s }", message);
55           }
56         
57         public void setVisualizationService(VisualizationService visualizationService){
58                 this.visualizationService = visualizationService; 
59         }
60         public VisualizationService getVisualizationService(){
61                 return visualizationService; 
62         }
63
64         public void processVisualizationRequest(Exchange exchange){
65
66                 String visualizationPayload="";
67                 QueryRequest hashId = null;
68                 OperationResult operationResult = null;
69                 Request request = null;
70                 Response response = null;
71                 Object xTransactionId = null;
72                 Object partnerName = null;
73
74                 xTransactionId = exchange.getIn().getHeader("X-TransactionId");
75             if (xTransactionId == null) {
76               xTransactionId = NodeUtils.getRandomTxnId();
77             }
78             partnerName = exchange.getIn().getHeader("X-FromAppId");
79             if (partnerName == null) {
80               partnerName = "Browser";
81             }
82
83              request = exchange.getIn().getHeader(RestletConstants.RESTLET_REQUEST, Request.class);
84              response = exchange.getIn().getHeader(RestletConstants.RESTLET_RESPONSE, Response.class);
85
86             /* Disables automatic Apache Camel Restlet component logging which prints out an undesirable log entry
87                which includes client (e.g. browser) information */
88             request.setLoggable(false);
89
90             ClientInfo clientInfo = request.getClientInfo();
91             MdcContext.initialize((String) xTransactionId, "AAI-UI", "", (String) partnerName, clientInfo.getAddress() + ":" + clientInfo.getPort());
92
93                 visualizationPayload = exchange.getIn().getBody(String.class);
94                 hashId = this.getVisualizationService().analyzeQueryRequestBody(visualizationPayload);
95
96                         if (hashId != null) {
97
98                           operationResult = this.getVisualizationService().buildVisualizationUsingGenericQuery(hashId);
99
100                           if(operationResult.getResultCode()== Status.SUCCESS_OK.getCode()){
101
102                                   response.setStatus(Status.SUCCESS_OK);
103                           }
104                           else{
105                                   response.setStatus(Status.SERVER_ERROR_INTERNAL);
106                                   LOG.error(AaiUiMsgs.FAILURE_TO_PROCESS_REQUEST,
107                                                 String.format("Failed to process Visualization Schema Payload = '%s'", visualizationPayload));
108                           }
109
110                         }else{
111                                 operationResult = new OperationResult();
112                         operationResult.setResult(String.format("Failed to analyze Visualization Schema Payload = '%s'", visualizationPayload));
113                         response.setStatus(Status.SERVER_ERROR_INTERNAL);
114                         LOG.error(AaiUiMsgs.FAILED_TO_ANALYZE,
115                                 String.format("Failed to analyze Visualization Schema Payload = '%s'", visualizationPayload));
116
117                         }
118
119
120                       response.setEntity(operationResult.getResult(), MediaType.APPLICATION_JSON);
121                       exchange.getOut().setBody(response);
122         }
123 }