Authentication support for cdt
[appc/cdt.git] / CdtProxyService / src / main / java / org / onap / appc / cdt / service / controller / CdtController.java
1 /*
2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2018-2020 AT&T Intellectual Property. All rights reserved.
5 ===================================================================
6
7 Unless otherwise specified, all software contained herein is licensed
8 under the Apache License, Version 2.0 (the License);
9 you may not use this software 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 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
20 ============LICENSE_END============================================
21 */
22
23 package org.onap.appc.cdt.service.controller;
24
25 import io.swagger.annotations.Api;
26 import io.swagger.annotations.ApiOperation;
27 import io.swagger.annotations.ApiResponse;
28 import io.swagger.annotations.ApiResponses;
29 import org.apache.http.client.HttpClient;
30 import org.apache.http.impl.client.HttpClientBuilder;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Value;
34 import org.springframework.http.HttpEntity;
35 import org.springframework.http.HttpHeaders;
36 import org.springframework.http.MediaType;
37 import org.springframework.http.client.ClientHttpRequestFactory;
38 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
39 import org.springframework.web.bind.annotation.*;
40 import org.springframework.web.client.RestTemplate;
41
42 import java.net.UnknownHostException;
43 import java.util.Base64;
44 import java.util.List;
45
46 /**
47  * Created by Amaresh Kumar on 09/May/2018.
48  */
49
50 @RestController
51 @RequestMapping("/cdtService")
52 @CrossOrigin(origins = "*", allowedHeaders = "*")
53 @Api(value = "cdtService", description = "Backend service to eliminate CORS issue for CDT Tool")
54 public class CdtController {
55
56     private static final Logger logger = LoggerFactory.getLogger(CdtController.class);
57     private RestTemplate restTemplate = new RestTemplate();
58     private String urlAddress;
59
60     @Value("${restConf.backend.hostname}")
61     private String restConfHostname;
62
63     @Value("${restConf.backend.port}")
64     private String restConfPort;
65
66
67     @ApiOperation(value = "Return All Test Data for a given user", response = CdtController.class)
68     @ApiResponses(value = {
69             @ApiResponse(code = 200, message = "OK"),
70             @ApiResponse(code = 404, message = "The resource not found")
71     })
72     @RequestMapping(value = "", method = RequestMethod.GET)
73     @CrossOrigin(origins = "*", allowedHeaders = "*")
74     public String DefaultEndpoint()  {
75         return  "CDT Proxy Service is up and running.";
76
77     }
78
79     @ApiOperation(value = "Return All Test Data for a given user", response = CdtController.class)
80     @ApiResponses(value = {
81             @ApiResponse(code = 200, message = "OK"),
82             @ApiResponse(code = 404, message = "The resource not found")
83     })
84     @RequestMapping(value = "/getDesigns", method = RequestMethod.POST)
85     @CrossOrigin(origins = "*", allowedHeaders = "*")
86     public String getDesigns(@RequestBody String getDesignsRequest, @RequestHeader HttpHeaders requestHeader) throws UnknownHostException {
87         HttpEntity<String> entity = getStringHttpEntity(getDesignsRequest, requestHeader);
88         HttpClient httpClient = HttpClientBuilder.create().build();
89         ClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
90         restTemplate.setRequestFactory(factory);
91         String getDesignsResponse = restTemplate.postForObject(getUrl("getDesigns"), entity, String.class);
92         return getDesignsResponse;
93     }
94
95     @ApiOperation(value = "Test VNF", response = CdtController.class)
96     @ApiResponses(value = {
97             @ApiResponse(code = 200, message = "OK"),
98             @ApiResponse(code = 404, message = "The resource not found")
99     })
100     @RequestMapping(value = "/testVnf", method = RequestMethod.POST)
101     @CrossOrigin(origins = "*", allowedHeaders = "*")
102     public String testVnf(@RequestParam String urlAction, @RequestBody String testVnf, @RequestHeader HttpHeaders requestHeader) throws UnknownHostException {
103         HttpEntity<String> entity = getStringHttpEntity(testVnf, requestHeader);
104         String testVnfResponse = restTemplate.postForObject(getUrl("testVnf")+urlAction, entity, String.class);
105         return testVnfResponse;
106     }
107
108     @ApiOperation(value = "Check status of submitted Test", response = CdtController.class)
109     @ApiResponses(value = {
110             @ApiResponse(code = 200, message = "OK"),
111             @ApiResponse(code = 404, message = "The resource not found")
112     })
113     @RequestMapping(value = "/checkTestStatus", method = RequestMethod.POST)
114     @CrossOrigin(origins = "*", allowedHeaders = "*")
115     public String checkTestStatus(@RequestBody String checkTestStatusRequest, @RequestHeader HttpHeaders requestHeader) throws UnknownHostException {
116         HttpEntity<String> entity = getStringHttpEntity(checkTestStatusRequest, requestHeader);
117         String checkTestStatusResponse = restTemplate.postForObject(getUrl("checkTestStatus"), entity, String.class);
118         return checkTestStatusResponse;
119     }
120
121     @ApiOperation(value = "Validate a template which is being uploaded", response = CdtController.class)
122     @ApiResponses(value = {
123             @ApiResponse(code = 200, message = "OK"),
124             @ApiResponse(code = 404, message = "The resource not found")
125     })
126     @RequestMapping(value = "/validateTemplate", method = RequestMethod.POST)
127     @CrossOrigin(origins = "*", allowedHeaders = "*")
128     public String validateTemplate(@RequestBody String validateTemplateRequest, @RequestHeader HttpHeaders requestHeader) throws UnknownHostException {
129         HttpEntity<String> entity = getStringHttpEntity(validateTemplateRequest, requestHeader);
130         String validateTemplateResponse = restTemplate.postForObject(getUrl("validateTemplate"), entity, String.class);
131         return validateTemplateResponse;
132     }
133
134     private HttpEntity<String> getStringHttpEntity(@RequestBody String getDesignsRequest, @RequestHeader HttpHeaders requestHeader) {
135
136         HttpHeaders headers = new HttpHeaders();
137         if(requestHeader.containsKey("authorization")) {
138           List<String> headerAuthValue = requestHeader.get("authorization");
139           if(headerAuthValue != null && headerAuthValue.size() > 0) {
140               headers.set("authorization", headerAuthValue.get(0));
141           }
142       }
143         headers.setAccessControlAllowCredentials(true);
144         headers.setContentType(MediaType.APPLICATION_JSON);
145         return new HttpEntity<String>(getDesignsRequest, headers);
146     }
147
148     private String getUrl(String ApiName) throws UnknownHostException {
149
150         switch (ApiName) {
151             case "getDesigns":
152                 urlAddress = "http://" + restConfHostname + ":" + restConfPort + "/restconf/operations/design-services:dbservice";
153                 break;
154             case "testVnf":
155                 urlAddress = "http://" + restConfHostname + ":" + restConfPort + "/restconf/operations/appc-provider-lcm:";
156                 break;
157             case "checkTestStatus":
158                 urlAddress = "http://" + restConfHostname + ":" + restConfPort + "/restconf/operations/appc-provider-lcm:action-status";
159                 break;
160             case "validateTemplate":
161                 urlAddress = "http://" + restConfHostname + ":" + restConfPort + "/restconf/operations/design-services:validator";
162                 break;
163             default:
164                 logger.error("URI not resolved because Api call not supported ");
165         }
166         logger.info("Calling Endpoint..... " + urlAddress);
167         return urlAddress;
168     }
169 }