Implement Attributes/Outputs BE (part 2)
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / BeGenericServlet.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.servlets;
22
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 io.swagger.v3.oas.annotations.OpenAPIDefinition;
32 import io.swagger.v3.oas.annotations.info.Info;
33 import io.swagger.v3.oas.annotations.servers.Server;
34 import org.json.simple.JSONArray;
35 import org.json.simple.JSONObject;
36 import org.json.simple.parser.JSONParser;
37 import org.json.simple.parser.ParseException;
38 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
39 import org.openecomp.sdc.be.components.impl.BaseBusinessLogic;
40 import org.openecomp.sdc.be.components.impl.CapabilitiesBusinessLogic;
41 import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic;
42 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
43 import org.openecomp.sdc.be.components.impl.ElementBusinessLogic;
44 import org.openecomp.sdc.be.components.impl.GenericArtifactBrowserBusinessLogic;
45 import org.openecomp.sdc.be.components.impl.InputsBusinessLogic;
46 import org.openecomp.sdc.be.components.impl.InterfaceOperationBusinessLogic;
47 import org.openecomp.sdc.be.components.impl.OutputsBusinessLogic;
48 import org.openecomp.sdc.be.components.impl.PolicyBusinessLogic;
49 import org.openecomp.sdc.be.components.impl.ProductBusinessLogic;
50 import org.openecomp.sdc.be.components.impl.RelationshipTypeBusinessLogic;
51 import org.openecomp.sdc.be.components.impl.RequirementBusinessLogic;
52 import org.openecomp.sdc.be.components.impl.ResourceBusinessLogic;
53 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
54 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
55 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
56 import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic;
57 import org.openecomp.sdc.be.components.scheduledtasks.ComponentsCleanBusinessLogic;
58 import org.openecomp.sdc.be.components.upgrade.UpgradeBusinessLogic;
59 import org.openecomp.sdc.be.config.BeEcompErrorManager;
60 import org.openecomp.sdc.be.dao.api.ActionStatus;
61 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
62 import org.openecomp.sdc.be.datatypes.enums.DeclarationTypeEnum;
63 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
64 import org.openecomp.sdc.be.ecomp.converters.AssetMetadataConverter;
65 import org.openecomp.sdc.be.impl.ComponentsUtils;
66 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
67 import org.openecomp.sdc.be.model.ComponentInstInputsMap;
68 import org.openecomp.sdc.be.model.PropertyConstraint;
69 import org.openecomp.sdc.be.model.PropertyDefinition;
70 import org.openecomp.sdc.be.model.InputDefinition;
71 import org.openecomp.sdc.be.model.User;
72 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
73 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation.PropertyConstraintJacksonDeserializer;
74 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
75 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
76 import org.openecomp.sdc.be.user.UserBusinessLogic;
77 import org.openecomp.sdc.common.api.Constants;
78 import org.openecomp.sdc.common.log.wrappers.Logger;
79 import org.openecomp.sdc.common.servlets.BasicServlet;
80 import org.openecomp.sdc.exception.ResponseFormat;
81 import org.springframework.web.context.WebApplicationContext;
82
83 import javax.servlet.ServletContext;
84 import javax.servlet.http.HttpServletRequest;
85 import javax.ws.rs.core.Context;
86 import javax.ws.rs.core.Response;
87 import javax.ws.rs.core.Response.ResponseBuilder;
88 import java.io.IOException;
89 import java.lang.reflect.Type;
90 import java.util.HashMap;
91 import java.util.Iterator;
92 import java.util.List;
93 import java.util.Map;
94 import java.util.Map.Entry;
95 import java.util.Objects;
96 import java.util.Set;
97 import java.util.function.Supplier;
98
99 @OpenAPIDefinition(info = @Info(title = "SDC API", description = "SDC External, Distribution and Internal APIs"),
100         servers = {@Server(url = "/sdc", description = "SDC External and Distribution APIs"),
101                 @Server(url = "/sdc2/rest", description = "SDC Internal APIs")})
102 public class BeGenericServlet extends BasicServlet {
103
104     public BeGenericServlet(UserBusinessLogic userAdminManager,
105         ComponentsUtils componentsUtils) {
106         this.userAdminManager = userAdminManager;
107         this.componentsUtils = componentsUtils;
108     }
109
110     @Context
111     protected HttpServletRequest servletRequest;
112
113     private static final Logger log = Logger.getLogger(BeGenericServlet.class);
114
115     private static final String PROPERTY_NAME_REGEX = "[\\w,\\d,_]+";
116
117     private UserBusinessLogic userAdminManager;
118     protected ComponentsUtils componentsUtils;
119
120     /******************** New error response mechanism
121      * @param requestErrorWrapper **************/
122
123     protected Response buildErrorResponse(ResponseFormat requestErrorWrapper) {
124         return Response.status(requestErrorWrapper.getStatus()).entity(gson.toJson(requestErrorWrapper.getRequestError())).build();
125     }
126
127     protected Response buildGeneralErrorResponse() {
128         return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
129     }
130
131     protected Response buildOkResponse(Object entity) {
132         return buildOkResponseStatic(entity);
133     }
134
135     private static Response buildOkResponseStatic(Object entity) {
136         return Response.status(Response.Status.OK)
137             .entity(entity)
138             .build();
139     }
140
141     public HttpServletRequest getServletRequest() {
142         return servletRequest;
143     }
144
145     @VisibleForTesting
146     public void setRequestServlet(HttpServletRequest request) {this.servletRequest = request;}
147
148     protected Response buildOkResponse(ResponseFormat errorResponseWrapper, Object entity) {
149         return buildOkResponse(errorResponseWrapper, entity, null);
150     }
151
152     protected Response buildOkResponse(ResponseFormat errorResponseWrapper, Object entity, Map<String, String> additionalHeaders) {
153         int status = errorResponseWrapper.getStatus();
154         ResponseBuilder responseBuilder = Response.status(status);
155         if (entity != null) {
156             if (log.isTraceEnabled())
157                 log.trace("returned entity is {}", entity.toString());
158             responseBuilder = responseBuilder.entity(entity);
159         }
160         if (additionalHeaders != null) {
161             for (Entry<String, String> additionalHeader : additionalHeaders.entrySet()) {
162                 String headerName = additionalHeader.getKey();
163                 String headerValue = additionalHeader.getValue();
164                 if (log.isTraceEnabled())
165                     log.trace("Adding header {} with value {} to the response", headerName, headerValue);
166                 responseBuilder.header(headerName, headerValue);
167             }
168         }
169         return responseBuilder.build();
170     }
171
172     /*******************************************************************************************************/
173     protected Either<User, ResponseFormat> getUser(final HttpServletRequest request, String userId) {
174         User user;
175         try {
176             user = getUserAdminManager(request.getSession().getServletContext()).getUser(userId, false);
177             return Either.left(user);
178         } catch (ComponentException ce) {
179             log.info("createResource method - user is not listed. userId= {}", userId);
180             ResponseFormat errorResponse = getComponentsUtils().getResponseFormat(ce);
181             user = new User("", "", userId, "", null, null);
182             getComponentsUtils().auditResource(errorResponse, user, "", AuditingActionEnum.CHECKOUT_RESOURCE);
183             return Either.right(errorResponse);
184         }
185     }
186
187     UserBusinessLogic getUserAdminManager(ServletContext context) {
188         return getClassFromWebAppContext(context, () -> UserBusinessLogic.class);
189     }
190
191     protected GenericArtifactBrowserBusinessLogic getGenericArtifactBrowserBL(ServletContext context) {
192         return getClassFromWebAppContext(context, () -> GenericArtifactBrowserBusinessLogic.class);
193     }
194
195     protected ResourceBusinessLogic getResourceBL(ServletContext context) {
196         return getClassFromWebAppContext(context, () -> ResourceBusinessLogic.class);
197     }
198
199     InterfaceOperationBusinessLogic getInterfaceOperationBL(ServletContext context) {
200         return getClassFromWebAppContext(context, () -> InterfaceOperationBusinessLogic.class);
201     }
202
203     protected CapabilitiesBusinessLogic getCapabilitiesBL(ServletContext context) {
204         return getClassFromWebAppContext(context, () -> CapabilitiesBusinessLogic.class);
205     }
206
207     protected RelationshipTypeBusinessLogic getRelationshipTypeBL(ServletContext context) {
208         return getClassFromWebAppContext(context, () -> RelationshipTypeBusinessLogic.class);
209     }
210     protected RequirementBusinessLogic getRequirementBL(ServletContext context) {
211         return getClassFromWebAppContext(context, () -> RequirementBusinessLogic.class);
212     }
213     ComponentsCleanBusinessLogic getComponentCleanerBL(ServletContext context) {
214         return getClassFromWebAppContext(context, () -> ComponentsCleanBusinessLogic.class);
215     }
216
217     protected ServiceBusinessLogic getServiceBL(ServletContext context) {
218         return getClassFromWebAppContext(context, () -> ServiceBusinessLogic.class);
219     }
220
221     ProductBusinessLogic getProductBL(ServletContext context) {
222         return getClassFromWebAppContext(context, () -> ProductBusinessLogic.class);
223     }
224
225     protected ArtifactsBusinessLogic getArtifactBL(ServletContext context) {
226         return getClassFromWebAppContext(context, () -> ArtifactsBusinessLogic.class);
227     }
228     protected UpgradeBusinessLogic getUpgradeBL(ServletContext context) {
229         return getClassFromWebAppContext(context, () -> UpgradeBusinessLogic.class);
230     }
231
232     protected ElementBusinessLogic getElementBL(ServletContext context) {
233         return getClassFromWebAppContext(context, () -> ElementBusinessLogic.class);
234     }
235
236     protected AssetMetadataConverter getAssetUtils(ServletContext context) {
237         return getClassFromWebAppContext(context, () -> AssetMetadataConverter.class);
238     }
239
240     protected LifecycleBusinessLogic getLifecycleBL(ServletContext context) {
241         return getClassFromWebAppContext(context, () -> LifecycleBusinessLogic.class);
242     }
243
244     <T> T getClassFromWebAppContext(final ServletContext context, final Supplier<Class<T>> businessLogicClassGen) {
245         return getWebAppContext(context).getBean(businessLogicClassGen.get());
246     }
247
248     protected ComponentInstanceBusinessLogic getComponentInstanceBL(final ServletContext context) {
249         return getClassFromWebAppContext(context, () -> ComponentInstanceBusinessLogic.class);
250     }
251
252     protected ComponentsUtils getComponentsUtils() {
253         final ServletContext context = this.servletRequest.getSession().getServletContext();
254         return getClassFromWebAppContext(context, () -> ComponentsUtils.class);
255     }
256
257     /**
258      * Used to support Unit Test.<br>
259      * Header Params are not supported in Unit Tests
260      *
261      * @return
262      */
263     String initHeaderParam(String headerValue, HttpServletRequest request, String headerName) {
264         String retValue;
265         if (headerValue != null) {
266             retValue = headerValue;
267         } else {
268             retValue = request.getHeader(headerName);
269         }
270         return retValue;
271     }
272
273     protected String getContentDispositionValue(String artifactFileName) {
274         return new StringBuilder().append("attachment; filename=\"").append(artifactFileName).append("\"").toString();
275     }
276
277     protected ComponentBusinessLogic getComponentBL(ComponentTypeEnum componentTypeEnum, ServletContext context) {
278         ComponentBusinessLogic businessLogic;
279         switch (componentTypeEnum) {
280             case RESOURCE:
281                 businessLogic = getResourceBL(context);
282                 break;
283             case SERVICE:
284                 businessLogic = getServiceBL(context);
285                 break;
286             case PRODUCT:
287                 businessLogic = getProductBL(context);
288                 break;
289             case RESOURCE_INSTANCE:
290                 businessLogic = getResourceBL(context);
291                 break;
292             default:
293                 BeEcompErrorManager.getInstance().logBeSystemError("getComponentBL");
294                 throw new IllegalArgumentException("Illegal component type:" + componentTypeEnum.getValue());
295         }
296         return businessLogic;
297     }
298
299     <T> T convertJsonToObjectOfClass(String json, Class<T> clazz) {
300         T object = null;
301         ObjectMapper mapper = new ObjectMapper()
302             .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
303             .configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
304         try {
305             log.trace("Starting to convert json to object. Json=\n{}", json);
306
307             SimpleModule module = new SimpleModule("customDeserializationModule");
308             module.addDeserializer(PropertyConstraint.class, new PropertyConstraintJacksonDeserializer());
309             mapper.registerModule(module);
310
311             object = mapper.readValue(json, clazz);
312             if (object != null) {
313                 return object;
314             } else {
315                 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("convertJsonToObject");
316                 log.debug("The object of class {} is null after converting from json. ", clazz);
317                 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
318             }
319         } catch (IOException e) {
320             BeEcompErrorManager.getInstance().logBeInvalidJsonInput("convertJsonToObject");
321             log.debug("The exception {} occurred upon json to object convertation. Json=\n{}", e, json);
322             throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
323         }
324     }
325
326     protected Either<Map<String, PropertyDefinition>, ActionStatus> getPropertyModel(String componentId, String data) {
327         JSONParser parser = new JSONParser();
328         JSONObject root;
329         try {
330             Map<String, PropertyDefinition> properties = new HashMap<>();
331             root = (JSONObject) parser.parse(data);
332
333             Set entrySet = root.entrySet();
334             Iterator iterator = entrySet.iterator();
335             while (iterator.hasNext()) {
336                 Entry next = (Entry) iterator.next();
337                 String propertyName = (String) next.getKey();
338
339                 if(!isPropertyNameValid(propertyName)) {
340                     return Either.right(ActionStatus.INVALID_PROPERTY_NAME);
341                 }
342
343                 JSONObject value = (JSONObject) next.getValue();
344                 Either<PropertyDefinition, ActionStatus> propertyDefinitionEither =
345                         getPropertyDefinitionFromJson(componentId, propertyName, value);
346
347                 if(propertyDefinitionEither.isRight()) {
348                     return Either.right(propertyDefinitionEither.right().value());
349                 }
350
351                 properties.put(propertyName, propertyDefinitionEither.left().value());
352             }
353
354             return Either.left(properties);
355         } catch (ParseException e) {
356             log.info("Property conetnt is invalid - {}", data);
357             return Either.right(ActionStatus.INVALID_CONTENT);
358         }
359     }
360
361     protected boolean isPropertyNameValid(String propertyName) {
362         return Objects.nonNull(propertyName)
363                        && propertyName.matches(PROPERTY_NAME_REGEX);
364
365     }
366
367     private Either<PropertyDefinition, ActionStatus> getPropertyDefinitionFromJson(String componentId, String propertyName, JSONObject value) {
368         String jsonString = value.toJSONString();
369         Either<PropertyDefinition, ActionStatus> convertJsonToObject = convertJsonToObject(jsonString, PropertyDefinition.class);
370         if (convertJsonToObject.isRight()) {
371             return Either.right(convertJsonToObject.right().value());
372         }
373         PropertyDefinition propertyDefinition = convertJsonToObject.left().value();
374         String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(componentId, propertyName);
375         propertyDefinition.setUniqueId(uniqueId);
376
377         return Either.left(propertyDefinition);
378     }
379
380     private Either<InputDefinition, ActionStatus> getInputDefinitionFromJson(String componentId, String inputName, JSONObject value) {
381         String jsonString = value.toJSONString();
382         Either<InputDefinition, ActionStatus> convertJsonToObject = convertJsonToObject(jsonString, InputDefinition.class);
383         if (convertJsonToObject.isRight()) {
384             return Either.right(convertJsonToObject.right().value());
385         }
386         InputDefinition inputDefinition = convertJsonToObject.left().value();
387         String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(componentId, inputName);
388         inputDefinition.setUniqueId(uniqueId);
389
390         return Either.left(inputDefinition);
391     }
392
393     protected Either<Map<String, PropertyDefinition>, ActionStatus> getPropertiesListForUpdate(String data) {
394
395         Map<String, PropertyDefinition> properties = new HashMap<>();
396         JSONParser parser = new JSONParser();
397         JSONArray jsonArray;
398
399         try {
400             jsonArray = (JSONArray) parser.parse(data);
401             for (Object jsonElement : jsonArray) {
402                 String propertyAsString = jsonElement.toString();
403                 Either<PropertyDefinition, ActionStatus> convertJsonToObject = convertJsonToObject(propertyAsString, PropertyDefinition.class);
404
405                 if (convertJsonToObject.isRight()) {
406                     return Either.right(convertJsonToObject.right().value());
407                 }
408
409                 PropertyDefinition propertyDefinition = convertJsonToObject.left().value();
410                 properties.put(propertyDefinition.getName(), propertyDefinition);
411             }
412
413             return Either.left(properties);
414         } catch (Exception e) {
415             log.info("Property content is invalid - {}", data);
416             return Either.right(ActionStatus.INVALID_CONTENT);
417         }
418
419     }
420
421     protected String propertyToJson(Map.Entry<String, PropertyDefinition> property) {
422         JSONObject root = new JSONObject();
423         String propertyName = property.getKey();
424         PropertyDefinition propertyDefinition = property.getValue();
425         JSONObject propertyDefinitionO = getPropertyDefinitionJSONObject(propertyDefinition);
426         root.put(propertyName, propertyDefinitionO);
427         propertyDefinition.getType();
428         return root.toString();
429     }
430
431     private JSONObject getPropertyDefinitionJSONObject(PropertyDefinition propertyDefinition) {
432
433         Either<String, ActionStatus> either = convertObjectToJson(propertyDefinition);
434         if (either.isRight()) {
435             return new JSONObject();
436         }
437         try {
438             return  (JSONObject) new JSONParser().parse(either.left().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();
443         }
444
445     }
446
447     protected  <T> Either<T, ActionStatus> convertJsonToObject(String data, Class<T> clazz) {
448         T t = null;
449         Type constraintType = new TypeToken<PropertyConstraint>() {
450         }.getType();
451         Gson
452             gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyOperation.PropertyConstraintDeserialiser()).create();
453         try {
454             log.trace("convert json to object. json=\n {}", data);
455             t = gson.fromJson(data, clazz);
456             if (t == null) {
457                 log.info("object is null after converting from json");
458                 return Either.right(ActionStatus.INVALID_CONTENT);
459             }
460         } catch (Exception e) {
461             // INVALID JSON
462             log.info("failed to convert from json");
463             log.error("failed to convert from json", e);
464             return Either.right(ActionStatus.INVALID_CONTENT);
465         }
466         return Either.left(t);
467     }
468
469     private Either<String, ActionStatus> convertObjectToJson(PropertyDefinition propertyDefinition) {
470         Type constraintType = new TypeToken<PropertyConstraint>() {
471         }.getType();
472         Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyOperation.PropertyConstraintSerialiser()).create();
473         try {
474             log.trace("convert object to json. propertyDefinition= {}", propertyDefinition);
475             String json = gson.toJson(propertyDefinition);
476             if (json == null) {
477                 log.info("object is null after converting to json");
478                 return Either.right(ActionStatus.INVALID_CONTENT);
479             }
480             return Either.left(json);
481         } catch (Exception e) {
482             // INVALID JSON
483             log.info("failed to convert to json");
484             log.debug("failed to convert fto json", e);
485             return Either.right(ActionStatus.INVALID_CONTENT);
486         }
487
488     }
489
490     private OutputsBusinessLogic getOutputBL(final ServletContext context) {
491         return getClassFromWebAppContext(context, () -> OutputsBusinessLogic.class);
492     }
493
494     private InputsBusinessLogic getInputBL(final ServletContext context) {
495         return getClassFromWebAppContext(context, () -> InputsBusinessLogic.class);
496     }
497
498     private PolicyBusinessLogic getPolicyBL(final ServletContext context) {
499         return getClassFromWebAppContext(context, () -> PolicyBusinessLogic.class);
500     }
501
502     private WebApplicationContext getWebAppContext(final ServletContext context) {
503         return ((WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).getWebAppContext(context);
504     }
505
506     protected <T> Either<T, ResponseFormat> parseToComponentInstanceMap(final String componentJson,
507                                                                         final User user,
508                                                                         final ComponentTypeEnum componentType,
509                                                                         final Class<T> clazz) {
510         return getComponentsUtils()
511             .convertJsonToObjectUsingObjectMapper(componentJson, user, clazz, AuditingActionEnum.CREATE_RESOURCE, componentType);
512     }
513
514     protected Response declareProperties(String userId, String componentId, String componentType,
515                                          String componentInstInputsMapObj, DeclarationTypeEnum typeEnum, HttpServletRequest request) {
516         ServletContext context = request.getSession().getServletContext();
517         String url = request.getMethod() + " " + request.getRequestURI();
518         log.debug("(get) Start handle request of {}", url);
519
520         try {
521             BaseBusinessLogic businessLogic = getBlForDeclaration(typeEnum, context);
522
523             // get modifier id
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(
529                 componentInstInputsMapObj, modifier, componentTypeEnum, ComponentInstInputsMap.class);
530             if (componentInstInputsMapRes.isRight()) {
531                 log.debug("failed to parse componentInstInputsMap");
532                 return buildErrorResponse(componentInstInputsMapRes.right().value());
533             }
534
535             Either<List<ToscaDataDefinition>, ResponseFormat> propertiesAfterDeclaration = businessLogic
536                 .declareProperties(userId, componentId,
537                     componentTypeEnum,
538                     componentInstInputsMapRes.left().value());
539             if (propertiesAfterDeclaration.isRight()) {
540                 log.debug("failed to create inputs  for service: {}", componentId);
541                 return buildErrorResponse(propertiesAfterDeclaration.right().value());
542             }
543             Object properties = RepresentationUtils.toRepresentation(propertiesAfterDeclaration.left().value());
544             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), properties);
545
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             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
550         }
551     }
552
553     public BaseBusinessLogic getBlForDeclaration(final DeclarationTypeEnum typeEnum,
554                                                  final ServletContext context) {
555         switch (typeEnum) {
556             case OUTPUT:
557                 return getOutputBL(context);
558             case POLICY:
559                 return getPolicyBL(context);
560             case INPUT:
561                 return getInputBL(context);
562             default:
563                 throw new IllegalArgumentException("Invalid DeclarationTypeEnum");
564         }
565     }
566
567     protected Either<Map<String, InputDefinition>, ActionStatus> getInputModel(String componentId, String data) {
568         JSONParser parser = new JSONParser();
569         JSONObject root;
570         try {
571             Map<String, InputDefinition> inputs = new HashMap<>();
572             root = (JSONObject) parser.parse(data);
573
574             Set entrySet = root.entrySet();
575             Iterator iterator = entrySet.iterator();
576             while (iterator.hasNext()) {
577                 Entry next = (Entry) iterator.next();
578                 String inputName = (String) next.getKey();
579
580                 if(!isInputNameValid(inputName)) {
581                     return Either.right(ActionStatus.INVALID_INPUT_NAME);
582                 }
583
584                 JSONObject value = (JSONObject) next.getValue();
585                 Either<InputDefinition, ActionStatus> inputDefinitionEither =
586                         getInputDefinitionFromJson(componentId, inputName, value);
587
588                 if(inputDefinitionEither.isRight()) {
589                     return Either.right(inputDefinitionEither.right().value());
590                 }
591
592                 inputs.put(inputName, inputDefinitionEither.left().value());
593             }
594
595             return Either.left(inputs);
596         } catch (ParseException e) {
597             log.warn("Input content is invalid - {}", data, e);
598             return Either.right(ActionStatus.INVALID_CONTENT);
599         }
600     }
601
602     protected boolean isInputNameValid(String inputName) {
603         return Objects.nonNull(inputName)
604                 && inputName.matches(PROPERTY_NAME_REGEX);
605
606     }
607 }