aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/java/org/openecomp/sdc/be/externalapi/servlet/AssetsDataServlet.java
blob: 1b8d6fdb7533277dd07c458982c4bb8fd719a9c6 (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
298
299
300
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * 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.sdc.be.externalapi.servlet;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

import javax.inject.Singleton;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
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.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.http.NameValuePair;
import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic;
import org.openecomp.sdc.be.components.impl.ElementBusinessLogic;
import org.openecomp.sdc.be.config.BeEcompErrorManager;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.FilterKeyEnum;
import org.openecomp.sdc.be.externalapi.servlet.representation.AssetMetadata;
import org.openecomp.sdc.be.impl.WebAppContextWrapper;
import org.openecomp.sdc.be.model.Component;
import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
import org.openecomp.sdc.be.servlets.BeGenericServlet;
import org.openecomp.sdc.be.servlets.RepresentationUtils;
import org.openecomp.sdc.common.api.Constants;
import org.openecomp.sdc.common.datastructure.AuditingFieldsKeysEnum;
import org.openecomp.sdc.common.util.GeneralUtility;
import org.openecomp.sdc.exception.ResponseFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;

import com.jcabi.aspects.Loggable;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import com.wordnik.swagger.annotations.ApiResponse;
import com.wordnik.swagger.annotations.ApiResponses;

import fj.data.Either;

@Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
@Path("/v1/catalog")
@Singleton
public class AssetsDataServlet extends BeGenericServlet {

	@Context
	private HttpServletRequest request;

	private AssetMetadataConverter assetMetadataUtils;
	private static Logger log = LoggerFactory.getLogger(AssetsDataServlet.class.getName());

	@GET
	@Path("/{assetType}")
	@Produces(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "Fetch list of assets", httpMethod = "GET", notes = "Returns list of assets", response = Response.class)
	@ApiResponses(value = { @ApiResponse(code = 200, message = "Assets Fetched"), @ApiResponse(code = 400, message = "Invalid content / Missing content"), @ApiResponse(code = 401, message = "Authorization required"),
			@ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Asset not found") })
	public Response getAssetList(@PathParam("assetType") final String assetType, @QueryParam("category") String category, @QueryParam("subCategory") String subCategory, @QueryParam("distributionStatus") String distributionStatus) {

		Response response = null;
		ResponseFormat responseFormat = null;
		String instanceIdHeader = request.getHeader(Constants.X_ECOMP_INSTANCE_ID_HEADER);
		String query = request.getQueryString();
		String requestURI = request.getRequestURI();
		String url = request.getMethod() + " " + requestURI;
		log.debug("Start handle request of {}", url);
		String serverBaseURL = request.getRequestURL().toString();

		AuditingActionEnum auditingActionEnum = query == null ? AuditingActionEnum.GET_ASSET_LIST : AuditingActionEnum.GET_FILTERED_ASSET_LIST;

		EnumMap<AuditingFieldsKeysEnum, Object> additionalParam = new EnumMap<AuditingFieldsKeysEnum, Object>(AuditingFieldsKeysEnum.class);
		additionalParam.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_CONSUMER_ID, instanceIdHeader);
		additionalParam.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_RESOURCE_URL, query == null ? requestURI : requestURI + "?" + query);

		// Mandatory
		if (instanceIdHeader == null || instanceIdHeader.isEmpty()) {
			log.debug("getAssetList: Missing X-ECOMP-InstanceID header");
			responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
			getComponentsUtils().auditExternalGetAsset(responseFormat, auditingActionEnum, additionalParam);
			return buildErrorResponse(responseFormat);
		}

		try {
			ServletContext context = request.getSession().getServletContext();
			ElementBusinessLogic elementLogic = getElementBL(context);

			getAssetUtils(context);
			Map<FilterKeyEnum, String> filters = new HashMap<FilterKeyEnum, String>();

			if (category != null) {
				filters.put(FilterKeyEnum.CATEGORY, category);
			}
			if (subCategory != null) {
				filters.put(FilterKeyEnum.SUB_CATEGORY, subCategory);
			}
			if (distributionStatus != null) {
				filters.put(FilterKeyEnum.DISTRIBUTION_STATUS, distributionStatus);
			}

			Either<List<? extends Component>, ResponseFormat> assetTypeData = elementLogic.getFilteredCatalogComponents(assetType, filters, query);

			if (assetTypeData.isRight()) {
				log.debug("getAssetList: Asset Fetching Failed");
				responseFormat = assetTypeData.right().value();
				getComponentsUtils().auditExternalGetAsset(responseFormat, auditingActionEnum, additionalParam);
				return buildErrorResponse(responseFormat);
			} else {
				log.debug("getAssetList: Asset Fetching Success");
				Either<List<? extends AssetMetadata>, ResponseFormat> resMetadata = assetMetadataUtils.convertToAssetMetadata(assetTypeData.left().value(), serverBaseURL, false);
				if (resMetadata.isRight()) {
					log.debug("getAssetList: Asset conversion Failed");
					responseFormat = resMetadata.right().value();
					getComponentsUtils().auditExternalGetAsset(responseFormat, auditingActionEnum, additionalParam);
					return buildErrorResponse(responseFormat);
				}
				Object result = RepresentationUtils.toRepresentation(resMetadata.left().value());
				responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
				getComponentsUtils().auditExternalGetAsset(responseFormat, auditingActionEnum, additionalParam);

				response = buildOkResponse(responseFormat, result);
				return response;
			}
		} catch (Exception e) {
			BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Fetch filtered list of assets");
			log.debug("getAssetList: Fetch list of assets failed with exception", e);
			return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
		}
	}

	@GET
	@Path("/{assetType}/{uuid}/metadata")
	@Produces(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "Fetch metadata of asset by uuid", httpMethod = "GET", notes = "Returns metadata of asset", response = Response.class)
	@ApiResponses(value = { @ApiResponse(code = 200, message = "Assets Fetched"), @ApiResponse(code = 401, message = "Authorization required"), @ApiResponse(code = 403, message = "Restricted operation"),
			@ApiResponse(code = 404, message = "Asset not found") })
	public Response getAssetListByUuid(@PathParam("assetType") final String assetType, @PathParam("uuid") final String uuid, @Context final HttpServletRequest request) {

		Response response = null;
		ResponseFormat responseFormat = null;
		String instanceIdHeader = request.getHeader(Constants.X_ECOMP_INSTANCE_ID_HEADER);
		AuditingActionEnum auditingActionEnum = AuditingActionEnum.GET_ASSET_METADATA;
		String requestURI = request.getRequestURI();
		String url = request.getMethod() + " " + requestURI;
		log.debug("Start handle request of {}", url);
		String serverBaseURL = request.getRequestURL().toString();

		EnumMap<AuditingFieldsKeysEnum, Object> additionalParam = new EnumMap<AuditingFieldsKeysEnum, Object>(AuditingFieldsKeysEnum.class);
		ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType);
		additionalParam.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_CONSUMER_ID, instanceIdHeader);
		additionalParam.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_RESOURCE_URL, requestURI);
		additionalParam.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_TYPE, componentType.getValue());
		additionalParam.put(AuditingFieldsKeysEnum.AUDIT_SERVICE_INSTANCE_ID, uuid);

		// Mandatory
		if (instanceIdHeader == null || instanceIdHeader.isEmpty()) {
			log.debug("getAssetList: Missing X-ECOMP-InstanceID header");
			responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
			getComponentsUtils().auditExternalGetAsset(responseFormat, auditingActionEnum, additionalParam);
			return buildErrorResponse(responseFormat);
		}

		try {
			ServletContext context = request.getSession().getServletContext();
			ElementBusinessLogic elementLogic = getElementBL(context);
			getAssetUtils(context);

			Either<List<? extends Component>, ResponseFormat> assetTypeData = elementLogic.getCatalogComponentsByUuidAndAssetType(assetType, uuid);

			if (assetTypeData.isRight()) {
				log.debug("getAssetList: Asset Fetching Failed");
				responseFormat = assetTypeData.right().value();
				getComponentsUtils().auditExternalGetAsset(responseFormat, auditingActionEnum, additionalParam);

				return buildErrorResponse(responseFormat);
			} else {
				log.debug("getAssetList: Asset Fetching Success");
				additionalParam.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_NAME, assetTypeData.left().value().iterator().next().getName());
				Either<List<? extends AssetMetadata>, ResponseFormat> resMetadata = assetMetadataUtils.convertToAssetMetadata(assetTypeData.left().value(), serverBaseURL, true);
				if (resMetadata.isRight()) {
					log.debug("getAssetList: Asset conversion Failed");
					responseFormat = resMetadata.right().value();
					getComponentsUtils().auditExternalGetAsset(responseFormat, auditingActionEnum, additionalParam);
					return buildErrorResponse(responseFormat);
				}
				Object result = RepresentationUtils.toRepresentation(resMetadata.left().value().iterator().next());
				responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
				getComponentsUtils().auditExternalGetAsset(responseFormat, auditingActionEnum, additionalParam);

				response = buildOkResponse(responseFormat, result);
				return response;
			}
		} catch (Exception e) {
			BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Fetch filtered list of assets");
			log.debug("getAssetList: Fetch list of assets failed with exception", e);
			return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
		}
	}

	private void getAssetUtils(ServletContext context) {
		WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
		WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
		assetMetadataUtils = webApplicationContext.getBean(AssetMetadataConverter.class);
	}

	@GET
	@Path("/{assetType}/{uuid}/toscaModel")
	@Produces(MediaType.APPLICATION_OCTET_STREAM)
	@ApiOperation(value = "Fetch asset csar", httpMethod = "GET", notes = "Returns asset csar", response = Response.class)
	@ApiResponses(value = { @ApiResponse(code = 200, message = "Asset Model Fetched"), @ApiResponse(code = 401, message = "Authorization required"), @ApiResponse(code = 403, message = "Restricted operation"),
			@ApiResponse(code = 404, message = "Asset not found") })
	public Response getToscaModel(@PathParam("uuid") final String uuid,
			@ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("assetType") final String assetType,
			@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization) {

		String url = request.getRequestURI();
		log.debug("Start handle request of {} {}", request.getMethod(), url);
		Response response = null;
		ResponseFormat responseFormat = null;
		ServletContext context = request.getSession().getServletContext();
		ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType);
		AuditingActionEnum auditingActionEnum = AuditingActionEnum.GET_TOSCA_MODEL;
		String userId = request.getHeader(Constants.X_ECOMP_INSTANCE_ID_HEADER);
		EnumMap<AuditingFieldsKeysEnum, Object> additionalParam = new EnumMap<AuditingFieldsKeysEnum, Object>(AuditingFieldsKeysEnum.class);
		additionalParam.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_CONSUMER_ID, userId);
		additionalParam.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_RESOURCE_URL, url);
		additionalParam.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_TYPE, componentType.getValue());
		additionalParam.put(AuditingFieldsKeysEnum.AUDIT_SERVICE_INSTANCE_ID, uuid);

		if (userId == null || userId.isEmpty()) {
			log.debug("getToscaModel: Missing X-ECOMP-InstanceID header");
			responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
			getComponentsUtils().auditExternalGetAsset(responseFormat, auditingActionEnum, additionalParam);
			return buildErrorResponse(responseFormat);
		}

		try {
			ComponentBusinessLogic componentBL = getComponentBL(componentType, context);

			Either<ImmutablePair<String, byte[]>, ResponseFormat> csarArtifact = componentBL.getToscaModelByComponentUuid(componentType, uuid, additionalParam);
			if (csarArtifact.isRight()) {
				responseFormat = csarArtifact.right().value();
				getComponentsUtils().auditExternalGetAsset(responseFormat, auditingActionEnum, additionalParam);
				response = buildErrorResponse(responseFormat);
			} else {
				byte[] value = csarArtifact.left().value().getRight();
				InputStream is = new ByteArrayInputStream(value);
				String contenetMD5 = GeneralUtility.calculateMD5ByByteArray(value);
				Map<String, String> headers = new HashMap<>();
				headers.put(Constants.CONTENT_DISPOSITION_HEADER, getContentDispositionValue(csarArtifact.left().value().getLeft()));
				headers.put(Constants.MD5_HEADER, contenetMD5);
				responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
				getComponentsUtils().auditExternalGetAsset(responseFormat, auditingActionEnum, additionalParam);
				response = buildOkResponse(responseFormat, is, headers);
			}
			return response;

		} catch (Exception e) {
			BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get asset tosca model");
			log.debug("falied to get asset tosca model", e);
			responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
			response = buildErrorResponse(responseFormat);
			getComponentsUtils().auditExternalGetAsset(responseFormat, auditingActionEnum, additionalParam);
			return response;
		}
	}
}