Merge "Add proxy support in DockerFile"
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / controller / UserRecommendationController.java
1 package org.openecomp.portalapp.portal.controller;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
8
9 import org.openecomp.portalapp.controller.EPRestrictedBaseController;
10 import org.openecomp.portalapp.portal.domain.EPUser;
11 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
12 import org.openecomp.portalapp.portal.service.ConsulHealthService;
13 import org.openecomp.portalapp.util.EPUserUtils;
14 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
15 import org.openecomp.portalsdk.core.util.SystemProperties;
16 import org.springframework.beans.factory.annotation.Autowired;
17 import org.springframework.context.annotation.EnableAspectJAutoProxy;
18 import org.springframework.http.HttpEntity;
19 import org.springframework.http.HttpHeaders;
20 import org.springframework.http.HttpMethod;
21 import org.springframework.http.MediaType;
22 import org.springframework.http.ResponseEntity;
23 import org.springframework.web.bind.annotation.RequestMapping;
24 import org.springframework.web.bind.annotation.RequestMethod;
25 import org.springframework.web.bind.annotation.RestController;
26 import org.springframework.web.client.RestTemplate;
27
28 @RestController
29 @org.springframework.context.annotation.Configuration
30 @EnableAspectJAutoProxy
31 @EPAuditLog
32 public class UserRecommendationController extends EPRestrictedBaseController {
33
34         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UserRecommendationController.class);
35
36         @Autowired
37         private ConsulHealthService consulHealthService;
38
39         private static final String MACHINE_LEARNING_SERVICE_CTX = "/ml_api";
40         private static final String GET_RECOMMENDATION =  MACHINE_LEARNING_SERVICE_CTX + "/" + "getRecommendation";
41         private static final String GET_RECOMM_COUNT = MACHINE_LEARNING_SERVICE_CTX + "/" + "getRecommCount";
42         private static final String CONSUL_ML_SERVICE_ID = "mlearning-service"; 
43         private static final String SERVICE_PROTOCOL = "http";
44
45         @RequestMapping(value = {
46                         "/portalApi/getRecommendationsCount" }, method = RequestMethod.GET, produces = "application/json")
47         public String getRecommendationsCount(HttpServletRequest request, HttpServletResponse response) {
48                 EPUser user = EPUserUtils.getUserSession(request);
49                 Map<String, String> requestMapping = new HashMap<String, String>();
50                 requestMapping.put("id", user.getOrgUserId());
51                 requestMapping.put("action", "reports");
52
53                 HttpHeaders headers = new HttpHeaders();
54                 headers.setContentType(MediaType.APPLICATION_JSON);
55
56                 // set your entity to send
57                 HttpEntity<Map<String,String>> entity = new HttpEntity<>(requestMapping, headers);
58                 String endpoint = SERVICE_PROTOCOL + "://"+     consulHealthService.getServiceLocation(CONSUL_ML_SERVICE_ID,
59                                 SystemProperties.getProperty("microservices.m-learn.local.port")) + GET_RECOMM_COUNT;
60                 logger.debug(EELFLoggerDelegate.debugLogger, "Going to hit mlearning endpoint on: {1}", endpoint);
61                 ResponseEntity<String> out = new RestTemplate().exchange(endpoint, HttpMethod.POST, entity, String.class);
62                 return out.getBody();
63         }
64
65         @RequestMapping(value = {
66                         "/portalApi/getRecommendations" }, method = RequestMethod.GET, produces = "application/json")
67         public String getRecommendations(HttpServletRequest request, HttpServletResponse response) {
68                 EPUser user = EPUserUtils.getUserSession(request);
69                 Map<String, String> requestMapping = new HashMap<String, String>();
70                 requestMapping.put("id", user.getOrgUserId());
71                 requestMapping.put("action", "reports");
72                 requestMapping.put("recommendations", "1");
73
74                 HttpHeaders headers = new HttpHeaders();
75                 headers.setContentType(MediaType.APPLICATION_JSON);
76
77                 // set your entity to send
78                 HttpEntity<Map<String,String>> entity = new HttpEntity<>(requestMapping, headers);
79                 String endpoint = SERVICE_PROTOCOL + "://"+ 
80                                 consulHealthService.getServiceLocation(CONSUL_ML_SERVICE_ID,
81                                                 SystemProperties.getProperty("microservices.m-learn.local.port")) + GET_RECOMMENDATION;
82                 logger.debug(EELFLoggerDelegate.debugLogger, "Going to hit mlearning endpoint on: {1}", endpoint);
83                 ResponseEntity<String> out = new RestTemplate().exchange(endpoint, HttpMethod.POST, entity, String.class);
84                 return out.getBody();
85         }
86
87 }