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;
66 * Instantiates a new visualization servlet.
68 * @throws Exception the exception
70 public VisualizationServlet() throws Exception {
71 this.visualizationService = new VisualizationService(OxmModelLoader.getInstance());
77 * @param filterConfig the filter config
78 * @throws ServletException the servlet exception
80 public void init(FilterConfig filterConfig) throws ServletException {
81 LOG.debug(AaiUiMsgs.DEBUG_GENERIC, "init()");
85 * Gets the request body.
87 * @param request the request
88 * @return the request body
90 private String getRequestBody(HttpServletRequest request) {
92 ResettableStreamHttpServletRequest requestWrapper =
93 new ResettableStreamHttpServletRequest(request);
97 body = IOUtils.toString(requestWrapper.getRequestBody());
98 } catch (IOException exc) {
99 LOG.error(AaiUiMsgs.EXCEPTION_CAUGHT, "Trying to get body from request",
100 exc.getLocalizedMessage());
109 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
110 * javax.servlet.http.HttpServletResponse)
113 protected void doGet(HttpServletRequest request, HttpServletResponse response)
114 throws ServletException, IOException {
115 doPost(request, response);
121 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
122 * javax.servlet.http.HttpServletResponse)
125 protected void doPost(HttpServletRequest request, HttpServletResponse response)
126 throws ServletException, IOException {
127 String txnID = request.getHeader("X-TransactionId");
129 txnID = NodeUtils.getRandomTxnId();
132 String partnerName = request.getHeader("X-FromAppId");
133 if (partnerName == null) {
134 partnerName = "Browser";
137 MdcContext.initialize(txnID, "AAI-UI", "", partnerName, request.getRemoteAddr());
139 String postRequestBody = getRequestBody(request);
141 String requestUri = request.getRequestURI();
142 OperationResult operationResult = null;
145 * For now we only have a single API call but there could be more in the future
147 if (requestUri.endsWith(VISUALIZATION_API_ENDPOINT)) {
150 * Work our magic and determine the best way to interrogate AAI to get the stuff we are
151 * interested in. Perhaps it should be an edge-tag-query or perhaps it is a straight up
152 * derived self-link query.
156 * Map request body to an interpreted API PoJo object
158 QueryRequest queryRequest = visualizationService.analyzeQueryRequestBody(postRequestBody);
160 if (queryRequest != null) {
161 operationResult = visualizationService.buildVisualizationUsingGenericQuery(queryRequest);
163 LOG.error(AaiUiMsgs.FAILED_TO_ANALYZE,
164 String.format("Failed to analyze post request query body = '%s'", postRequestBody));
166 operationResult = new OperationResult();
167 operationResult.setResult(500,
168 String.format("Failed to analyze post request query body = '%s'", postRequestBody));
174 LOG.error(AaiUiMsgs.UNKNOWN_SERVER_ERROR, "Unhandled requestUri - " + requestUri);
175 operationResult = new OperationResult();
176 operationResult.setResult(500, "Unknown Server Error: Unhandled requestUri = " + requestUri);
179 PrintWriter out = response.getWriter();
180 response.addHeader("Content-Type", "application/xml");
182 response.setStatus(operationResult.getResultCode());
184 if (operationResult.getResultCode() == 200) {
185 response.setContentLength(operationResult.getResult().length());
186 out.print(operationResult.getResult());
189 response.setContentLength(operationResult.getResult().length());
190 out.print(operationResult.getResult());
196 public void destroy() {
198 visualizationService.shutdown();