aboutsummaryrefslogtreecommitdiffstats
path: root/plans/so/integration-etsi-testing/so-simulators/multicloud-simulator/src/main/java/org/onap/so/multicloudsimulator/controller/MultiCloudController.java
blob: b0e13362edb676889944bb668d6ce494e5e70925 (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
170
171
package org.onap.so.multicloudsimulator.controller;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.MediaType;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.onap.so.multicloudsimulator.beans.InstanceResponse;
import org.onap.so.multicloudsimulator.beans.MulticloudInstanceRequest;
import org.onap.so.multicloudsimulator.beans.MulticloudCreateResponse;
import org.onap.so.multicloudsimulator.beans.MulticloudRequest;
import org.onap.so.openstack.beans.HeatStatus;

import org.springframework.http.ResponseEntity;

import org.springframework.web.bind.annotation.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URI;

import static org.onap.so.multicloudsimulator.utils.Constants.BASE_URL;
@RestController
@RequestMapping(path = BASE_URL)
public class MultiCloudController {

	public static final String X_HTTP_METHOD_OVERRIDE = "X-HTTP-Method-Override";
	private static final Logger LOGGER = LoggerFactory.getLogger(MultiCloudController.class);
	public MulticloudCreateResponse multicloudCreateResponse = new MulticloudCreateResponse();

	@PostMapping(value="/v1/instance")
	public ResponseEntity<?> createInstance(@RequestBody MulticloudInstanceRequest req){
	   System.out.println("MultiCloud createInstance ");
		InstanceResponse InstanceResponse = new InstanceResponse();
		
		return ResponseEntity.ok(InstanceResponse);
	}
	
	@GetMapping(value = "/{cloud-owner}/{cloud-region-id}/infra_workload", produces = {
			MediaType.APPLICATION_JSON })
    public ResponseEntity<?> getInstance(
		@PathVariable("cloud-owner") String cloudOwner, @PathVariable("cloud-region-id") String cloudRegionId,
		@RequestParam(value = "depth", required = false, defaultValue = "0") Integer depth,
		@RequestParam(name = "format", required = false) final String name, final HttpServletRequest request) throws IOException {
		
		LOGGER.info("found CloudOwner {} in cache", cloudOwner);
		LOGGER.info("found cloudRegionId {} in cache", cloudRegionId);
		LOGGER.info("found name {} in cache", name);
			JSONObject json = new JSONObject();

			json.put("template_type", "heat");
			json.put("workload_id", "");
			json.put("workload_status", "GET_COMPLETE");
			JSONObject workload = new JSONObject();
			workload.put("stacks", HeatStatus.NOTFOUND);
			json.put("workload_status_reason", workload);

			return ResponseEntity.ok(json);
	}

	@PostMapping(value = "/{cloud-owner}/{cloud-region-id}/infra_workload",
			consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
			produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
	public ResponseEntity<?> postCreateInstance(
			@RequestBody final MulticloudRequest inputRequest, @PathVariable("cloud-owner") final String cloudOwner,
			@PathVariable("cloud-region-id") final String cloudRegionId,
			@RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String xHttpHeaderOverride,
			final HttpServletRequest request) throws IOException {

			LOGGER.info("input request {}: ",inputRequest.toString());
		String input = "{\n" +
				"   \"template_type\": \"heat\",\n" +
				"   \"workload_id\": \"sad_sammet\",\n" +
				"   \"template_response\": [\n" +
				"      {\n" +
				"         \"GVK\": {\n" +
				"            \"Group\": \"k8s.plugin.opnfv.org\",\n" +
				"            \"Version\": \"v1alpha1\",\n" +
				"            \"Kind\": \"Network\"\n" +
				"         },\n" +
				"         \"Name\": \"k8s-region-2-onap-nf-20210120t221126760z-management-network\"\n" +
				"      },\n" +
				"      {\n" +
				"         \"GVK\": {\n" +
				"            \"Group\": \"k8s.plugin.opnfv.org\",\n" +
				"            \"Version\": \"v1alpha1\",\n" +
				"            \"Kind\": \"Network\"\n" +
				"         },\n" +
				"         \"Name\": \"k8s-region-2-onap-nf-20210120t221126760z-protected-network\"\n" +
				"      },\n" +
				"      {\n" +
				"         \"GVK\": {\n" +
				"            \"Group\": \"k8s.plugin.opnfv.org\",\n" +
				"            \"Version\": \"v1alpha1\",\n" +
				"            \"Kind\": \"Network\"\n" +
				"         },\n" +
				"         \"Name\": \"k8s-region-2-onap-nf-20210120t221126760z-unprotected-network\"\n" +
				"      },\n" +
				"      {\n" +
				"         \"GVK\": {\n" +
				"            \"Group\": \"k8s.cni.cncf.io\",\n" +
				"            \"Version\": \"v1\",\n" +
				"            \"Kind\": \"NetworkAttachmentDefinition\"\n" +
				"         },\n" +
				"         \"Name\": \"k8s-region-2-onap-nf-20210120t221126760z-ovn-nat\"\n" +
				"      }\n" +
				"   ],\n" +
				"   \"workload_status\": \"CREATE_COMPLETE\",\n" +
				"   \"workload_status_reason\": \"test\"\n" +
				"}";

		ObjectMapper objectMapper = new ObjectMapper();
		JSONObject workload = new JSONObject();

		workload.put("stack",true);

		JsonNode jsonNode = objectMapper.readTree(workload.toJSONString());
		MulticloudCreateResponse multiResponse = objectMapper.readValue(input, MulticloudCreateResponse.class);
		multiResponse.setWorkloadStatusReason(null);

		LOGGER.info("workload reason: {}",multiResponse.getWorkloadStatusReason());
		multiResponse.setWorkloadId("sad_sammet");
		multiResponse.setTemplateType("heat");
		multiResponse.setWorkloadStatus("CREATE_COMPLETE");

		return ResponseEntity.status(201).body(multiResponse);
	}

	@GetMapping(value = "/{cloud-owner}/{cloud-region-id}/infra_workload/{workload-id}", produces = {
			MediaType.APPLICATION_JSON })
	public ResponseEntity<?> getInstanceName(
			@PathVariable("cloud-owner") String cloudOwner, @PathVariable("cloud-region-id") String cloudRegionId,
			@PathVariable("workload-id") String workloadId,
			@RequestParam(value = "depth", required = false, defaultValue = "0") Integer depth,
			@RequestParam(name = "format", required = false) final String name, final HttpServletRequest request) throws IOException {

		LOGGER.info("Calling getInstanceName");
		LOGGER.info("found CloudOwner {} in cache", cloudOwner);
		LOGGER.info("found cloudRegionId {} in cache", cloudRegionId);
		LOGGER.info("found name {} in cache", name);
		JSONObject json = new JSONObject();

			json.put("template_type", "heat");
			json.put("workload_id", "sad_sammet");
			json.put("workload_status", "CREATE_COMPLETE");
			JSONObject workload = new JSONObject();
			workload.put("stacks", true);
			json.put("workload_status_reason", null);

			return ResponseEntity.ok(json);
	}

	@PostMapping(value = "/{cloud-owner}/{cloud-region-id}/infra_workload/{workload-id}",
			consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
			produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
	public ResponseEntity<?> postCreateInstanceName(
			@RequestBody final MulticloudRequest inputRequest, @PathVariable("cloud-owner") final String cloudOwner,
			@PathVariable("workload-id") String workloadId,
			@PathVariable("cloud-region-id") final String cloudRegionId,
			@RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String xHttpHeaderOverride,
			final HttpServletRequest request) throws IOException {

			LOGGER.info("Calling postCreateInstanceName");

			return ResponseEntity.status(405).build();
	}
}