aboutsummaryrefslogtreecommitdiffstats
path: root/CdtProxyService/src/main/java/org/onap/appc/cdt/service/controller/CdtController.java
blob: 78a94f64aff7a6430988c643cfa86755a8f6bc3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
============LICENSE_START==========================================
===================================================================
Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
===================================================================

Unless otherwise specified, all software contained herein is licensed
under the Apache License, Version 2.0 (the License);
you may not use this software except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

============LICENSE_END============================================
*/

package org.onap.appc.cdt.service.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.net.UnknownHostException;
import java.util.Base64;

/**
 * Created by Amaresh Kumar on 09/May/2018.
 */

@RestController
@RequestMapping("/cdtService")
@CrossOrigin(origins = "*", allowedHeaders = "*")
@Api(value = "cdtService", description = "Backend service to eliminate CORS issue for CDT Tool")
public class CdtController {

    private static final Logger logger = LoggerFactory.getLogger(CdtController.class);
    private RestTemplate restTemplate = new RestTemplate();
    private String urlAddress;

    @Value("${restConf.backend.hostname}")
    private String restConfHostname;

    @Value("${restConf.backend.port}")
    private String restConfPort;

    @Value("${restConf.username}")
    private String restConfUsername;

    @Value("${restConf.password}")
    private String restConfPassword;

    @ApiOperation(value = "Return All Test Data for a given user", response = CdtController.class)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "OK"),
            @ApiResponse(code = 404, message = "The resource not found")
    })
    @RequestMapping(value = "", method = RequestMethod.GET)
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public String DefaultEndpoint()  {
        return  "CDT Proxy Service is up and running.";

    }

    @ApiOperation(value = "Return All Test Data for a given user", response = CdtController.class)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "OK"),
            @ApiResponse(code = 404, message = "The resource not found")
    })
    @RequestMapping(value = "/getDesigns", method = RequestMethod.POST)
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public String getDesigns(@RequestBody String getDesignsRequest) throws UnknownHostException {
        HttpEntity<String> entity = getStringHttpEntity(getDesignsRequest);
        HttpClient httpClient = HttpClientBuilder.create().build();
        ClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
        restTemplate.setRequestFactory(factory);
        String getDesignsResponse = restTemplate.postForObject(getUrl("getDesigns"), entity, String.class);
        return getDesignsResponse;
    }

    @ApiOperation(value = "Test VNF", response = CdtController.class)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "OK"),
            @ApiResponse(code = 404, message = "The resource not found")
    })
    @RequestMapping(value = "/testVnf", method = RequestMethod.POST)
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public String testVnf(@RequestParam String urlAction, @RequestBody String testVnf) throws UnknownHostException {
        HttpEntity<String> entity = getStringHttpEntity(testVnf);
        String testVnfResponse = restTemplate.postForObject(getUrl("testVnf")+urlAction, entity, String.class);
        return testVnfResponse;
    }

    @ApiOperation(value = "Check status of submitted Test", response = CdtController.class)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "OK"),
            @ApiResponse(code = 404, message = "The resource not found")
    })
    @RequestMapping(value = "/checkTestStatus", method = RequestMethod.POST)
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public String checkTestStatus(@RequestBody String checkTestStatusRequest) throws UnknownHostException {
        HttpEntity<String> entity = getStringHttpEntity(checkTestStatusRequest);
        String checkTestStatusResponse = restTemplate.postForObject(getUrl("checkTestStatus"), entity, String.class);
        return checkTestStatusResponse;
    }

    @ApiOperation(value = "Validate a template which is being uploaded", response = CdtController.class)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "OK"),
            @ApiResponse(code = 404, message = "The resource not found")
    })
    @RequestMapping(value = "/validateTemplate", method = RequestMethod.POST)
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public String validateTemplate(@RequestBody String validateTemplateRequest) throws UnknownHostException {
        HttpEntity<String> entity = getStringHttpEntity(validateTemplateRequest);
        String validateTemplateResponse = restTemplate.postForObject(getUrl("validateTemplate"), entity, String.class);
        return validateTemplateResponse;
    }

    private HttpEntity<String> getStringHttpEntity(@RequestBody String getDesignsRequest) {
        HttpHeaders headers = new HttpHeaders();
        headers.setAccessControlAllowCredentials(true);
        headers.setContentType(MediaType.APPLICATION_JSON);
        String planCredentials = restConfUsername + ":" + restConfPassword;
        String base64Credentails = Base64.getEncoder().encodeToString(planCredentials.getBytes());
        headers.set("Authorization", "Basic " + base64Credentails);
        return new HttpEntity<String>(getDesignsRequest, headers);
    }

    private String getUrl(String ApiName) throws UnknownHostException {

        switch (ApiName) {
            case "getDesigns":
                urlAddress = "http://" + restConfHostname + ":" + restConfPort + "/restconf/operations/design-services:dbservice";
                break;
            case "testVnf":
                urlAddress = "http://" + restConfHostname + ":" + restConfPort + "/restconf/operations/appc-provider-lcm:";
                break;
            case "checkTestStatus":
                urlAddress = "http://" + restConfHostname + ":" + restConfPort + "/restconf/operations/appc-provider-lcm:action-status";
                break;
            case "validateTemplate":
                urlAddress = "http://" + restConfHostname + ":" + restConfPort + "/restconf/operations/design-services:validator";
                break;
            default:
                logger.error("URI not resolved because Api call not supported ");
        }
        logger.info("Calling Endpoint..... " + urlAddress);
        return urlAddress;
    }
}