Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / viewandinspect / servlet / SearchServlet.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
26 package org.openecomp.sparky.viewandinspect.servlet;
27
28 import java.io.IOException;
29 import java.io.PrintWriter;
30 import java.util.HashMap;
31 import java.util.Map;
32
33 import javax.servlet.ServletException;
34 import javax.servlet.http.HttpServlet;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
37
38 import org.json.JSONException;
39 import org.json.JSONObject;
40 import org.openecomp.cl.api.Logger;
41 import org.openecomp.cl.eelf.LoggerFactory;
42 import org.openecomp.sparky.config.oxm.OxmModelLoader;
43 import org.openecomp.sparky.dal.elasticsearch.SearchAdapter;
44 import org.openecomp.sparky.dal.rest.OperationResult;
45 import org.openecomp.sparky.dal.sas.config.SearchServiceConfig;
46 import org.openecomp.sparky.logging.AaiUiMsgs;
47 import org.openecomp.sparky.search.VnfSearchService;
48 import org.openecomp.sparky.search.config.SuggestionConfig;
49 import org.openecomp.sparky.util.NodeUtils;
50 import org.openecomp.sparky.viewandinspect.services.SearchServiceWrapper;
51
52 import org.openecomp.cl.mdc.MdcContext;
53
54 /**
55  * The Class SearchServlet.
56  */
57
58 public class SearchServlet extends HttpServlet {
59
60   private static final long serialVersionUID = 1L;
61
62   private static final Logger LOG = LoggerFactory.getInstance().getLogger(SearchServlet.class);
63
64   private SearchServiceWrapper searchWrapper = null;
65
66   private static final String KEY_PAYLOAD = "payload";
67   
68   /**
69    * Instantiates a new search servlet.
70    */
71   public SearchServlet() {
72   }
73
74   /*
75    * (non-Javadoc)
76    * 
77    * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
78    * javax.servlet.http.HttpServletResponse)
79    */
80   @Override
81   public void doGet(HttpServletRequest request, HttpServletResponse response)
82       throws ServletException, IOException {
83     doPost(request, response);
84   }
85
86    public void destroy() {
87     // TODO Auto-generated method stub
88     super.destroy();
89   }
90   
91   public void init() throws ServletException {
92     super.init();
93     searchWrapper = new SearchServiceWrapper();
94   }
95
96   protected Map<String, String> getPayloadParams(JSONObject parameters) {
97     Map<String, String> payloadParams = new HashMap<String, String>();
98     try {
99       JSONObject payload = parameters.getJSONObject(KEY_PAYLOAD);
100       if (payload.length() > 0) {
101         for (String key : JSONObject.getNames(payload)) {
102           payloadParams.put(key, payload.getString(key));
103         }
104       }
105     } catch (JSONException exc) {
106       LOG.error(AaiUiMsgs.ERROR_PARSING_PARAMS, exc);
107     }
108     return payloadParams;
109   }
110
111   /*
112    * (non-Javadoc)
113    * 
114    * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
115    * javax.servlet.http.HttpServletResponse)
116    */
117   @Override
118   public void doPost(HttpServletRequest request, HttpServletResponse response)
119       throws ServletException, IOException {
120     String txnID = request.getHeader("X-TransactionId");
121     if (txnID == null) { 
122       txnID = NodeUtils.getRandomTxnId();
123     }
124
125     String partnerName = request.getHeader("X-FromAppId");
126     if (partnerName == null) {
127       partnerName = "Browser";
128     }
129     MdcContext.initialize(txnID, "AAI_UI", "", partnerName, request.getRemoteAddr());
130     searchWrapper.doPost(request, response);
131   }
132
133   /**
134    * Generate json error response.
135    *
136    * @param message the message
137    * @return the string
138    */
139   /*
140    * This is the manual approach, however we could also create an object container for the error
141    * then use the Jackson ObjectWrite to dump the object to json instead. If it gets any more
142    * complicated we could do that approach so we don't have to manually trip over the JSON
143    * formatting.
144    */
145   protected String generateJsonErrorResponse(String message) {
146     return String.format("{ \"errorMessage\" : %s }", message);
147   }
148
149   /**
150    * Handle search servlet errors.
151    *
152    * @param errorMsg the error msg
153    * @param exc the exc
154    * @param response the response
155    * @throws IOException Signals that an I/O exception has occurred.
156    */
157   public void handleSearchServletErrors(String errorMsg, Exception exc,
158       HttpServletResponse response) throws IOException {
159
160     String errorLogMsg =
161         (exc == null ? errorMsg : errorMsg + ". Error:" + exc.getLocalizedMessage());
162
163     LOG.error(AaiUiMsgs.SEARCH_SERVLET_ERROR, errorLogMsg);
164
165     response.setContentType("application/json");
166     PrintWriter out = response.getWriter();
167     out.println(generateJsonErrorResponse(errorMsg));
168     out.close();
169   }
170
171
172   /**
173    * Sets the servlet response.
174    * 
175    * @param response the response
176    * @param postPayload the post payload
177    *
178    * @throws IOException Signals that an I/O exception has occurred.
179    */
180   private void setServletResponse(HttpServletResponse response, String postPayload)
181       throws IOException {
182
183     if (postPayload != null) {
184       response.setContentType("application/json");
185       PrintWriter out = response.getWriter();
186       out.println(postPayload);
187       out.close();
188     }
189   }
190
191   
192
193   
194 }