2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sparky.viewandinspect.servlet;
25 import java.io.IOException;
26 import java.io.PrintWriter;
28 import javax.servlet.FilterConfig;
29 import javax.servlet.ServletException;
30 import javax.servlet.http.HttpServlet;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
34 import org.apache.commons.io.IOUtils;
35 import org.onap.aai.sparky.config.oxm.OxmModelLoader;
36 import org.onap.aai.sparky.dal.rest.OperationResult;
37 import org.onap.aai.sparky.dal.servlet.ResettableStreamHttpServletRequest;
38 import org.onap.aai.sparky.logging.AaiUiMsgs;
39 import org.onap.aai.sparky.util.NodeUtils;
40 import org.onap.aai.sparky.viewandinspect.entity.QueryRequest;
41 import org.onap.aai.sparky.viewandinspect.services.VisualizationService;
42 import org.onap.aai.cl.api.Logger;
43 import org.onap.aai.cl.eelf.LoggerFactory;
44 import org.onap.aai.cl.mdc.MdcContext;
47 * A dedicated servlet for handling Front-End Visualization Requests and performing feats of magic
48 * to execute the right model/type/config driven queries to build the D3 visualization output JSON
54 public class VisualizationServlet extends HttpServlet {
59 private static final long serialVersionUID = 4678831934652478571L;
60 private static final Logger LOG =
61 LoggerFactory.getInstance().getLogger(VisualizationServlet.class);
62 private static final String VISUALIZATION_API_ENDPOINT = "prepareVisualization";
63 private final VisualizationService visualizationService;
65 * Instantiates a new visualization servlet.
67 * @throws Exception the exception
69 public VisualizationServlet() throws Exception {
70 this.visualizationService = new VisualizationService(OxmModelLoader.getInstance());
76 * @param filterConfig the filter config
77 * @throws ServletException the servlet exception
79 public void init(FilterConfig filterConfig) throws ServletException {
80 LOG.debug(AaiUiMsgs.DEBUG_GENERIC, "init()");
84 * Gets the request body.
86 * @param request the request
87 * @return the request body
89 private String getRequestBody(HttpServletRequest request) {
91 ResettableStreamHttpServletRequest requestWrapper =
92 new ResettableStreamHttpServletRequest(request);
96 body = IOUtils.toString(requestWrapper.getRequestBody());
97 } catch (IOException exc) {
98 LOG.error(AaiUiMsgs.EXCEPTION_CAUGHT, "Trying to get body from request",
99 exc.getLocalizedMessage());
108 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
109 * javax.servlet.http.HttpServletResponse)
112 protected void doGet(HttpServletRequest request, HttpServletResponse response)
113 throws ServletException, IOException {
114 doPost(request, response);
120 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
121 * javax.servlet.http.HttpServletResponse)
124 protected void doPost(HttpServletRequest request, HttpServletResponse response)
125 throws ServletException, IOException {
126 String txnID = request.getHeader("X-TransactionId");
128 txnID = NodeUtils.getRandomTxnId();
131 String partnerName = request.getHeader("X-FromAppId");
132 if (partnerName == null) {
133 partnerName = "Browser";
136 MdcContext.initialize(txnID, "AAI-UI", "", partnerName, request.getRemoteAddr());
138 String postRequestBody = getRequestBody(request);
140 String requestUri = request.getRequestURI();
141 OperationResult operationResult = null;
144 * For now we only have a single API call but there could be more in the future
146 if (requestUri.endsWith(VISUALIZATION_API_ENDPOINT)) {
149 * Work our magic and determine the best way to interrogate AAI to get the stuff we are
150 * interested in. Perhaps it should be an edge-tag-query or perhaps it is a straight up
151 * derived self-link query.
155 * Map request body to an interpreted API PoJo object
157 QueryRequest queryRequest = visualizationService.analyzeQueryRequestBody(postRequestBody);
159 if (queryRequest != null) {
160 operationResult = visualizationService.buildVisualizationUsingGenericQuery(queryRequest);
162 LOG.error(AaiUiMsgs.FAILED_TO_ANALYZE,
163 String.format("Failed to analyze post request query body = '%s'", postRequestBody));
165 operationResult = new OperationResult();
166 operationResult.setResult(500,
167 String.format("Failed to analyze post request query body = '%s'", postRequestBody));
173 LOG.error(AaiUiMsgs.UNKNOWN_SERVER_ERROR, "Unhandled requestUri - " + requestUri);
174 operationResult = new OperationResult();
175 operationResult.setResult(500, "Unknown Server Error: Unhandled requestUri = " + requestUri);
178 PrintWriter out = response.getWriter();
179 response.addHeader("Content-Type", "application/xml");
181 response.setStatus(operationResult.getResultCode());
183 if (operationResult.getResultCode() == 200) {
184 response.setContentLength(operationResult.getResult().length());
185 out.print(operationResult.getResult());
188 response.setContentLength(operationResult.getResult().length());
189 out.print(operationResult.getResult());
195 public void destroy() {
197 visualizationService.shutdown();