2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
5 ===================================================================
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
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.
20 ============LICENSE_END============================================
23 package org.onap.appc.cdt.service.controller;
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;
42 import java.net.UnknownHostException;
43 import java.util.Base64;
46 * Created by Amaresh Kumar on 09/May/2018.
50 @RequestMapping("/cdtService")
51 @CrossOrigin(origins = "*", allowedHeaders = "*")
52 @Api(value = "cdtService", description = "Backend service to eliminate CORS issue for CDT Tool")
53 public class CdtController {
55 private static final Logger logger = LoggerFactory.getLogger(CdtController.class);
56 private RestTemplate restTemplate = new RestTemplate();
57 private String urlAddress;
59 @Value("${restConf.backend.hostname}")
60 private String restConfHostname;
62 @Value("${restConf.backend.port}")
63 private String restConfPort;
65 @Value("${restConf.username}")
66 private String restConfUsername;
68 @Value("${restConf.password}")
69 private String restConfPassword;
71 @ApiOperation(value = "Return All Test Data for a given user", response = CdtController.class)
72 @ApiResponses(value = {
73 @ApiResponse(code = 200, message = "OK"),
74 @ApiResponse(code = 404, message = "The resource not found")
76 @RequestMapping(value = "", method = RequestMethod.GET)
77 @CrossOrigin(origins = "*", allowedHeaders = "*")
78 public String DefaultEndpoint() {
79 return "CDT Proxy Service is up and running.";
83 @ApiOperation(value = "Return All Test Data for a given user", response = CdtController.class)
84 @ApiResponses(value = {
85 @ApiResponse(code = 200, message = "OK"),
86 @ApiResponse(code = 404, message = "The resource not found")
88 @RequestMapping(value = "/getDesigns", method = RequestMethod.POST)
89 @CrossOrigin(origins = "*", allowedHeaders = "*")
90 public String getDesigns(@RequestBody String getDesignsRequest) throws UnknownHostException {
91 HttpEntity<String> entity = getStringHttpEntity(getDesignsRequest);
92 HttpClient httpClient = HttpClientBuilder.create().build();
93 ClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
94 restTemplate.setRequestFactory(factory);
95 String getDesignsResponse = restTemplate.postForObject(getUrl("getDesigns"), entity, String.class);
96 return getDesignsResponse;
99 @ApiOperation(value = "Test VNF", response = CdtController.class)
100 @ApiResponses(value = {
101 @ApiResponse(code = 200, message = "OK"),
102 @ApiResponse(code = 404, message = "The resource not found")
104 @RequestMapping(value = "/testVnf", method = RequestMethod.POST)
105 @CrossOrigin(origins = "*", allowedHeaders = "*")
106 public String testVnf(@RequestParam String urlAction, @RequestBody String testVnf) throws UnknownHostException {
107 HttpEntity<String> entity = getStringHttpEntity(testVnf);
108 String testVnfResponse = restTemplate.postForObject(getUrl("testVnf")+urlAction, entity, String.class);
109 return testVnfResponse;
112 @ApiOperation(value = "Check status of submitted Test", response = CdtController.class)
113 @ApiResponses(value = {
114 @ApiResponse(code = 200, message = "OK"),
115 @ApiResponse(code = 404, message = "The resource not found")
117 @RequestMapping(value = "/checkTestStatus", method = RequestMethod.POST)
118 @CrossOrigin(origins = "*", allowedHeaders = "*")
119 public String checkTestStatus(@RequestBody String checkTestStatusRequest) throws UnknownHostException {
120 HttpEntity<String> entity = getStringHttpEntity(checkTestStatusRequest);
121 String checkTestStatusResponse = restTemplate.postForObject(getUrl("checkTestStatus"), entity, String.class);
122 return checkTestStatusResponse;
125 @ApiOperation(value = "Validate a template which is being uploaded", response = CdtController.class)
126 @ApiResponses(value = {
127 @ApiResponse(code = 200, message = "OK"),
128 @ApiResponse(code = 404, message = "The resource not found")
130 @RequestMapping(value = "/validateTemplate", method = RequestMethod.POST)
131 @CrossOrigin(origins = "*", allowedHeaders = "*")
132 public String validateTemplate(@RequestBody String validateTemplateRequest) throws UnknownHostException {
133 HttpEntity<String> entity = getStringHttpEntity(validateTemplateRequest);
134 String validateTemplateResponse = restTemplate.postForObject(getUrl("validateTemplate"), entity, String.class);
135 return validateTemplateResponse;
138 private HttpEntity<String> getStringHttpEntity(@RequestBody String getDesignsRequest) {
139 HttpHeaders headers = new HttpHeaders();
140 headers.setAccessControlAllowCredentials(true);
141 headers.setContentType(MediaType.APPLICATION_JSON);
142 String planCredentials = restConfUsername + ":" + restConfPassword;
143 String base64Credentails = Base64.getEncoder().encodeToString(planCredentials.getBytes());
144 headers.set("Authorization", "Basic " + base64Credentails);
145 return new HttpEntity<String>(getDesignsRequest, headers);
148 private String getUrl(String ApiName) throws UnknownHostException {
152 urlAddress = "http://" + restConfHostname + ":" + restConfPort + "/restconf/operations/design-services:dbservice";
155 urlAddress = "http://" + restConfHostname + ":" + restConfPort + "/restconf/operations/appc-provider-lcm:";
157 case "checkTestStatus":
158 urlAddress = "http://" + restConfHostname + ":" + restConfPort + "/restconf/operations/appc-provider-lcm:action-status";
160 case "validateTemplate":
161 urlAddress = "http://" + restConfHostname + ":" + restConfPort + "/restconf/operations/design-services:validator";
164 logger.error("URI not resolved because Api call not supported ");
166 logger.info("Calling Endpoint..... " + urlAddress);