Revise POMs for LF CI/CD env
[federation.git] / gateway / src / main / java / org / acumos / federation / gateway / controller / CatalogController.java
1 /*-
2  * ===============LICENSE_START=======================================================
3  * Acumos
4  * ===================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property & Tech Mahindra. All rights reserved.
6  * ===================================================================================
7  * This Acumos software file is distributed by AT&T and Tech Mahindra
8  * 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  * This file is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ===============LICENSE_END=========================================================
19  */
20
21 package org.acumos.federation.gateway.controller;
22
23 import java.net.URI;
24 import java.util.List;
25 import java.util.Map;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import org.acumos.cds.domain.MLPArtifact;
31 import org.acumos.cds.domain.MLPSolution;
32 import org.acumos.cds.domain.MLPSolutionRevision;
33 import org.acumos.federation.gateway.common.API;
34 import org.acumos.federation.gateway.common.JSONTags;
35 import org.acumos.federation.gateway.common.JsonResponse;
36 import org.acumos.federation.gateway.config.EELFLoggerDelegate;
37 import org.acumos.federation.gateway.security.Peer;
38 import org.acumos.federation.gateway.service.CatalogService;
39 import org.acumos.federation.gateway.service.ServiceContext;
40 import org.acumos.federation.gateway.util.Utils;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.core.io.InputStreamResource;
43 import org.springframework.http.MediaType;
44 import org.springframework.security.access.prepost.PreAuthorize;
45 import org.springframework.security.core.context.SecurityContextHolder;
46 import org.springframework.stereotype.Controller;
47 import org.springframework.util.Base64Utils;
48 import org.springframework.web.bind.annotation.CrossOrigin;
49 import org.springframework.web.bind.annotation.PathVariable;
50 import org.springframework.web.bind.annotation.RequestMapping;
51 import org.springframework.web.bind.annotation.RequestMethod;
52 import org.springframework.web.bind.annotation.RequestParam;
53 import org.springframework.web.bind.annotation.ResponseBody;
54
55 import io.swagger.annotations.ApiOperation;
56
57 /**
58  * 
59  *
60  */
61 @Controller
62 @RequestMapping("/")
63 public class CatalogController extends AbstractController {
64
65         private static final EELFLoggerDelegate log = EELFLoggerDelegate.getLogger(CatalogController.class.getName());
66
67         @Autowired
68         private CatalogService catalogService;
69
70         /**
71          * @param theHttpResponse
72          *            HttpServletResponse
73          * @param theSelector
74          *            Solutions selector
75          * @return List of Published ML Solutions in JSON format.
76          */
77         @CrossOrigin
78         // @PreAuthorize("hasAuthority('PEER')"
79         @PreAuthorize("hasAuthority(T(org.acumos.federation.gateway.security.Priviledge).CATALOG_ACCESS)")
80         @ApiOperation(value = "Invoked by Peer Acumos to get a list of Published Solutions from the Catalog of the local Acumos Instance .", response = MLPSolution.class, responseContainer = "List")
81         @RequestMapping(value = { API.Paths.SOLUTIONS }, method = RequestMethod.GET, produces = APPLICATION_JSON)
82         @ResponseBody
83         public JsonResponse<List<MLPSolution>> getSolutions(
84                         /* HttpServletRequest theHttpRequest, */
85                         HttpServletResponse theHttpResponse,
86                         @RequestParam(value = API.QueryParameters.SOLUTIONS_SELECTOR, required = false) String theSelector) {
87                 JsonResponse<List<MLPSolution>> response = null;
88                 List<MLPSolution> peerCatalogSolutions = null;
89                 log.debug(EELFLoggerDelegate.debugLogger, API.Paths.SOLUTIONS);
90                 try {
91                         response = new JsonResponse<List<MLPSolution>>();
92                         log.debug(EELFLoggerDelegate.debugLogger, "getSolutionsListFromPeer: selector " + theSelector);
93                         Map<String, ?> selector = null;
94                         if (theSelector != null)
95                                 selector = Utils.jsonStringToMap(new String(Base64Utils.decodeFromString(theSelector), "UTF-8"));
96
97                         peerCatalogSolutions = catalogService.getSolutions(selector, new ControllerContext());
98                         if (peerCatalogSolutions != null) {
99                                 response.setResponseBody(peerCatalogSolutions);
100                                 response.setResponseCode(String.valueOf(HttpServletResponse.SC_OK));
101                                 response.setResponseDetail(JSONTags.TAG_STATUS_SUCCESS);
102                                 response.setStatus(true);
103                                 theHttpResponse.setStatus(HttpServletResponse.SC_OK);
104                                 log.debug(EELFLoggerDelegate.debugLogger, "getSolutions: size is " + peerCatalogSolutions.size());
105                         }
106                 } catch (Exception e) {
107                         response.setResponseCode(String.valueOf(HttpServletResponse.SC_BAD_REQUEST));
108                         response.setResponseDetail(JSONTags.TAG_STATUS_FAILURE);
109                         response.setStatus(false);
110                         theHttpResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST);
111                         log.error(EELFLoggerDelegate.errorLogger, "Exception occurred fetching Solutions for Market Place Catalog",
112                                         e);
113                 }
114                 return response;
115         }
116
117         @CrossOrigin
118         @PreAuthorize("hasAuthority('CATALOG_ACCESS')")
119         @ApiOperation(value = "Invoked by Peer Acumos to get a list detailed solution information from the Catalog of the local Acumos Instance .", response = MLPSolution.class)
120         @RequestMapping(value = { API.Paths.SOLUTION_DETAILS }, method = RequestMethod.GET, produces = APPLICATION_JSON)
121         @ResponseBody
122         public JsonResponse<MLPSolution> getSolutionDetails(HttpServletResponse theHttpResponse,
123                         @PathVariable(value = "solutionId") String theSolutionId) {
124                 JsonResponse<MLPSolution> response = null;
125                 MLPSolution solution = null;
126                 log.debug(EELFLoggerDelegate.debugLogger, API.Paths.SOLUTION_DETAILS + ": " + theSolutionId);
127                 try {
128                         response = new JsonResponse<MLPSolution>();
129                         solution = catalogService.getSolution(theSolutionId, new ControllerContext());
130                         if (solution != null) {
131                                 response.setResponseBody(solution);
132                                 response.setResponseCode(String.valueOf(HttpServletResponse.SC_OK));
133                                 response.setResponseDetail(JSONTags.TAG_STATUS_SUCCESS);
134                                 response.setStatus(true);
135                                 theHttpResponse.setStatus(HttpServletResponse.SC_OK);
136                         }
137                 } catch (Exception e) {
138                         response.setResponseCode(String.valueOf(HttpServletResponse.SC_BAD_REQUEST));
139                         response.setResponseDetail(JSONTags.TAG_STATUS_FAILURE);
140                         response.setStatus(false);
141                         theHttpResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST);
142                         log.error(EELFLoggerDelegate.errorLogger, "An error occurred fetching solution " + theSolutionId, e);
143                 }
144                 return response;
145         }
146
147         /**
148          * @param theSolutionId
149          *            Solution ID
150          * @param theHttpResponse
151          *            HttpServletResponse
152          * @return List of Published ML Solutions in JSON format.
153          */
154         @CrossOrigin
155         @PreAuthorize("hasAuthority('CATALOG_ACCESS')")
156         @ApiOperation(value = "Invoked by Peer Acumos to get a list of Solution Revision from the Catalog of the local Acumos Instance .", response = MLPSolutionRevision.class, responseContainer = "List")
157         @RequestMapping(value = { API.Paths.SOLUTION_REVISIONS }, method = RequestMethod.GET, produces = APPLICATION_JSON)
158         @ResponseBody
159         public JsonResponse<List<MLPSolutionRevision>> getSolutionRevisions(HttpServletResponse theHttpResponse,
160                         @PathVariable("solutionId") String theSolutionId) {
161                 JsonResponse<List<MLPSolutionRevision>> response = null;
162                 List<MLPSolutionRevision> solutionRevisions = null;
163                 log.debug(EELFLoggerDelegate.debugLogger, API.Paths.SOLUTION_REVISIONS);
164                 try {
165                         response = new JsonResponse<List<MLPSolutionRevision>>();
166                         solutionRevisions = catalogService.getSolutionRevisions(theSolutionId, new ControllerContext());
167                         if (solutionRevisions != null) {
168                                 response.setResponseBody(solutionRevisions);
169                                 response.setResponseCode(String.valueOf(HttpServletResponse.SC_OK));
170                                 response.setResponseDetail(JSONTags.TAG_STATUS_SUCCESS);
171                                 response.setStatus(true);
172                                 theHttpResponse.setStatus(HttpServletResponse.SC_OK);
173                                 log.debug(EELFLoggerDelegate.debugLogger, "getSolutionsRevisions: size is {} ",
174                                                 solutionRevisions.size());
175                         }
176                 } catch (Exception e) {
177                         response.setResponseCode(String.valueOf(HttpServletResponse.SC_BAD_REQUEST));
178                         response.setResponseDetail(JSONTags.TAG_STATUS_FAILURE);
179                         response.setStatus(false);
180                         theHttpResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST);
181                         log.error(EELFLoggerDelegate.errorLogger, "Exception Occurred Fetching Solution Revisions", e);
182                 }
183                 return response;
184         }
185
186         /**
187          * 
188          * @param theSolutionId
189          *            Solution ID
190          * @param theRevisionId
191          *            Revision ID
192          * @param theHttpResponse
193          *            HttpServletResponse
194          * @return List of Published ML Solutions in JSON format.
195          */
196         @CrossOrigin
197         @PreAuthorize("hasAuthority('CATALOG_ACCESS')")
198         @ApiOperation(value = "Invoked by Peer Acumos to get Solution Revision details from the Catalog of the local Acumos Instance .", response = MLPSolutionRevision.class)
199         @RequestMapping(value = {
200                         API.Paths.SOLUTION_REVISION_DETAILS }, method = RequestMethod.GET, produces = APPLICATION_JSON)
201         @ResponseBody
202         public JsonResponse<MLPSolutionRevision> getSolutionRevisionDetails(HttpServletResponse theHttpResponse,
203                         @PathVariable("solutionId") String theSolutionId, @PathVariable("revisionId") String theRevisionId) {
204                 JsonResponse<MLPSolutionRevision> response = null;
205                 MLPSolutionRevision solutionRevision = null;
206                 log.debug(EELFLoggerDelegate.debugLogger,
207                                 API.Paths.SOLUTION_REVISION_DETAILS + "(" + theSolutionId + "," + theRevisionId + ")");
208                 try {
209                         response = new JsonResponse<MLPSolutionRevision>();
210                         solutionRevision = catalogService.getSolutionRevision(theSolutionId, theRevisionId,
211                                         new ControllerContext());
212                         if (solutionRevision != null) {
213                                 response.setResponseBody(solutionRevision);
214                                 response.setResponseCode(String.valueOf(HttpServletResponse.SC_OK));
215                                 response.setResponseDetail(JSONTags.TAG_STATUS_SUCCESS);
216                                 response.setStatus(true);
217                                 theHttpResponse.setStatus(HttpServletResponse.SC_OK);
218                         }
219                 } catch (Exception e) {
220                         response.setResponseCode(String.valueOf(HttpServletResponse.SC_BAD_REQUEST));
221                         response.setResponseDetail(JSONTags.TAG_STATUS_FAILURE);
222                         response.setStatus(false);
223                         theHttpResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST);
224                         log.error(EELFLoggerDelegate.errorLogger, "Exception Occurred Fetching Solution Revision", e);
225                 }
226                 return response;
227         }
228
229         /**
230          * @param theHttpRequest
231          *            HttpServletRequest
232          * @param theHttpResponse
233          *            HttpServletResponse
234          * @param theSolutionId
235          *            Solution ID
236          * @param theRevisionId
237          *            Revision ID
238          * @return List of Published ML Solutions in JSON format.
239          */
240         @CrossOrigin
241         @PreAuthorize("hasAuthority('CATALOG_ACCESS')")
242         @ApiOperation(value = "Invoked by Peer Acumos to get a list of Solution Revision Artifacts from the Catalog of the local Acumos Instance .", response = MLPArtifact.class, responseContainer = "List")
243         @RequestMapping(value = {
244                         API.Paths.SOLUTION_REVISION_ARTIFACTS }, method = RequestMethod.GET, produces = APPLICATION_JSON)
245         @ResponseBody
246         public JsonResponse<List<MLPArtifact>> getSolutionRevisionArtifacts(HttpServletRequest theHttpRequest,
247                         HttpServletResponse theHttpResponse, @PathVariable("solutionId") String theSolutionId,
248                         @PathVariable("revisionId") String theRevisionId) {
249                 JsonResponse<List<MLPArtifact>> response = null;
250                 List<MLPArtifact> solutionRevisionArtifacts = null;
251                 log.debug(EELFLoggerDelegate.debugLogger,
252                                 API.Paths.SOLUTION_REVISION_ARTIFACTS + "(" + theSolutionId + "," + theRevisionId + ")");
253                 try {
254                         response = new JsonResponse<List<MLPArtifact>>();
255                         solutionRevisionArtifacts = catalogService.getSolutionRevisionArtifacts(theSolutionId, theRevisionId,
256                                         new ControllerContext());
257                         if (solutionRevisionArtifacts != null) {
258                                 // re-encode the artifact uri
259                                 {
260                                         for (MLPArtifact artifact : solutionRevisionArtifacts) {
261                                                 // sooo cumbersome
262                                                 URI requestUri = new URI(theHttpRequest.getRequestURL().toString());
263                                                 URI artifactUri = API.ARTIFACT_DOWNLOAD
264                                                                 .buildUri(
265                                                                                 new URI(requestUri.getScheme(), null, requestUri.getHost(),
266                                                                                                 requestUri.getPort(), null, null, null).toString(),
267                                                                                 artifact.getArtifactId());
268                                                 log.debug(EELFLoggerDelegate.debugLogger,
269                                                                 "getSolutionRevisionArtifacts: content uri " + artifactUri);
270                                                 artifact.setUri(artifactUri.toString());
271                                         }
272                                 }
273                                 response.setResponseBody(solutionRevisionArtifacts);
274                                 response.setResponseCode(String.valueOf(HttpServletResponse.SC_OK));
275                                 response.setResponseDetail(JSONTags.TAG_STATUS_SUCCESS);
276                                 response.setStatus(true);
277                                 theHttpResponse.setStatus(HttpServletResponse.SC_OK);
278                                 log.debug(EELFLoggerDelegate.debugLogger, "getSolutionRevisionArtifacts: size is {} ",
279                                                 solutionRevisionArtifacts.size());
280                         }
281                 } catch (Exception e) {
282                         response.setResponseCode(String.valueOf(HttpServletResponse.SC_BAD_REQUEST));
283                         response.setResponseDetail(JSONTags.TAG_STATUS_FAILURE);
284                         response.setStatus(false);
285                         theHttpResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST);
286                         log.error(EELFLoggerDelegate.errorLogger, "Failed to fetch solution revision artifacts", e);
287                 }
288                 return response;
289         }
290
291         /**
292          * @param theHttpRequest
293          *            HttpServletRequest
294          * @param theHttpResponse
295          *            HttpServletResponse
296          * @param theArtifactId
297          *            Artifact ID
298          * @return Archive file of the Artifact for the Solution.
299          */
300         @CrossOrigin
301         @PreAuthorize("hasAuthority('CATALOG_ACCESS')")
302         @ApiOperation(value = "API to download the Machine Learning Artifact of the Machine Learning Solution", response = InputStreamResource.class, code = 200)
303         @RequestMapping(value = {
304                         API.Paths.ARTIFACT_DOWNLOAD }, method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
305         @ResponseBody
306         public InputStreamResource downloadSolutionArtifact(HttpServletRequest theHttpRequest,
307                         HttpServletResponse theHttpResponse, @PathVariable("artifactId") String theArtifactId) {
308                 InputStreamResource inputStreamResource = null;
309                 try {
310                         inputStreamResource = catalogService.getSolutionRevisionArtifactContent(theArtifactId,
311                                         new ControllerContext());
312                         theHttpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
313                         theHttpResponse.setHeader("Pragma", "no-cache");
314                         theHttpResponse.setHeader("Expires", "0");
315                         theHttpResponse.setStatus(HttpServletResponse.SC_OK);
316                 } catch (Exception e) {
317                         theHttpResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST);
318                         log.error(EELFLoggerDelegate.errorLogger,
319                                         "Exception Occurred downloading a artifact for a Solution in Market Place Catalog", e);
320                 }
321                 return inputStreamResource;
322         }
323
324         protected class ControllerContext implements ServiceContext {
325
326                 public Peer getPeer() {
327                         return (Peer) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
328                 }
329         }
330 }