Fix 'Fail to declare Input for duplicated name of property'
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / InputsServlet.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 package org.openecomp.sdc.be.servlets;
21
22 import com.fasterxml.jackson.core.JsonProcessingException;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import com.jcabi.aspects.Loggable;
25 import fj.data.Either;
26 import io.swagger.v3.oas.annotations.Operation;
27 import io.swagger.v3.oas.annotations.Parameter;
28 import io.swagger.v3.oas.annotations.media.ArraySchema;
29 import io.swagger.v3.oas.annotations.media.Content;
30 import io.swagger.v3.oas.annotations.media.Schema;
31 import io.swagger.v3.oas.annotations.responses.ApiResponse;
32 import io.swagger.v3.oas.annotations.servers.Server;
33 import io.swagger.v3.oas.annotations.tags.Tag;
34 import io.swagger.v3.oas.annotations.tags.Tags;
35 import java.io.IOException;
36 import java.util.Arrays;
37 import java.util.List;
38 import java.util.Map;
39 import javax.inject.Inject;
40 import javax.servlet.http.HttpServletRequest;
41 import javax.ws.rs.Consumes;
42 import javax.ws.rs.DELETE;
43 import javax.ws.rs.GET;
44 import javax.ws.rs.HeaderParam;
45 import javax.ws.rs.POST;
46 import javax.ws.rs.Path;
47 import javax.ws.rs.PathParam;
48 import javax.ws.rs.Produces;
49 import javax.ws.rs.core.Context;
50 import javax.ws.rs.core.MediaType;
51 import javax.ws.rs.core.Response;
52 import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
53 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
54 import org.openecomp.sdc.be.components.impl.DataTypeBusinessLogic;
55 import org.openecomp.sdc.be.components.impl.InputsBusinessLogic;
56 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
57 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
58 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
59 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
60 import org.openecomp.sdc.be.config.BeEcompErrorManager;
61 import org.openecomp.sdc.be.dao.api.ActionStatus;
62 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
63 import org.openecomp.sdc.be.datatypes.enums.DeclarationTypeEnum;
64 import org.openecomp.sdc.be.impl.ComponentsUtils;
65 import org.openecomp.sdc.be.impl.ServletUtils;
66 import org.openecomp.sdc.be.model.ComponentInstListInput;
67 import org.openecomp.sdc.be.model.ComponentInstanceInput;
68 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
69 import org.openecomp.sdc.be.model.DataTypeDefinition;
70 import org.openecomp.sdc.be.model.InputDefinition;
71 import org.openecomp.sdc.be.model.Resource;
72 import org.openecomp.sdc.be.model.User;
73 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
74 import org.openecomp.sdc.be.resources.data.EntryData;
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.elements.LoggerSupportability;
79 import org.openecomp.sdc.common.log.enums.LoggerSupportabilityActions;
80 import org.openecomp.sdc.common.log.enums.StatusCode;
81 import org.openecomp.sdc.common.log.wrappers.Logger;
82 import org.openecomp.sdc.exception.ResponseFormat;
83 import org.springframework.stereotype.Controller;
84
85 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
86 @Tags({@Tag(name = "SDCE-2 APIs")})
87 @Server(url = "/sdc2/rest")
88 @Path("/v1/catalog")
89 @Controller
90 @Consumes(MediaType.APPLICATION_JSON)
91 @Produces(MediaType.APPLICATION_JSON)
92 public class InputsServlet extends AbstractValidationsServlet {
93
94     private static final Logger log = Logger.getLogger(InputsServlet.class);
95     private static final LoggerSupportability loggerSupportability = LoggerSupportability.getLogger(InputsServlet.class.getName());
96     private static final String START_HANDLE_REQUEST_OF = "(get) Start handle request of {}";
97     private static final String CREATE_INPUT = "CreateInput";
98     private final DataTypeBusinessLogic businessLogic;
99     private final InputsBusinessLogic inputsBusinessLogic;
100
101     @Inject
102     public InputsServlet(UserBusinessLogic userBusinessLogic, InputsBusinessLogic inputsBusinessLogic,
103                          ComponentInstanceBusinessLogic componentInstanceBL, ComponentsUtils componentsUtils, ServletUtils servletUtils,
104                          ResourceImportManager resourceImportManager, DataTypeBusinessLogic dataTypeBusinessLogic) {
105         super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
106         this.inputsBusinessLogic = inputsBusinessLogic;
107         this.businessLogic = dataTypeBusinessLogic;
108     }
109
110     @POST
111     @Path("/{containerComponentType}/{componentId}/update/inputs")
112     @Operation(description = "Update resource  inputs", method = "POST", summary = "Returns updated input", responses = {
113         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
114         @ApiResponse(responseCode = "200", description = "Input updated"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
115         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
116     public Response updateComponentInputs(
117         @Parameter(description = "valid values: resources / services", schema = @Schema(allowableValues = {ComponentTypeEnum.RESOURCE_PARAM_NAME,
118             ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
119         @PathParam("componentId") final String componentId, @Parameter(description = "json describe the input", required = true) String data,
120         @Context final HttpServletRequest request) throws JsonProcessingException {
121         String url = request.getMethod() + " " + request.getRequestURI();
122         log.debug("Start handle request of {}", url);
123         String userId = request.getHeader(Constants.USER_ID_HEADER);
124         try {
125             User modifier = new User();
126             modifier.setUserId(userId);
127             log.debug("modifier id is {}", userId);
128             Either<InputDefinition[], ResponseFormat> inputsEither = getComponentsUtils()
129                 .convertJsonToObjectUsingObjectMapper(data, modifier, InputDefinition[].class, AuditingActionEnum.UPDATE_RESOURCE_METADATA,
130                     ComponentTypeEnum.SERVICE);
131             if (inputsEither.isRight()) {
132                 log.debug("Failed to convert data to input definition. Status is {}", inputsEither.right().value());
133                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
134             }
135             List<InputDefinition> inputsToUpdate = Arrays.asList(inputsEither.left().value());
136             log.debug("Start handle request of updateComponentInputs. Received inputs are {}", inputsToUpdate);
137             ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(containerComponentType);
138             if (businessLogic == null) {
139                 log.debug("Unsupported component type {}", containerComponentType);
140                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR));
141             }
142             Either<List<InputDefinition>, ResponseFormat> actionResponse = inputsBusinessLogic
143                 .updateInputsValue(componentType, componentId, inputsToUpdate, userId, true);
144             if (actionResponse.isRight()) {
145                 return buildErrorResponse(actionResponse.right().value());
146             }
147             List<InputDefinition> componentInputs = actionResponse.left().value();
148             ObjectMapper mapper = new ObjectMapper();
149             String result = mapper.writeValueAsString(componentInputs);
150             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
151         } catch (Exception e) {
152             log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
153             throw e;
154         }
155     }
156
157     @GET
158     @Path("/{componentType}/{componentId}/componentInstances/{instanceId}/{originComponentUid}/inputs")
159     @Operation(description = "Get Inputs only", method = "GET", summary = "Returns Inputs list", responses = {
160         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
161         @ApiResponse(responseCode = "200", description = "Component found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
162         @ApiResponse(responseCode = "404", description = "Component not found")})
163     public Response getComponentInstanceInputs(@PathParam("componentType") final String componentType,
164                                                @PathParam("componentId") final String componentId, @PathParam("instanceId") final String instanceId,
165                                                @PathParam("originComponentUid") final String originComponentUid,
166                                                @Context final HttpServletRequest request,
167                                                @HeaderParam(value = Constants.USER_ID_HEADER) String userId) throws IOException {
168         String url = request.getMethod() + " " + request.getRequestURI();
169         log.debug(START_HANDLE_REQUEST_OF, url);
170         try {
171             Either<List<ComponentInstanceInput>, ResponseFormat> inputsResponse = inputsBusinessLogic
172                 .getComponentInstanceInputs(userId, componentId, instanceId);
173             if (inputsResponse.isRight()) {
174                 log.debug("failed to get component instance inputs {}", componentType);
175                 return buildErrorResponse(inputsResponse.right().value());
176             }
177             Object inputs = RepresentationUtils.toRepresentation(inputsResponse.left().value());
178             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), inputs);
179         } catch (Exception e) {
180             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Inputs " + componentType);
181             log.debug("getInputs failed with exception", e);
182             throw e;
183         }
184     }
185
186     @GET
187     @Path("/{componentType}/{componentId}/componentInstances/{instanceId}/{inputId}/properties")
188     @Operation(description = "Get properties", method = "GET", summary = "Returns properties list", responses = {
189         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
190         @ApiResponse(responseCode = "200", description = "Component found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
191         @ApiResponse(responseCode = "404", description = "Component not found")})
192     public Response getInputPropertiesForComponentInstance(@PathParam("componentType") final String componentType,
193                                                            @PathParam("componentId") final String componentId,
194                                                            @PathParam("instanceId") final String instanceId,
195                                                            @PathParam("inputId") final String inputId, @Context final HttpServletRequest request,
196                                                            @HeaderParam(value = Constants.USER_ID_HEADER) String userId) throws IOException {
197         String url = request.getMethod() + " " + request.getRequestURI();
198         log.debug(START_HANDLE_REQUEST_OF, url);
199         try {
200             Either<List<ComponentInstanceProperty>, ResponseFormat> inputPropertiesRes = inputsBusinessLogic
201                 .getComponentInstancePropertiesByInputId(userId, componentId, instanceId, inputId);
202             if (inputPropertiesRes.isRight()) {
203                 log.debug("failed to get properties of input: {}, with instance id: {}", inputId, instanceId);
204                 return buildErrorResponse(inputPropertiesRes.right().value());
205             }
206             Object properties = RepresentationUtils.toRepresentation(inputPropertiesRes.left().value());
207             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), properties);
208         } catch (Exception e) {
209             BeEcompErrorManager.getInstance()
210                 .logBeRestApiGeneralError("Get Properites by input id: " + inputId + " for instance with id: " + instanceId);
211             log.debug("getInputPropertiesForComponentInstance failed with exception", e);
212             throw e;
213         }
214     }
215
216     @GET
217     @Path("/{componentType}/{componentId}/inputs/{inputId}/inputs")
218     @Operation(description = "Get inputs", method = "GET", summary = "Returns inputs list", responses = {
219         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
220         @ApiResponse(responseCode = "200", description = "Component found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
221         @ApiResponse(responseCode = "404", description = "Component not found")})
222     public Response getInputsForComponentInput(@PathParam("componentType") final String componentType,
223                                                @PathParam("componentId") final String componentId, @PathParam("inputId") final String inputId,
224                                                @Context final HttpServletRequest request,
225                                                @HeaderParam(value = Constants.USER_ID_HEADER) String userId) throws IOException {
226         String url = request.getMethod() + " " + request.getRequestURI();
227         log.debug(START_HANDLE_REQUEST_OF, url);
228         try {
229             Either<List<ComponentInstanceInput>, ResponseFormat> inputsRes = inputsBusinessLogic
230                 .getInputsForComponentInput(userId, componentId, inputId);
231             if (inputsRes.isRight()) {
232                 log.debug("failed to get inputs of input: {}, with instance id: {}", inputId, componentId);
233                 return buildErrorResponse(inputsRes.right().value());
234             }
235             Object properties = RepresentationUtils.toRepresentation(inputsRes.left().value());
236             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), properties);
237         } catch (Exception e) {
238             BeEcompErrorManager.getInstance()
239                 .logBeRestApiGeneralError("Get inputs by input id: " + inputId + " for component with id: " + componentId);
240             log.debug("getInputsForComponentInput failed with exception", e);
241             throw e;
242         }
243     }
244
245     @GET
246     @Path("/{componentType}/{componentId}/inputs/{inputId}")
247     @Operation(description = "Get inputs", method = "GET", summary = "Returns inputs list", responses = {
248         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
249         @ApiResponse(responseCode = "200", description = "Component found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
250         @ApiResponse(responseCode = "404", description = "Component not found")})
251     public Response getInputsAndPropertiesForComponentInput(@PathParam("componentType") final String componentType,
252                                                             @PathParam("componentId") final String componentId,
253                                                             @PathParam("inputId") final String inputId, @Context final HttpServletRequest request,
254                                                             @HeaderParam(value = Constants.USER_ID_HEADER) String userId) throws IOException {
255         String url = request.getMethod() + " " + request.getRequestURI();
256         log.debug(START_HANDLE_REQUEST_OF, url);
257         try {
258             Either<InputDefinition, ResponseFormat> inputsRes = inputsBusinessLogic
259                 .getInputsAndPropertiesForComponentInput(userId, componentId, inputId, false);
260             if (inputsRes.isRight()) {
261                 log.debug("failed to get inputs of input: {}, with instance id: {}", inputId, componentId);
262                 return buildErrorResponse(inputsRes.right().value());
263             }
264             Object properties = RepresentationUtils.toRepresentation(inputsRes.left().value());
265             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), properties);
266         } catch (Exception e) {
267             BeEcompErrorManager.getInstance()
268                 .logBeRestApiGeneralError("Get inputs by input id: " + inputId + " for component with id: " + componentId);
269             log.debug("getInputsForComponentInput failed with exception", e);
270             throw e;
271         }
272     }
273
274     private Either<ComponentInstListInput, ResponseFormat> parseToComponentInstListInput(String json, User user) {
275         return getComponentsUtils().convertJsonToObjectUsingObjectMapper(json, user, ComponentInstListInput.class, AuditingActionEnum.CREATE_RESOURCE,
276             ComponentTypeEnum.SERVICE);
277     }
278
279     @POST
280     @Path("/{componentType}/{componentId}/create/inputs")
281     @Operation(description = "Create inputs on service", method = "POST", summary = "Return inputs list", responses = {
282         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
283         @ApiResponse(responseCode = "200", description = "Component found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
284         @ApiResponse(responseCode = "404", description = "Component not found")})
285     public Response createMultipleInputs(@PathParam("componentType") final String componentType, @PathParam("componentId") final String componentId,
286                                          @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
287                                          @Parameter(description = "ComponentIns Inputs Object to be created", required = true) String componentInstInputsMapObj) {
288         return super.declareProperties(userId, componentId, componentType, componentInstInputsMapObj, DeclarationTypeEnum.INPUT, request);
289     }
290
291     @POST
292     @Path("/{componentType}/{componentId}/create/input")
293     @Operation(description = "Create inputs on service", method = "POST", summary = "Return inputs list", responses = {
294         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
295         @ApiResponse(responseCode = "200", description = "Component found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
296         @ApiResponse(responseCode = "404", description = "Component not found")})
297     public Response createInput(@PathParam("componentType") final String componentType, @PathParam("componentId") final String componentId,
298                                 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
299                                 @Parameter(description = "ComponentIns Inputs Object to be created", required = true) String componentInstInputsMapObj) {
300         return createInput(componentId, componentInstInputsMapObj, request, userId);
301     }
302
303     /**
304      * Creates a "list input" and updates given list of properties to get value from the input. also a data type which has same properties is created.
305      * the data type will be the entry_schema of the list input.
306      *
307      * @param componentType             the container type (service, resource, ...)
308      * @param componentId               the container ID
309      * @param request                   HttpServletRequest object
310      * @param userId                    the User ID
311      * @param componentInstInputsMapObj the list of properties to be declared and the "list input" to be created. the type of the input must be
312      *                                  "list". schema.type of the input will be the name of new data type.
313      * @return the created input
314      */
315     @POST
316     @Path("/{componentType}/{componentId}/create/listInput")
317     @Operation(description = "Create a list input on service", method = "POST", summary = "Return input", responses = {
318         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
319         @ApiResponse(responseCode = "200", description = "Component found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
320         @ApiResponse(responseCode = "404", description = "Component not found")})
321     public Response createListInput(@PathParam("componentType") final String componentType, @PathParam("componentId") final String componentId,
322                                     @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
323                                     @Parameter(description = "ComponentIns Inputs Object to be created", required = true) String componentInstInputsMapObj) {
324         String url = request.getMethod() + " " + request.getRequestURI();
325         log.debug("#createListInput: Start handle request of {}", url);
326         try {
327             // get modifier id
328             User modifier = new User();
329             modifier.setUserId(userId);
330             log.debug("modifier id is {}", userId);
331             Either<ComponentInstListInput, ResponseFormat> componentInstInputsMapRes = parseToComponentInstListInput(componentInstInputsMapObj,
332                 modifier);
333             if (componentInstInputsMapRes.isRight()) {
334                 log.debug("failed to parse componentInstInputsMap");
335                 return buildErrorResponse(componentInstInputsMapRes.right().value());
336             }
337             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
338             ComponentInstListInput componentInstInputsMap = componentInstInputsMapRes.left().value();
339             if (log.isDebugEnabled()) {
340                 // for inspection on debug
341                 log.debug("parsed componentInstInputsMap={}", ReflectionToStringBuilder.toString(componentInstInputsMap));
342             }
343             Either<List<InputDefinition>, ResponseFormat> inputPropertiesRes = inputsBusinessLogic
344                 .createListInput(userId, componentId, componentTypeEnum, componentInstInputsMap, true, false);
345             if (inputPropertiesRes.isRight()) {
346                 log.debug("failed to create list input for service: {}", componentId);
347                 return buildErrorResponse(inputPropertiesRes.right().value());
348             }
349             Object properties = RepresentationUtils.toRepresentation(inputPropertiesRes.left().value());
350             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), properties);
351         } catch (Exception e) {
352             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create list input for service with id: " + componentId);
353             log.debug("createListInput failed with exception", e);
354             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
355         }
356     }
357
358     @DELETE
359     @Path("/{componentType}/{componentId}/delete/{inputId}/input")
360     @Operation(description = "Delete input from service", method = "DELETE", summary = "Delete service input", responses = {
361         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
362         @ApiResponse(responseCode = "200", description = "Input deleted"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
363         @ApiResponse(responseCode = "404", description = "Input not found")})
364     public Response deleteInput(@PathParam("componentType") final String componentType, @PathParam("componentId") final String componentId,
365                                 @PathParam("inputId") final String inputId, @Context final HttpServletRequest request,
366                                 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
367                                 @Parameter(description = "Service Input to be deleted", required = true) String componentInstInputsMapObj) {
368         String url = request.getMethod() + " " + request.getRequestURI();
369         log.debug(START_HANDLE_REQUEST_OF, url);
370         loggerSupportability.log(LoggerSupportabilityActions.DELETE_INPUTS, StatusCode.STARTED, "Starting to delete Inputs for component {} ",
371             componentId + " by " + userId);
372         try {
373             InputDefinition deleteInput = inputsBusinessLogic.deleteInput(componentId, userId, inputId);
374             loggerSupportability.log(LoggerSupportabilityActions.DELETE_INPUTS, StatusCode.COMPLETE, "Ended delete Inputs for component {} ",
375                 componentId + " by " + userId);
376             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), deleteInput);
377         } catch (ComponentException e) {
378             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete input for service + " + componentId + " + with id: " + inputId);
379             log.debug("Delete input failed with exception", e);
380             throw e;
381         }
382     }
383
384     /**
385      * Gets a specific data type associated with a component.
386      *
387      * @param componentType the container type (service, resource, ...)
388      * @param componentId   the container ID
389      * @param dataTypeName  the data type name
390      * @param request       HttpServletRequest object
391      * @return the data type info
392      */
393     @GET
394     @Path("/{componentType}/{componentId}/dataType/{dataTypeName}")
395     @Operation(description = "Get data type in service", method = "GET", summary = "Get data type in service", responses = {
396         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = DataTypeDefinition.class)))),
397         @ApiResponse(responseCode = "200", description = "Data type found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
398         @ApiResponse(responseCode = "404", description = "Data type not found")})
399     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
400     public Response getDataType(@PathParam("componentType") final String componentType, @PathParam("componentId") final String componentId,
401                                 @PathParam("dataTypeName") final String dataTypeName, @Context final HttpServletRequest request) {
402         String url = request.getMethod() + " " + request.getRequestURI();
403         log.debug("(getDataType) Start handle request of {}", url);
404         Response response;
405         try {
406             Either<DataTypeDefinition, StorageOperationStatus> getResult = businessLogic.getPrivateDataType(componentId, dataTypeName);
407             if (getResult.isRight()) {
408                 ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(getResult.right().value());
409                 return buildErrorResponse(componentsUtils.getResponseFormat(actionStatus));
410             }
411             Object json = RepresentationUtils.toRepresentation(getResult.left().value());
412             return buildOkResponse(componentsUtils.getResponseFormat(ActionStatus.OK), json);
413         } catch (Exception e) {
414             BeEcompErrorManager.getInstance()
415                 .logBeRestApiGeneralError("Get data type from service + " + componentId + " + with name: " + dataTypeName);
416             log.debug("Get data type failed with exception", e);
417             response = buildErrorResponse(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
418             return response;
419         }
420     }
421
422     /**
423      * Gets a list of data types which a component has.
424      *
425      * @param componentType the container type (service, resource, ...)
426      * @param componentId   the container ID
427      * @param request       HttpServletRequest object
428      * @return the list of data types in the component
429      */
430     @GET
431     @Path("/{componentType}/{componentId}/dataTypes")
432     @Operation(description = "Get data types that service has", method = "GET", summary = "Get data types in service", responses = {
433         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
434         @ApiResponse(responseCode = "200", description = "Data type found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
435         @ApiResponse(responseCode = "404", description = "Component not found")})
436     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
437     public Response getDataTypes(@PathParam("componentType") final String componentType, @PathParam("componentId") final String componentId,
438                                  @Context final HttpServletRequest request) {
439         ComponentsUtils componentsUtils = getComponentsUtils();
440         String url = request.getMethod() + " " + request.getRequestURI();
441         log.debug("(getDataType) Start handle request of {}", url);
442         Response response;
443         try {
444             Either<List<DataTypeDefinition>, StorageOperationStatus> getResult = businessLogic.getPrivateDataTypes(componentId);
445             if (getResult.isRight()) {
446                 ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(getResult.right().value());
447                 return buildErrorResponse(componentsUtils.getResponseFormat(actionStatus));
448             }
449             Object json = RepresentationUtils.toRepresentation(getResult.left().value());
450             return buildOkResponse(componentsUtils.getResponseFormat(ActionStatus.OK), json);
451         } catch (Exception e) {
452             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get data type from service + " + componentId);
453             log.debug("Get data type failed with exception", e);
454             response = buildErrorResponse(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
455             return response;
456         }
457     }
458
459     /**
460      * Deletes a data type from a component.
461      *
462      * @param componentType the container type (service, resource, ...)
463      * @param componentId   the container ID
464      * @param dataTypeName  the data type name to be deleted
465      * @param request       HttpServletRequest object
466      * @return operation result
467      */
468     @DELETE
469     @Path("/{componentType}/{componentId}/dataType/{dataTypeName}")
470     @Operation(description = "Delete data type from service", method = "DELETE", summary = "Delete service input", responses = {
471         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
472         @ApiResponse(responseCode = "200", description = "Data type deleted"),
473         @ApiResponse(responseCode = "403", description = "Restricted operation"),
474         @ApiResponse(responseCode = "404", description = "Data type not found")})
475     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
476     public Response deleteDataType(@PathParam("componentType") final String componentType, @PathParam("componentId") final String componentId,
477                                    @PathParam("dataTypeName") final String dataTypeName, @Context final HttpServletRequest request) {
478         ComponentsUtils componentsUtils = getComponentsUtils();
479         String url = request.getMethod() + " " + request.getRequestURI();
480         log.debug(START_HANDLE_REQUEST_OF, url);
481         Response response;
482         try {
483             Either<DataTypeDefinition, StorageOperationStatus> deleteResult = businessLogic.deletePrivateDataType(componentId, dataTypeName);
484             if (deleteResult.isRight()) {
485                 ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(deleteResult.right().value());
486                 return buildErrorResponse(componentsUtils.getResponseFormat(actionStatus));
487             }
488             Object json = RepresentationUtils.toRepresentation(deleteResult.left().value());
489             return buildOkResponse(componentsUtils.getResponseFormat(ActionStatus.OK), json);
490         } catch (Exception e) {
491             BeEcompErrorManager.getInstance()
492                 .logBeRestApiGeneralError("Delete data type for service + " + componentId + " + with name: " + dataTypeName);
493             log.debug("Delete data type failed with exception", e);
494             response = buildErrorResponse(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
495             return response;
496         }
497     }
498
499     private Response createInput(String componentId, String data, HttpServletRequest request, String userId) {
500         String url = request.getMethod() + " " + request.getRequestURI();
501         log.debug("Start handle request of {} modifier id is {} data is {}", url, userId, data);
502         loggerSupportability.log(LoggerSupportabilityActions.CREATE_INPUTS, StatusCode.STARTED, "CREATE_INPUTS by user {} ", userId);
503         try {
504             Either<Map<String, InputDefinition>, ActionStatus> inputDefinition = getInputModel(componentId, data);
505             if (inputDefinition.isRight()) {
506                 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(inputDefinition.right().value());
507                 return buildErrorResponse(responseFormat);
508             }
509             Map<String, InputDefinition> inputs = inputDefinition.left().value();
510             if (inputs == null || inputs.size() != 1) {
511                 log.info("Input content is invalid - {}", data);
512                 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT);
513                 return buildErrorResponse(responseFormat);
514             }
515             Map.Entry<String, InputDefinition> entry = inputs.entrySet().iterator().next();
516             InputDefinition newInputDefinition = entry.getValue();
517             newInputDefinition.setParentUniqueId(componentId);
518             String inputName = newInputDefinition.getName();
519             Either<EntryData<String, InputDefinition>, ResponseFormat> addInputEither = inputsBusinessLogic
520                 .addInputToComponent(componentId, inputName, newInputDefinition, userId);
521             if (addInputEither.isRight()) {
522                 return buildErrorResponse(addInputEither.right().value());
523             }
524             loggerSupportability.log(LoggerSupportabilityActions.CREATE_INPUTS, StatusCode.COMPLETE, "CREATE_INPUTS by user {} ", userId);
525             return buildOkResponse(newInputDefinition);
526         } catch (Exception e) {
527             BeEcompErrorManager.getInstance().logBeRestApiGeneralError(CREATE_INPUT);
528             log.debug("create input failed with exception", e);
529             ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
530             return buildErrorResponse(responseFormat);
531         }
532     }
533 }