1 package org.onap.so.multicloudsimulator.controller;
3 import javax.servlet.http.HttpServletRequest;
4 import javax.ws.rs.core.MediaType;
6 import com.fasterxml.jackson.databind.JsonNode;
7 import com.fasterxml.jackson.databind.ObjectMapper;
8 import org.json.simple.JSONArray;
9 import org.json.simple.JSONObject;
10 import org.onap.so.multicloudsimulator.beans.InstanceResponse;
11 import org.onap.so.multicloudsimulator.beans.MulticloudInstanceRequest;
12 import org.onap.so.multicloudsimulator.beans.MulticloudCreateResponse;
13 import org.onap.so.multicloudsimulator.beans.MulticloudRequest;
14 import org.onap.so.openstack.beans.HeatStatus;
16 import org.springframework.http.ResponseEntity;
18 import org.springframework.web.bind.annotation.*;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 import java.io.IOException;
26 import static org.onap.so.multicloudsimulator.utils.Constants.BASE_URL;
28 @RequestMapping(path = BASE_URL)
29 public class MultiCloudController {
31 public static final String X_HTTP_METHOD_OVERRIDE = "X-HTTP-Method-Override";
32 private static final Logger LOGGER = LoggerFactory.getLogger(MultiCloudController.class);
33 public MulticloudCreateResponse multicloudCreateResponse = new MulticloudCreateResponse();
35 @PostMapping(value="/v1/instance")
36 public ResponseEntity<?> createInstance(@RequestBody MulticloudInstanceRequest req){
37 System.out.println("MultiCloud createInstance ");
38 InstanceResponse InstanceResponse = new InstanceResponse();
40 return ResponseEntity.ok(InstanceResponse);
43 @GetMapping(value = "/{cloud-owner}/{cloud-region-id}/infra_workload", produces = {
44 MediaType.APPLICATION_JSON })
45 public ResponseEntity<?> getInstance(
46 @PathVariable("cloud-owner") String cloudOwner, @PathVariable("cloud-region-id") String cloudRegionId,
47 @RequestParam(value = "depth", required = false, defaultValue = "0") Integer depth,
48 @RequestParam(name = "format", required = false) final String name, final HttpServletRequest request) throws IOException {
50 LOGGER.info("found CloudOwner {} in cache", cloudOwner);
51 LOGGER.info("found cloudRegionId {} in cache", cloudRegionId);
52 LOGGER.info("found name {} in cache", name);
53 JSONObject json = new JSONObject();
55 json.put("template_type", "heat");
56 json.put("workload_id", "");
57 json.put("workload_status", "GET_COMPLETE");
58 JSONObject workload = new JSONObject();
59 workload.put("stacks", HeatStatus.NOTFOUND);
60 json.put("workload_status_reason", workload);
62 return ResponseEntity.ok(json);
65 @PostMapping(value = "/{cloud-owner}/{cloud-region-id}/infra_workload",
66 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
67 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
68 public ResponseEntity<?> postCreateInstance(
69 @RequestBody final MulticloudRequest inputRequest, @PathVariable("cloud-owner") final String cloudOwner,
70 @PathVariable("cloud-region-id") final String cloudRegionId,
71 @RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String xHttpHeaderOverride,
72 final HttpServletRequest request) throws IOException {
74 LOGGER.info("input request {}: ",inputRequest.toString());
75 String input = "{\n" +
76 " \"template_type\": \"heat\",\n" +
77 " \"workload_id\": \"sad_sammet\",\n" +
78 " \"template_response\": [\n" +
81 " \"Group\": \"k8s.plugin.opnfv.org\",\n" +
82 " \"Version\": \"v1alpha1\",\n" +
83 " \"Kind\": \"Network\"\n" +
85 " \"Name\": \"k8s-region-2-onap-nf-20210120t221126760z-management-network\"\n" +
89 " \"Group\": \"k8s.plugin.opnfv.org\",\n" +
90 " \"Version\": \"v1alpha1\",\n" +
91 " \"Kind\": \"Network\"\n" +
93 " \"Name\": \"k8s-region-2-onap-nf-20210120t221126760z-protected-network\"\n" +
97 " \"Group\": \"k8s.plugin.opnfv.org\",\n" +
98 " \"Version\": \"v1alpha1\",\n" +
99 " \"Kind\": \"Network\"\n" +
101 " \"Name\": \"k8s-region-2-onap-nf-20210120t221126760z-unprotected-network\"\n" +
105 " \"Group\": \"k8s.cni.cncf.io\",\n" +
106 " \"Version\": \"v1\",\n" +
107 " \"Kind\": \"NetworkAttachmentDefinition\"\n" +
109 " \"Name\": \"k8s-region-2-onap-nf-20210120t221126760z-ovn-nat\"\n" +
112 " \"workload_status\": \"CREATE_COMPLETE\",\n" +
113 " \"workload_status_reason\": \"test\"\n" +
116 ObjectMapper objectMapper = new ObjectMapper();
117 JSONObject workload = new JSONObject();
119 workload.put("stack",true);
121 JsonNode jsonNode = objectMapper.readTree(workload.toJSONString());
122 MulticloudCreateResponse multiResponse = objectMapper.readValue(input, MulticloudCreateResponse.class);
123 multiResponse.setWorkloadStatusReason(null);
125 LOGGER.info("workload reason: {}",multiResponse.getWorkloadStatusReason());
126 multiResponse.setWorkloadId("sad_sammet");
127 multiResponse.setTemplateType("heat");
128 multiResponse.setWorkloadStatus("CREATE_COMPLETE");
130 return ResponseEntity.status(201).body(multiResponse);
133 @GetMapping(value = "/{cloud-owner}/{cloud-region-id}/infra_workload/{workload-id}", produces = {
134 MediaType.APPLICATION_JSON })
135 public ResponseEntity<?> getInstanceName(
136 @PathVariable("cloud-owner") String cloudOwner, @PathVariable("cloud-region-id") String cloudRegionId,
137 @PathVariable("workload-id") String workloadId,
138 @RequestParam(value = "depth", required = false, defaultValue = "0") Integer depth,
139 @RequestParam(name = "format", required = false) final String name, final HttpServletRequest request) throws IOException {
141 LOGGER.info("Calling getInstanceName");
142 LOGGER.info("found CloudOwner {} in cache", cloudOwner);
143 LOGGER.info("found cloudRegionId {} in cache", cloudRegionId);
144 LOGGER.info("found name {} in cache", name);
145 JSONObject json = new JSONObject();
147 json.put("template_type", "heat");
148 json.put("workload_id", "sad_sammet");
149 json.put("workload_status", "CREATE_COMPLETE");
150 JSONObject workload = new JSONObject();
151 workload.put("stacks", true);
152 json.put("workload_status_reason", null);
154 return ResponseEntity.ok(json);
157 @PostMapping(value = "/{cloud-owner}/{cloud-region-id}/infra_workload/{workload-id}",
158 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
159 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
160 public ResponseEntity<?> postCreateInstanceName(
161 @RequestBody final MulticloudRequest inputRequest, @PathVariable("cloud-owner") final String cloudOwner,
162 @PathVariable("workload-id") String workloadId,
163 @PathVariable("cloud-region-id") final String cloudRegionId,
164 @RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String xHttpHeaderOverride,
165 final HttpServletRequest request) throws IOException {
167 LOGGER.info("Calling postCreateInstanceName");
169 return ResponseEntity.status(405).build();