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
9 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
18 package org.onap.so.multicloudsimulator.controller;
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;
30 import org.springframework.http.ResponseEntity;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.web.bind.annotation.PathVariable;
34 import org.springframework.web.bind.annotation.RequestBody;
35 import org.springframework.web.bind.annotation.RequestHeader;
36 import org.springframework.web.bind.annotation.RequestParam;
37 import org.springframework.web.bind.annotation.RestController;
38 import org.springframework.web.bind.annotation.PostMapping;
39 import org.springframework.web.bind.annotation.GetMapping;
41 import java.io.IOException;
42 import java.io.InputStream;
44 import static org.onap.so.multicloudsimulator.utils.Constants.BASE_URL;
47 public class MultiCloudController {
49 public static final String X_HTTP_METHOD_OVERRIDE = "X-HTTP-Method-Override";
50 private static final Logger LOGGER = LoggerFactory.getLogger(MultiCloudController.class);
52 @PostMapping("/v1/instance")
53 public ResponseEntity<?> createInstance(@RequestBody MulticloudInstanceRequest req) {
54 LOGGER.info("MultiCloud createInstance ");
55 final InstanceResponse InstanceResponse = new InstanceResponse();
57 LOGGER.info("Calling v1 instance method");
58 return ResponseEntity.ok(req);
61 @GetMapping(value = BASE_URL + "/{cloud-owner}/{cloud-region-id}/infra_workload",
62 produces = { MediaType.APPLICATION_JSON })
63 public ResponseEntity<?> getInstance(@PathVariable("cloud-owner") String cloudOwner,
64 @PathVariable("cloud-region-id") String cloudRegionId,
65 @RequestParam(value = "depth", required = false, defaultValue = "0") Integer depth,
66 @RequestParam(name = "format", required = false) final String name, final HttpServletRequest request)
69 LOGGER.info("found CloudOwner {}", cloudOwner);
70 LOGGER.info("found cloudRegionId {}", cloudRegionId);
71 LOGGER.info("found name {}", name);
72 final InputStream instanceOutput = InstanceOutput.getInstance();
73 final String output = IOUtils.toString(instanceOutput, "utf-8");
75 return ResponseEntity.ok(output);
78 @PostMapping(value = BASE_URL + "/{cloud-owner}/{cloud-region-id}/infra_workload",
79 consumes = { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML },
80 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 {
87 LOGGER.info("Calling postCreateInstance");
88 inputRequest.setWorkloadStatusReason(null);
90 inputRequest.setWorkloadId("sad_sammet");
91 inputRequest.setTemplateType("heat");
92 inputRequest.setWorkloadStatus("CREATE_COMPLETE");
94 return ResponseEntity.status(201).body(inputRequest);
97 @GetMapping(value = BASE_URL + "/{cloud-owner}/{cloud-region-id}/infra_workload/{workload-id}",
98 produces = {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)
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");
112 return ResponseEntity.ok(output);
115 @PostMapping(value = BASE_URL + "/{cloud-owner}/{cloud-region-id}/infra_workload/{workload-id}",
116 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML },
117 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 {
124 LOGGER.info("Calling postCreateInstanceName");
126 return ResponseEntity.status(405).build();