aboutsummaryrefslogtreecommitdiffstats
path: root/adapters/mso-tenant-adapter/src/main/java/org/openecomp/mso/adapters/tenant/TenantAdapterRest.java
blob: 0c9993cd206e51cf7e008af7f6a4fa34b5d97b3c (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*-
 * ============LICENSE_START=======================================================
 * OPENECOMP - MSO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file 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.openecomp.mso.adapters.tenant;


import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.HEAD;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.xml.ws.Holder;

import org.openecomp.mso.adapters.tenant.exceptions.TenantAlreadyExists;
import org.openecomp.mso.adapters.tenant.exceptions.TenantException;
import org.openecomp.mso.adapters.tenantrest.CreateTenantError;
import org.openecomp.mso.adapters.tenantrest.CreateTenantRequest;
import org.openecomp.mso.adapters.tenantrest.CreateTenantResponse;
import org.openecomp.mso.adapters.tenantrest.DeleteTenantError;
import org.openecomp.mso.adapters.tenantrest.DeleteTenantRequest;
import org.openecomp.mso.adapters.tenantrest.DeleteTenantResponse;
import org.openecomp.mso.adapters.tenantrest.QueryTenantError;
import org.openecomp.mso.adapters.tenantrest.QueryTenantResponse;
import org.openecomp.mso.adapters.tenantrest.RollbackTenantError;
import org.openecomp.mso.adapters.tenantrest.RollbackTenantRequest;
import org.openecomp.mso.adapters.tenantrest.RollbackTenantResponse;
import org.openecomp.mso.adapters.tenantrest.TenantRollback;
import org.openecomp.mso.logger.MsoLogger;
import org.openecomp.mso.openstack.beans.MsoTenant;
import org.openecomp.mso.openstack.exceptions.MsoExceptionCategory;

/**
 * This class services calls to the REST interface for Tenants (http://host:port/vnfs/rest/v1/tenants)
 * Both XML and JSON can be produced/consumed.  Set Accept: and Content-Type: headers appropriately.  XML is the default.
 */
@Path("/v1/tenants")
public class TenantAdapterRest {
	private static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);

	//RAA? No logging in wrappers

	@HEAD
	@GET
	@Path("/healthcheck")
	@Produces(MediaType.TEXT_HTML)
	public Response healthcheck () {
		String CHECK_HTML = "<!DOCTYPE html><html><head><meta charset=\"ISO-8859-1\"><title>Health Check</title></head><body>Application ready</body></html>";
		return Response.status (HttpServletResponse.SC_OK).entity (CHECK_HTML).build ();
	}

	/*
	URL:
	EP: http://host:8080/tenants/rest
	Resource: v1/tenants
	REQ - metadata?
	{
	"cloudSiteId": "DAN",
	"tenantName": "RAA_1",
	"failIfExists": true,
	"msoRequest": {
	"requestId": "ra1",
	"serviceInstanceId": "sa1"
	}}
	RESP-
	{
   "cloudSiteId": "DAN",
   "tenantId": "128e10b9996d43a7874f19bbc4eb6749",
   "tenantCreated": true,
   "tenantRollback":    {
      "tenantId": "128e10b9996d43a7874f19bbc4eb6749",
      "cloudId": "DAN", // RAA? cloudId instead of cloudSiteId
      "tenantCreated": true,
      "msoRequest":       {
         "requestId": "ra1",
         "serviceInstanceId": "sa1"
      }
   	 }
	}
	*/
	@POST
	@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public Response createTenant(CreateTenantRequest req) {
		LOGGER.debug("createTenant enter: " + req.toJsonString());

		String newTenantId = null;
		TenantRollback tenantRollback = new TenantRollback ();

		try {
			Holder<String> htenant = new Holder<String>();
			Holder<TenantRollback> hrollback = new Holder<TenantRollback>();
			MsoTenantAdapter impl = new MsoTenantAdapterImpl();
		    impl.createTenant(
		    	req.getCloudSiteId(),
		    	req.getTenantName(),
		    	req.getMetadata(),
		    	req.getFailIfExists(),
                req.getBackout(),
                req.getMsoRequest(),
                htenant,
                hrollback);
		    newTenantId = htenant.value;
		    tenantRollback = hrollback.value;
//			TenantAdapterCore TAImpl = new TenantAdapterCore();
//			newTenantId =  TAImpl.createTenant (req.getCloudSiteId(),
//												req.getTenantName(),
//												req.getFailIfExists(),
//												req.getBackout(),
//												req.getMetadata(),
//												req.getMsoRequest(),
//												tenantRollback);
		}
		catch (TenantAlreadyExists tae) {
			CreateTenantError exc = new CreateTenantError(tae.getMessage(), tae.getFaultInfo().getCategory(), Boolean.TRUE);
			return Response.status(HttpServletResponse.SC_NOT_IMPLEMENTED).entity(exc).build();
		}
		catch (TenantException te) {
			CreateTenantError exc = new CreateTenantError(te.getFaultInfo().getMessage(), te.getFaultInfo().getCategory(), Boolean.TRUE);
			return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(exc).build();
		}
		catch (Exception e) {
			CreateTenantError exc = new CreateTenantError(e.getMessage(), MsoExceptionCategory.INTERNAL, Boolean.TRUE);
			return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(exc).build();
		}

		CreateTenantResponse resp = new CreateTenantResponse (req.getCloudSiteId(), newTenantId, tenantRollback.getTenantCreated(), tenantRollback);
		return Response.status(HttpServletResponse.SC_OK).entity(resp).build();
	}

	/*
	URL:
	http://host:8080/tenants/rest
	Resource: v1/tenant/tennatId
	REQ:
	{"cloudSiteId": "DAN",
	"tenantId": "ca84cd3d3df44272845da554656b3ace",
	"msoRequest": {
	"requestId": "ra1",
	"serviceInstanceId": "sa1"
	}
	}
	RESP:
	{"tenantDeleted": true}
	 */
	@DELETE
	@Path("{tenantId}")
	@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public Response deleteTenant(
		@PathParam("tenantId") String tenantId,
		DeleteTenantRequest req)
	{
		boolean tenantDeleted = false;

		try {
			Holder<Boolean> deleted = new Holder<Boolean>();
			MsoTenantAdapter impl = new MsoTenantAdapterImpl();
		    impl.deleteTenant(
		    	req.getCloudSiteId(),
		    	req.getTenantId(),
		    	req.getMsoRequest(),
		    	deleted);
		    tenantDeleted = deleted.value;
		}
		catch (TenantException te) {
			DeleteTenantError exc = new DeleteTenantError(te.getFaultInfo().getMessage(), te.getFaultInfo().getCategory(), Boolean.TRUE);
			return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(exc).build();
		}
		catch (Exception e) {
			DeleteTenantError exc = new DeleteTenantError(e.getMessage(), MsoExceptionCategory.INTERNAL, Boolean.TRUE);
			return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(exc).build();
		}
		DeleteTenantResponse resp = new DeleteTenantResponse();
		resp.setTenantDeleted(tenantDeleted);
		return Response.status(HttpServletResponse.SC_OK).entity(resp).build();
	}

	/*
	URL
	EP://http://host:8080/tenants/rest
	Resource: /v1/tenants
	Params:?tenantNameOrId=RAA_1&cloudSiteId=DAN
	RESP
	{
		   "tenantId": "214b428a1f554c02935e66330f6a5409",
		   "tenantName": "RAA_1",
		   "metadata": {}
	}
	*/
	@GET
	@Path("{tenantId}")
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public Response queryTenant(
			@PathParam("tenantId") String tenantId,
//			@QueryParam("tenantNameOrId") String tenantNameOrId, //RAA? diff from doc
			@QueryParam("cloudSiteId") String cloudSiteId,
			@QueryParam("msoRequest.requestId") String requestId,
			@QueryParam("msoRequest.serviceInstanceId") String serviceInstanceId)
	{
		MsoTenant tenant = null;
		try {
			Holder<String> htenant = new Holder<String>();
			Holder<String> tenantName = new Holder<String>();
			Holder<Map<String,String>> metadata = new Holder<Map<String,String>>();
			MsoTenantAdapter impl = new MsoTenantAdapterImpl();
		    impl.queryTenant(
		    	cloudSiteId,
		    	tenantId,
		    	null,
		    	htenant,
		    	tenantName,
		    	metadata
		    	);
		    tenant = new MsoTenant(htenant.value, tenantName.value, metadata.value);
//			TenantAdapterCore TAImpl = new TenantAdapterCore();
//			MsoRequest msoReq = new MsoRequest();
//			tenant = TAImpl.queryTenant (cloudSiteId, tenantId, msoReq);
		}
		catch (TenantException te) {
			QueryTenantError exc = new QueryTenantError(te.getFaultInfo().getMessage(), te.getFaultInfo().getCategory());
			return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(exc).build();
		}
		catch (Exception e) {
			QueryTenantError exc = new QueryTenantError(e.getMessage(), MsoExceptionCategory.INTERNAL);
			return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(exc).build();
		}
		QueryTenantResponse resp = new QueryTenantResponse(tenant.getTenantId(), tenant.getTenantName(), tenant.getMetadata());
		return Response.status(HttpServletResponse.SC_OK).entity(resp).build();
	}

	/*
	URL
	EP: //http://host:8080/tenants/rest
	Resource: /v1/tenants/rollback
	REQ
	{"cloudSiteId": "DAN",
	"tenantId": "f58abb05041d4ff384d4d22d1ccd2a6c",
	"msoRequest": {
	"requestId": "ra1",
	"serviceInstanceId": "sa1"
	}
	}
	RESP:
	{"tenantDeleted": true}
	 */
	@DELETE
	@Path("")
	@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public Response rollbackTenant(
		@QueryParam("rollback") String action, // WTF?
		RollbackTenantRequest req)
	{
		try {
			MsoTenantAdapter impl = new MsoTenantAdapterImpl();
		    impl.rollbackTenant(req.getTenantRollback());
		}
		catch (TenantException te) {
			RollbackTenantError exc = new RollbackTenantError(te.getFaultInfo().getMessage(), te.getFaultInfo().getCategory(), Boolean.TRUE);
			return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(exc).build();
		}
		catch (Exception e) {
			RollbackTenantError exc = new RollbackTenantError(e.getMessage(), MsoExceptionCategory.INTERNAL, Boolean.TRUE);
			return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(exc).build();
		}

		RollbackTenantResponse resp = new RollbackTenantResponse ();
		resp.setTenantRolledback(req != null);
		return Response.status(HttpServletResponse.SC_OK).entity(resp).build();
	}
}