2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.servlets;
23 import com.fasterxml.jackson.databind.DeserializationFeature;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import com.fasterxml.jackson.databind.module.SimpleModule;
26 import com.google.common.annotations.VisibleForTesting;
27 import com.google.gson.Gson;
28 import com.google.gson.GsonBuilder;
29 import com.google.gson.reflect.TypeToken;
30 import fj.data.Either;
31 import org.json.simple.JSONArray;
32 import org.json.simple.JSONObject;
33 import org.json.simple.parser.JSONParser;
34 import org.json.simple.parser.ParseException;
35 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
36 import org.openecomp.sdc.be.components.impl.BaseBusinessLogic;
37 import org.openecomp.sdc.be.components.impl.CapabilitiesBusinessLogic;
38 import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic;
39 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
40 import org.openecomp.sdc.be.components.impl.ElementBusinessLogic;
41 import org.openecomp.sdc.be.components.impl.GenericArtifactBrowserBusinessLogic;
42 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
43 import org.openecomp.sdc.be.components.impl.InputsBusinessLogic;
44 import org.openecomp.sdc.be.components.impl.InterfaceOperationBusinessLogic;
45 import org.openecomp.sdc.be.components.impl.PolicyBusinessLogic;
46 import org.openecomp.sdc.be.components.impl.ProductBusinessLogic;
47 import org.openecomp.sdc.be.components.impl.PropertyBusinessLogic;
48 import org.openecomp.sdc.be.components.impl.RelationshipTypeBusinessLogic;
49 import org.openecomp.sdc.be.components.impl.RequirementBusinessLogic;
50 import org.openecomp.sdc.be.components.impl.ResourceBusinessLogic;
51 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
52 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
53 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
54 import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic;
55 import org.openecomp.sdc.be.components.scheduledtasks.ComponentsCleanBusinessLogic;
56 import org.openecomp.sdc.be.components.upgrade.UpgradeBusinessLogic;
57 import org.openecomp.sdc.be.config.BeEcompErrorManager;
58 import org.openecomp.sdc.be.dao.api.ActionStatus;
59 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
60 import org.openecomp.sdc.be.datatypes.enums.DeclarationTypeEnum;
61 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
62 import org.openecomp.sdc.be.ecomp.converters.AssetMetadataConverter;
63 import org.openecomp.sdc.be.impl.ComponentsUtils;
64 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
65 import org.openecomp.sdc.be.model.ComponentInstInputsMap;
66 import org.openecomp.sdc.be.model.PropertyConstraint;
67 import org.openecomp.sdc.be.model.PropertyDefinition;
68 import org.openecomp.sdc.be.model.User;
69 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
70 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation.PropertyConstraintJacksonDeserializer;
71 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
72 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
73 import org.openecomp.sdc.be.user.UserBusinessLogic;
74 import org.openecomp.sdc.common.api.Constants;
75 import org.openecomp.sdc.common.log.wrappers.Logger;
76 import org.openecomp.sdc.common.servlets.BasicServlet;
77 import org.openecomp.sdc.exception.ResponseFormat;
78 import org.springframework.web.context.WebApplicationContext;
80 import javax.servlet.ServletContext;
81 import javax.servlet.http.HttpServletRequest;
82 import javax.ws.rs.core.Context;
83 import javax.ws.rs.core.Response;
84 import javax.ws.rs.core.Response.ResponseBuilder;
85 import java.io.IOException;
86 import java.lang.reflect.Type;
87 import java.util.HashMap;
88 import java.util.Iterator;
89 import java.util.List;
91 import java.util.Map.Entry;
92 import java.util.Objects;
94 import java.util.function.Supplier;
96 public class BeGenericServlet extends BasicServlet {
98 public BeGenericServlet(UserBusinessLogic userAdminManager,
99 ComponentsUtils componentsUtils) {
100 this.userAdminManager = userAdminManager;
101 this.componentsUtils = componentsUtils;
105 protected HttpServletRequest servletRequest;
107 private static final Logger log = Logger.getLogger(BeGenericServlet.class);
109 private static final String PROPERTY_NAME_REGEX = "[\\w,\\d,_]+";
111 private UserBusinessLogic userAdminManager;
112 protected ComponentsUtils componentsUtils;
114 /******************** New error response mechanism
115 * @param requestErrorWrapper **************/
117 protected Response buildErrorResponse(ResponseFormat requestErrorWrapper) {
118 return Response.status(requestErrorWrapper.getStatus()).entity(gson.toJson(requestErrorWrapper.getRequestError())).build();
121 protected Response buildGeneralErrorResponse() {
122 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
125 protected Response buildOkResponse(Object entity) {
126 return buildOkResponseStatic(entity);
129 private static Response buildOkResponseStatic(Object entity) {
130 return Response.status(Response.Status.OK)
135 public HttpServletRequest getServletRequest() {
136 return servletRequest;
140 public void setRequestServlet(HttpServletRequest request) {this.servletRequest = request;}
142 protected Response buildOkResponse(ResponseFormat errorResponseWrapper, Object entity) {
143 return buildOkResponse(errorResponseWrapper, entity, null);
146 protected Response buildOkResponse(ResponseFormat errorResponseWrapper, Object entity, Map<String, String> additionalHeaders) {
147 int status = errorResponseWrapper.getStatus();
148 ResponseBuilder responseBuilder = Response.status(status);
149 if (entity != null) {
150 if (log.isTraceEnabled())
151 log.trace("returned entity is {}", entity.toString());
152 responseBuilder = responseBuilder.entity(entity);
154 if (additionalHeaders != null) {
155 for (Entry<String, String> additionalHeader : additionalHeaders.entrySet()) {
156 String headerName = additionalHeader.getKey();
157 String headerValue = additionalHeader.getValue();
158 if (log.isTraceEnabled())
159 log.trace("Adding header {} with value {} to the response", headerName, headerValue);
160 responseBuilder.header(headerName, headerValue);
163 return responseBuilder.build();
166 /*******************************************************************************************************/
167 protected Either<User, ResponseFormat> getUser(final HttpServletRequest request, String userId) {
170 user = getUserAdminManager(request.getSession().getServletContext()).getUser(userId, false);
171 return Either.left(user);
172 } catch (ComponentException ce) {
173 log.info("createResource method - user is not listed. userId= {}", userId);
174 ResponseFormat errorResponse = getComponentsUtils().getResponseFormat(ce);
175 user = new User("", "", userId, "", null, null);
176 getComponentsUtils().auditResource(errorResponse, user, "", AuditingActionEnum.CHECKOUT_RESOURCE);
177 return Either.right(errorResponse);
181 UserBusinessLogic getUserAdminManager(ServletContext context) {
182 return getClassFromWebAppContext(context, () -> UserBusinessLogic.class);
185 protected GenericArtifactBrowserBusinessLogic getGenericArtifactBrowserBL(ServletContext context) {
186 return getClassFromWebAppContext(context, () -> GenericArtifactBrowserBusinessLogic.class);
189 protected ResourceBusinessLogic getResourceBL(ServletContext context) {
190 return getClassFromWebAppContext(context, () -> ResourceBusinessLogic.class);
193 InterfaceOperationBusinessLogic getInterfaceOperationBL(ServletContext context) {
194 return getClassFromWebAppContext(context, () -> InterfaceOperationBusinessLogic.class);
197 protected CapabilitiesBusinessLogic getCapabilitiesBL(ServletContext context) {
198 return getClassFromWebAppContext(context, () -> CapabilitiesBusinessLogic.class);
201 protected RelationshipTypeBusinessLogic getRelationshipTypeBL(ServletContext context) {
202 return getClassFromWebAppContext(context, () -> RelationshipTypeBusinessLogic.class);
204 protected RequirementBusinessLogic getRequirementBL(ServletContext context) {
205 return getClassFromWebAppContext(context, () -> RequirementBusinessLogic.class);
207 ComponentsCleanBusinessLogic getComponentCleanerBL(ServletContext context) {
208 return getClassFromWebAppContext(context, () -> ComponentsCleanBusinessLogic.class);
211 protected ServiceBusinessLogic getServiceBL(ServletContext context) {
212 return getClassFromWebAppContext(context, () -> ServiceBusinessLogic.class);
215 ProductBusinessLogic getProductBL(ServletContext context) {
216 return getClassFromWebAppContext(context, () -> ProductBusinessLogic.class);
219 protected ArtifactsBusinessLogic getArtifactBL(ServletContext context) {
220 return getClassFromWebAppContext(context, () -> ArtifactsBusinessLogic.class);
222 protected UpgradeBusinessLogic getUpgradeBL(ServletContext context) {
223 return getClassFromWebAppContext(context, () -> UpgradeBusinessLogic.class);
226 protected ElementBusinessLogic getElementBL(ServletContext context) {
227 return getClassFromWebAppContext(context, () -> ElementBusinessLogic.class);
230 protected AssetMetadataConverter getAssetUtils(ServletContext context) {
231 return getClassFromWebAppContext(context, () -> AssetMetadataConverter.class);
234 protected LifecycleBusinessLogic getLifecycleBL(ServletContext context) {
235 return getClassFromWebAppContext(context, () -> LifecycleBusinessLogic.class);
238 <T> T getClassFromWebAppContext(ServletContext context, Supplier<Class<T>> businessLogicClassGen) {
239 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
240 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
241 return webApplicationContext.getBean(businessLogicClassGen.get());
244 GroupBusinessLogic getGroupBL(ServletContext context) {
246 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
247 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
248 return webApplicationContext.getBean(GroupBusinessLogic.class);
251 protected ComponentInstanceBusinessLogic getComponentInstanceBL(ServletContext context) {
252 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
253 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
254 return webApplicationContext.getBean(ComponentInstanceBusinessLogic.class);
257 protected ComponentsUtils getComponentsUtils() {
258 ServletContext context = this.servletRequest.getSession().getServletContext();
260 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
261 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
262 return webApplicationContext.getBean(ComponentsUtils.class);
266 * Used to support Unit Test.<br>
267 * Header Params are not supported in Unit Tests
271 String initHeaderParam(String headerValue, HttpServletRequest request, String headerName) {
273 if (headerValue != null) {
274 retValue = headerValue;
276 retValue = request.getHeader(headerName);
281 protected String getContentDispositionValue(String artifactFileName) {
282 return new StringBuilder().append("attachment; filename=\"").append(artifactFileName).append("\"").toString();
287 protected ComponentBusinessLogic getComponentBL(ComponentTypeEnum componentTypeEnum, ServletContext context) {
288 ComponentBusinessLogic businessLogic;
289 switch (componentTypeEnum) {
291 businessLogic = getResourceBL(context);
294 businessLogic = getServiceBL(context);
297 businessLogic = getProductBL(context);
299 case RESOURCE_INSTANCE:
300 businessLogic = getResourceBL(context);
303 BeEcompErrorManager.getInstance().logBeSystemError("getComponentBL");
304 throw new IllegalArgumentException("Illegal component type:" + componentTypeEnum.getValue());
306 return businessLogic;
309 <T> T convertJsonToObjectOfClass(String json, Class<T> clazz) {
311 ObjectMapper mapper = new ObjectMapper()
312 .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
313 .configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
315 log.trace("Starting to convert json to object. Json=\n{}", json);
317 SimpleModule module = new SimpleModule("customDeserializationModule");
318 module.addDeserializer(PropertyConstraint.class, new PropertyConstraintJacksonDeserializer());
319 mapper.registerModule(module);
321 object = mapper.readValue(json, clazz);
322 if (object != null) {
325 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("convertJsonToObject");
326 log.debug("The object of class {} is null after converting from json. ", clazz);
327 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
329 } catch (IOException e) {
330 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("convertJsonToObject");
331 log.debug("The exception {} occurred upon json to object convertation. Json=\n{}", e, json);
332 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
336 protected Either<Map<String, PropertyDefinition>, ActionStatus> getPropertyModel(String componentId, String data) {
337 JSONParser parser = new JSONParser();
340 Map<String, PropertyDefinition> properties = new HashMap<>();
341 root = (JSONObject) parser.parse(data);
343 Set entrySet = root.entrySet();
344 Iterator iterator = entrySet.iterator();
345 while (iterator.hasNext()) {
346 Entry next = (Entry) iterator.next();
347 String propertyName = (String) next.getKey();
349 if(!isPropertyNameValid(propertyName)) {
350 return Either.right(ActionStatus.INVALID_PROPERTY_NAME);
353 JSONObject value = (JSONObject) next.getValue();
354 Either<PropertyDefinition, ActionStatus> propertyDefinitionEither =
355 getPropertyDefinitionFromJson(componentId, propertyName, value);
357 if(propertyDefinitionEither.isRight()) {
358 return Either.right(propertyDefinitionEither.right().value());
361 properties.put(propertyName, propertyDefinitionEither.left().value());
364 return Either.left(properties);
365 } catch (ParseException e) {
366 log.info("Property conetnt is invalid - {}", data);
367 return Either.right(ActionStatus.INVALID_CONTENT);
371 protected boolean isPropertyNameValid(String propertyName) {
372 return Objects.nonNull(propertyName)
373 && propertyName.matches(PROPERTY_NAME_REGEX);
377 private Either<PropertyDefinition, ActionStatus> getPropertyDefinitionFromJson(String componentId, String propertyName, JSONObject value) {
378 String jsonString = value.toJSONString();
379 Either<PropertyDefinition, ActionStatus> convertJsonToObject = convertJsonToObject(jsonString, PropertyDefinition.class);
380 if (convertJsonToObject.isRight()) {
381 return Either.right(convertJsonToObject.right().value());
383 PropertyDefinition propertyDefinition = convertJsonToObject.left().value();
384 String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(componentId, propertyName);
385 propertyDefinition.setUniqueId(uniqueId);
387 return Either.left(propertyDefinition);
390 protected Either<Map<String, PropertyDefinition>, ActionStatus> getPropertiesListForUpdate(String data) {
392 Map<String, PropertyDefinition> properties = new HashMap<>();
393 JSONParser parser = new JSONParser();
397 jsonArray = (JSONArray) parser.parse(data);
398 for (Object jsonElement : jsonArray) {
399 String propertyAsString = jsonElement.toString();
400 Either<PropertyDefinition, ActionStatus> convertJsonToObject = convertJsonToObject(propertyAsString, PropertyDefinition.class);
402 if (convertJsonToObject.isRight()) {
403 return Either.right(convertJsonToObject.right().value());
406 PropertyDefinition propertyDefinition = convertJsonToObject.left().value();
407 properties.put(propertyDefinition.getName(), propertyDefinition);
410 return Either.left(properties);
411 } catch (Exception e) {
412 log.info("Property content is invalid - {}", data);
413 return Either.right(ActionStatus.INVALID_CONTENT);
419 protected String propertyToJson(Map.Entry<String, PropertyDefinition> property) {
420 JSONObject root = new JSONObject();
421 String propertyName = property.getKey();
422 PropertyDefinition propertyDefinition = property.getValue();
423 JSONObject propertyDefinitionO = getPropertyDefinitionJSONObject(propertyDefinition);
424 root.put(propertyName, propertyDefinitionO);
425 propertyDefinition.getType();
426 return root.toString();
429 private JSONObject getPropertyDefinitionJSONObject(PropertyDefinition propertyDefinition) {
431 Either<String, ActionStatus> either = convertObjectToJson(propertyDefinition);
432 if (either.isRight()) {
433 return new JSONObject();
435 String value = either.left().value();
437 JSONObject root = (JSONObject) new JSONParser().parse(value);
439 } catch (ParseException e) {
440 log.info("failed to convert input to json");
441 log.error("failed to convert to json", e);
442 return new JSONObject();
447 protected <T> Either<T, ActionStatus> convertJsonToObject(String data, Class<T> clazz) {
449 Type constraintType = new TypeToken<PropertyConstraint>() {
452 gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyOperation.PropertyConstraintDeserialiser()).create();
454 log.trace("convert json to object. json=\n {}", data);
455 t = gson.fromJson(data, clazz);
457 log.info("object is null after converting from json");
458 return Either.right(ActionStatus.INVALID_CONTENT);
460 } catch (Exception e) {
462 log.info("failed to convert from json");
463 log.error("failed to convert from json", e);
464 return Either.right(ActionStatus.INVALID_CONTENT);
466 return Either.left(t);
469 private Either<String, ActionStatus> convertObjectToJson(PropertyDefinition propertyDefinition) {
470 Type constraintType = new TypeToken<PropertyConstraint>() {
472 Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyOperation.PropertyConstraintSerialiser()).create();
474 log.trace("convert object to json. propertyDefinition= {}", propertyDefinition);
475 String json = gson.toJson(propertyDefinition);
477 log.info("object is null after converting to json");
478 return Either.right(ActionStatus.INVALID_CONTENT);
480 return Either.left(json);
481 } catch (Exception e) {
483 log.info("failed to convert to json");
484 log.debug("failed to convert fto json", e);
485 return Either.right(ActionStatus.INVALID_CONTENT);
490 protected PropertyBusinessLogic getPropertyBL(ServletContext context) {
491 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
492 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
493 PropertyBusinessLogic propertytBl = webApplicationContext.getBean(PropertyBusinessLogic.class);
497 protected InputsBusinessLogic getInputBL(ServletContext context) {
498 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
499 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
500 return webApplicationContext.getBean(InputsBusinessLogic.class);
503 protected PolicyBusinessLogic getPolicyBL(ServletContext context) {
504 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
505 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
506 return webApplicationContext.getBean(PolicyBusinessLogic.class);
509 protected Either<ComponentInstInputsMap, ResponseFormat> parseToComponentInstanceMap(String componentJson, User user, ComponentTypeEnum componentType) {
510 return getComponentsUtils().convertJsonToObjectUsingObjectMapper(componentJson, user, ComponentInstInputsMap.class, AuditingActionEnum.CREATE_RESOURCE, componentType);
513 protected Response declareProperties(String userId, String componentId, String componentType,
514 String componentInstInputsMapObj, DeclarationTypeEnum typeEnum, HttpServletRequest request) {
515 ServletContext context = request.getSession().getServletContext();
516 String url = request.getMethod() + " " + request.getRequestURI();
517 log.debug("(get) Start handle request of {}", url);
518 Response response = null;
521 BaseBusinessLogic businessLogic = getBlForPropertyDeclaration(typeEnum, context);
524 User modifier = new User();
525 modifier.setUserId(userId);
526 log.debug("modifier id is {}", userId);
527 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
528 Either<ComponentInstInputsMap, ResponseFormat> componentInstInputsMapRes = parseToComponentInstanceMap(componentInstInputsMapObj, modifier, componentTypeEnum);
529 if (componentInstInputsMapRes.isRight()) {
530 log.debug("failed to parse componentInstInputsMap");
531 response = buildErrorResponse(componentInstInputsMapRes.right().value());
535 Either<List<ToscaDataDefinition>, ResponseFormat> propertiesAfterDeclaration = businessLogic
536 .declareProperties(userId, componentId,
538 componentInstInputsMapRes.left().value());
539 if (propertiesAfterDeclaration.isRight()) {
540 log.debug("failed to create inputs for service: {}", componentId);
541 return buildErrorResponse(propertiesAfterDeclaration.right().value());
543 Object properties = RepresentationUtils.toRepresentation(propertiesAfterDeclaration.left().value());
544 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), properties);
546 } catch (Exception e) {
547 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create inputs for service with id: " + componentId);
548 log.debug("Properties declaration failed with exception", e);
549 response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
554 public BaseBusinessLogic getBlForPropertyDeclaration(DeclarationTypeEnum typeEnum,
555 ServletContext context) {
556 if(typeEnum.equals(DeclarationTypeEnum.POLICY)) {
557 return getPolicyBL(context);
560 return getInputBL(context);