Added CSIT for Macroflow with HEAT
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / multicloud-simulator / src / main / java / org / onap / so / multicloudsimulator / controller / MultiCloudController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright 2021 Huawei Technologies Co., Ltd.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17  */
18 package org.onap.so.multicloudsimulator.controller;
19
20 import javax.servlet.http.HttpServletRequest;
21 import javax.ws.rs.core.MediaType;
22 import org.apache.commons.io.IOUtils;
23 import org.onap.so.multicloudsimulator.beans.InstanceOutput;
24 import org.onap.so.multicloudsimulator.beans.MulticloudCreateResponse;
25 import org.onap.so.multicloudsimulator.beans.MulticloudInstanceRequest;
26 import org.onap.so.multicloudsimulator.beans.InstanceResponse;
27 import org.onap.so.multicloudsimulator.beans.InstanceNameOutput;
28 import org.onap.so.multicloudsimulator.beans.MulticloudRequest;
29
30 import org.springframework.http.ResponseEntity;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.web.bind.annotation.GetMapping;
34 import org.springframework.web.bind.annotation.PathVariable;
35 import org.springframework.web.bind.annotation.PostMapping;
36 import org.springframework.web.bind.annotation.RequestBody;
37 import org.springframework.web.bind.annotation.RequestHeader;
38 import org.springframework.web.bind.annotation.RequestMapping;
39 import org.springframework.web.bind.annotation.RequestParam;
40 import org.springframework.web.bind.annotation.RestController;
41
42 import java.io.IOException;
43 import java.io.InputStream;
44
45 import static org.onap.so.multicloudsimulator.utils.Constants.BASE_URL;
46
47 @RestController
48 @RequestMapping(path = BASE_URL)
49 public class MultiCloudController {
50
51     public static final String X_HTTP_METHOD_OVERRIDE = "X-HTTP-Method-Override";
52     private static final Logger LOGGER = LoggerFactory.getLogger(MultiCloudController.class);
53
54     @PostMapping(value = "/v1/instance")
55     public ResponseEntity<?> createInstance(@RequestBody MulticloudInstanceRequest req) {
56         System.out.println("MultiCloud createInstance ");
57         final InstanceResponse InstanceResponse = new InstanceResponse();
58
59         LOGGER.info("Calling createInstance");
60         return ResponseEntity.ok(InstanceResponse);
61     }
62
63     @GetMapping(value = "/{cloud-owner}/{cloud-region-id}/infra_workload", produces = { MediaType.APPLICATION_JSON })
64     public ResponseEntity<?> getInstance(@PathVariable("cloud-owner") String cloudOwner,
65             @PathVariable("cloud-region-id") String cloudRegionId,
66             @RequestParam(value = "depth", required = false, defaultValue = "0") Integer depth,
67             @RequestParam(name = "format", required = false) final String name, final HttpServletRequest request)
68             throws Exception {
69
70         LOGGER.info("found CloudOwner {}", cloudOwner);
71         LOGGER.info("found cloudRegionId {}", cloudRegionId);
72         LOGGER.info("found name {}", name);
73         final InputStream instanceOutput = InstanceOutput.getInstance();
74         final String output = IOUtils.toString(instanceOutput, "utf-8");
75
76         return ResponseEntity.ok(output);
77     }
78
79     @PostMapping(value = "/{cloud-owner}/{cloud-region-id}/infra_workload", consumes = { MediaType.APPLICATION_JSON,
80             MediaType.APPLICATION_XML }, produces = { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
81     public ResponseEntity<?> postCreateInstance(@RequestBody final MulticloudCreateResponse inputRequest,
82             @PathVariable("cloud-owner") final String cloudOwner,
83             @PathVariable("cloud-region-id") final String cloudRegionId,
84             @RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String xHttpHeaderOverride,
85             final HttpServletRequest request) throws IOException {
86
87         LOGGER.info("Calling postCreateInstance");
88         inputRequest.setWorkloadStatusReason(null);
89
90         inputRequest.setWorkloadId("sad_sammet");
91         inputRequest.setTemplateType("heat");
92         inputRequest.setWorkloadStatus("CREATE_COMPLETE");
93
94         return ResponseEntity.status(201).body(inputRequest);
95     }
96
97     @GetMapping(value = "/{cloud-owner}/{cloud-region-id}/infra_workload/{workload-id}", produces = {
98             MediaType.APPLICATION_JSON })
99     public ResponseEntity<?> getInstanceName(@PathVariable("cloud-owner") final String cloudOwner,
100             @PathVariable("cloud-region-id") final String cloudRegionId, @PathVariable("workload-id") final String workloadId,
101             @RequestParam(value = "depth", required = false, defaultValue = "0") final Integer depth,
102             @RequestParam(name = "format", required = false) final String name, final HttpServletRequest request)
103             throws Exception {
104
105         LOGGER.info("Calling getInstanceName");
106         LOGGER.info("found CloudOwner {}", cloudOwner);
107         LOGGER.info("found cloudRegionId {}", cloudRegionId);
108         LOGGER.info("found name {}", name);
109         final InputStream instanceNameOutput = InstanceNameOutput.getInstanceName();
110         final String output = IOUtils.toString(instanceNameOutput, "utf-8");
111
112         return ResponseEntity.ok(output);
113     }
114
115     @PostMapping(value = "/{cloud-owner}/{cloud-region-id}/infra_workload/{workload-id}", consumes = {
116             MediaType.APPLICATION_JSON,
117             MediaType.APPLICATION_XML }, produces = { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
118     public ResponseEntity<?> postCreateInstanceName(@RequestBody final MulticloudRequest inputRequest,
119             @PathVariable("cloud-owner") final String cloudOwner, @PathVariable("workload-id") String workloadId,
120             @PathVariable("cloud-region-id") final String cloudRegionId,
121             @RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String xHttpHeaderOverride,
122             final HttpServletRequest request) throws IOException {
123
124         LOGGER.info("Calling postCreateInstanceName");
125
126         return ResponseEntity.status(405).build();
127     }
128 }