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 java.io.InputStream;
24 import java.lang.reflect.Type;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.List;
30 import javax.inject.Singleton;
31 import javax.servlet.ServletContext;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.ws.rs.Consumes;
34 import javax.ws.rs.DELETE;
35 import javax.ws.rs.GET;
36 import javax.ws.rs.HeaderParam;
37 import javax.ws.rs.POST;
38 import javax.ws.rs.PUT;
39 import javax.ws.rs.Path;
40 import javax.ws.rs.PathParam;
41 import javax.ws.rs.Produces;
42 import javax.ws.rs.QueryParam;
43 import javax.ws.rs.core.Context;
44 import javax.ws.rs.core.MediaType;
45 import javax.ws.rs.core.Response;
47 import org.apache.commons.io.IOUtils;
48 import org.apache.commons.lang.StringUtils;
49 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
50 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
51 import org.openecomp.sdc.be.config.BeEcompErrorManager;
52 import org.openecomp.sdc.be.dao.api.ActionStatus;
53 import org.openecomp.sdc.be.datamodel.ForwardingPaths;
54 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
55 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
56 import org.openecomp.sdc.be.info.CreateAndAssotiateInfo;
57 import org.openecomp.sdc.be.info.GroupDefinitionInfo;
58 import org.openecomp.sdc.be.model.ComponentInstance;
59 import org.openecomp.sdc.be.model.ComponentInstanceInput;
60 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
61 import org.openecomp.sdc.be.model.PropertyConstraint;
62 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
63 import org.openecomp.sdc.be.model.Resource;
64 import org.openecomp.sdc.be.model.User;
65 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation.PropertyConstraintDeserialiser;
66 import org.openecomp.sdc.common.api.Constants;
67 import org.openecomp.sdc.common.datastructure.Wrapper;
68 import org.openecomp.sdc.exception.ResponseFormat;
69 import org.slf4j.Logger;
70 import org.slf4j.LoggerFactory;
72 import com.fasterxml.jackson.databind.ObjectMapper;
73 import com.google.gson.Gson;
74 import com.google.gson.GsonBuilder;
75 import com.google.gson.reflect.TypeToken;
76 import com.jcabi.aspects.Loggable;
78 import fj.data.Either;
79 import io.swagger.annotations.Api;
80 import io.swagger.annotations.ApiOperation;
81 import io.swagger.annotations.ApiParam;
82 import io.swagger.annotations.ApiResponse;
83 import io.swagger.annotations.ApiResponses;
86 * Root resource (exposed at "/" path) .json
88 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
90 @Api(value = "Resource Instance Servlet")
92 public class ComponentInstanceServlet extends AbstractValidationsServlet {
94 private static final Logger log = LoggerFactory.getLogger(ComponentInstanceServlet.class);
96 Type constraintType = new TypeToken<PropertyConstraint>() {
99 Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyConstraintDeserialiser()).create();
102 @Path("/{containerComponentType}/{componentId}/resourceInstance")
103 @Consumes(MediaType.APPLICATION_JSON)
104 @Produces(MediaType.APPLICATION_JSON)
105 @ApiOperation(value = "Create ComponentInstance", httpMethod = "POST", notes = "Returns created ComponentInstance", response = Response.class)
106 @ApiResponses(value = { @ApiResponse(code = 201, message = "Component created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
107 @ApiResponse(code = 409, message = "Component instance already exist") })
108 public Response createComponentInstance(@ApiParam(value = "RI object to be created", required = true) String data, @PathParam("componentId") final String containerComponentId,
109 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
110 @HeaderParam(value = Constants.USER_ID_HEADER) @ApiParam(value = "USER_ID of modifier user", required = true) String userId, @Context final HttpServletRequest request) {
111 ServletContext context = request.getSession().getServletContext();
115 ComponentInstance componentInstance = RepresentationUtils.fromRepresentation(data, ComponentInstance.class);
116 componentInstance.setInvariantName(null);
117 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
118 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
119 if (componentInstanceLogic == null) {
120 log.debug("Unsupported component type {}", containerComponentType);
121 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
123 Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.createComponentInstance(containerComponentType, containerComponentId, userId, componentInstance);
125 if (actionResponse.isRight()) {
126 return buildErrorResponse(actionResponse.right().value());
128 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), actionResponse.left().value());
130 } catch (Exception e) {
131 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create Component Instance");
132 log.debug("create component instance failed with exception", e);
133 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
138 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}")
139 @Consumes(MediaType.APPLICATION_JSON)
140 @Produces(MediaType.APPLICATION_JSON)
141 @ApiOperation(value = "Update resource instance", httpMethod = "POST", notes = "Returns updated resource instance", response = Response.class)
142 @ApiResponses(value = { @ApiResponse(code = 200, message = "Resource instance updated"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
143 public Response updateComponentInstanceMetadata(@PathParam("componentId") final String componentId, @PathParam("componentInstanceId") final String componentInstanceId,
144 @ApiParam(value = "valid values: resources / services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
145 + ComponentTypeEnum.PRODUCT_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
146 @Context final HttpServletRequest request) {
147 ServletContext context = request.getSession().getServletContext();
149 String url = request.getMethod() + " " + request.getRequestURI();
150 log.debug("Start handle request of {}", url);
153 log.debug("Start handle request of {}", url);
155 InputStream inputStream = request.getInputStream();
157 byte[] bytes = IOUtils.toByteArray(inputStream);
159 if (bytes == null || bytes.length == 0) {
160 log.info("Empty body was sent.");
161 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
164 String userId = request.getHeader(Constants.USER_ID_HEADER);
166 String data = new String(bytes);
167 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
168 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
169 if (componentInstanceLogic == null) {
170 log.debug("Unsupported component type {}", containerComponentType);
171 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
173 Either<ComponentInstance, ResponseFormat> convertResponse = convertToResourceInstance(data);
175 if (convertResponse.isRight()) {
176 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - updateResourceInstance");
177 log.debug("Failed to convert received data to BE format.");
178 return buildErrorResponse(convertResponse.right().value());
181 ComponentInstance resourceInstance = convertResponse.left().value();
182 Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.updateComponentInstanceMetadata(containerComponentType, componentId, componentInstanceId, userId, resourceInstance);
184 if (actionResponse.isRight()) {
185 return buildErrorResponse(actionResponse.right().value());
187 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
189 } catch (Exception e) {
190 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Resource Instance");
191 log.debug("update resource instance with exception", e);
192 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
198 @Path("/{containerComponentType}/{componentId}/resourceInstance/multipleComponentInstance")
199 @Consumes(MediaType.APPLICATION_JSON)
200 @Produces(MediaType.APPLICATION_JSON)
201 @ApiOperation(value = "Update resource instance multiple component", httpMethod = "POST", notes = "Returns updated resource instance", response = Response.class)
202 @ApiResponses(value = { @ApiResponse(code = 200, message = "Resource instance updated"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
203 public Response updateMultipleComponentInstance(@PathParam("componentId") final String componentId,
204 @ApiParam(value = "valid values: resources / services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
205 + ComponentTypeEnum.PRODUCT_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
206 @Context final HttpServletRequest request, @ApiParam(value = "Component Instance JSON Array", required = true) final String componentInstanceJsonArray) {
208 ServletContext context = request.getSession().getServletContext();
209 String url = request.getMethod() + " " + request.getRequestURI();
210 log.debug("Start handle request of {}", url);
213 log.debug("Start handle request of {}", url);
215 if (componentInstanceJsonArray == null || componentInstanceJsonArray.length() == 0) {
216 log.info("Empty JSON list was sent.");
217 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
220 String userId = request.getHeader(Constants.USER_ID_HEADER);
222 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
223 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
224 if (componentInstanceLogic == null) {
225 log.debug("Unsupported component type {}", containerComponentType);
226 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
229 Either<List<ComponentInstance>, ResponseFormat> convertResponse = convertToMultipleResourceInstance(componentInstanceJsonArray);
231 if (convertResponse.isRight()) {
232 // Using both ECOMP error methods, show to Sofer
233 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - updateResourceInstance");
234 log.debug("Failed to convert received data to BE format.");
235 return buildErrorResponse(convertResponse.right().value());
238 List<ComponentInstance> componentInstanceList = convertResponse.left().value();
240 Either<List<ComponentInstance>, ResponseFormat> actionResponse = componentInstanceLogic.updateComponentInstance(containerComponentType, componentId, userId, componentInstanceList, true, true);
242 if (actionResponse.isRight()) {
243 return buildErrorResponse(actionResponse.right().value());
246 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
248 } catch (Exception e) {
249 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Resource Instance");
250 log.debug("update resource instance with exception", e);
251 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
257 @Path("/{containerComponentType}/{componentId}/resourceInstance/{resourceInstanceId}")
258 @Consumes(MediaType.APPLICATION_JSON)
259 @Produces(MediaType.APPLICATION_JSON)
260 @ApiOperation(value = "Delete ResourceInstance", httpMethod = "DELETE", notes = "Returns delete resourceInstance", response = Response.class)
261 @ApiResponses(value = { @ApiResponse(code = 201, message = "ResourceInstance deleted"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
262 public Response deleteResourceInstance(@PathParam("componentId") final String componentId, @PathParam("resourceInstanceId") final String resourceInstanceId,
263 @ApiParam(value = "valid values: resources / services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
264 + ComponentTypeEnum.PRODUCT_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
265 @Context final HttpServletRequest request) {
266 ServletContext context = request.getSession().getServletContext();
267 String url = request.getMethod() + " " + request.getRequestURI();
268 Response response = null;
270 log.debug("Start handle request of {}", url);
271 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
272 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
273 if (componentInstanceLogic == null) {
274 log.debug("Unsupported component type {}", containerComponentType);
275 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
277 String userId = request.getHeader(Constants.USER_ID_HEADER);
278 Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.deleteComponentInstance(containerComponentType, componentId, resourceInstanceId, userId);
280 if (actionResponse.isRight()) {
281 response = buildErrorResponse(actionResponse.right().value());
283 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT), null);
286 } catch (Exception e) {
287 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Resource Instance");
288 log.debug("delete resource instance with exception", e);
289 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
293 @ApiParam(value = "allowed values are resources /services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + "," + ComponentTypeEnum.PRODUCT_PARAM_NAME, required = true)
295 @Path("/{containerComponentType}/{componentId}/resourceInstance/associate")
296 @Consumes(MediaType.APPLICATION_JSON)
297 @Produces(MediaType.APPLICATION_JSON)
298 @ApiOperation(value = "Associate RI to RI", httpMethod = "POST", notes = "Returns created RelationshipInfo", response = Response.class)
299 @ApiResponses(value = { @ApiResponse(code = 201, message = "Relationship created"), @ApiResponse(code = 403, message = "Missing information"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
300 @ApiResponse(code = 409, message = "Relationship already exist") })
301 public Response associateRIToRI(@ApiParam(value = "unique id of the container component") @PathParam("componentId") final String componentId,
302 @ApiParam(value = "allowed values are resources /services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
303 + ComponentTypeEnum.PRODUCT_PARAM_NAME, required = true) @PathParam("containerComponentType") final String containerComponentType,
304 @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @ApiParam(value = "RelationshipInfo", required = true) String data, @Context final HttpServletRequest request) {
305 ServletContext context = request.getSession().getServletContext();
307 String url = request.getMethod() + " " + request.getRequestURI();
308 log.debug("Start handle request of {}", url);
309 Response response = null;
313 log.debug("Start handle request of {}", url);
315 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
316 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
317 if (componentInstanceLogic == null) {
318 log.debug("Unsupported component type {}", containerComponentType);
319 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
322 Either<RequirementCapabilityRelDef, ResponseFormat> regInfoW = convertToRequirementCapabilityRelDef(data);
324 Either<RequirementCapabilityRelDef, ResponseFormat> resultOp;
325 if (regInfoW.isRight()) {
326 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - associateRIToRI");
327 log.debug("Failed to convert received data to BE format.");
328 resultOp = Either.right(regInfoW.right().value());
330 RequirementCapabilityRelDef requirementDef = regInfoW.left().value();
331 resultOp = componentInstanceLogic.associateRIToRI(componentId, userId, requirementDef, componentTypeEnum);
334 Either<RequirementCapabilityRelDef, ResponseFormat> actionResponse = resultOp;
336 if (actionResponse.isRight()) {
337 response = buildErrorResponse(actionResponse.right().value());
339 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
343 } catch (Exception e) {
344 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Associate Resource Instance");
345 log.debug("associate resource instance to another RI with exception", e);
346 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
351 @Path("/{containerComponentType}/{componentId}/resourceInstance/dissociate")
352 @Consumes(MediaType.APPLICATION_JSON)
353 @Produces(MediaType.APPLICATION_JSON)
354 @ApiOperation(value = "Dissociate RI from RI", httpMethod = "PUT", notes = "Returns deleted RelationshipInfo", response = Response.class)
355 @ApiResponses(value = { @ApiResponse(code = 201, message = "Relationship deleted"), @ApiResponse(code = 403, message = "Missing information"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
356 public Response dissociateRIFromRI(
357 @ApiParam(value = "allowed values are resources /services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
358 + ComponentTypeEnum.PRODUCT_PARAM_NAME, required = true) @PathParam("containerComponentType") final String containerComponentType,
359 @ApiParam(value = "unique id of the container component") @PathParam("componentId") final String componentId, @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
360 @ApiParam(value = "RelationshipInfo", required = true) String data, @Context final HttpServletRequest request) {
361 ServletContext context = request.getSession().getServletContext();
363 String url = request.getMethod() + " " + request.getRequestURI();
364 log.debug("Start handle request of {}", url);
368 log.debug("Start handle request of {}", url);
370 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
371 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
372 if (componentInstanceLogic == null) {
373 log.debug("Unsupported component type {}", containerComponentType);
374 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
377 Either<RequirementCapabilityRelDef, ResponseFormat> regInfoW = convertToRequirementCapabilityRelDef(data);
378 if (regInfoW.isRight()) {
379 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - dissociateRIFromRI");
380 log.debug("Failed to convert received data to BE format.");
381 return buildErrorResponse(regInfoW.right().value());
384 RequirementCapabilityRelDef requirementDef = regInfoW.left().value();
385 Either<RequirementCapabilityRelDef, ResponseFormat> actionResponse = componentInstanceLogic.dissociateRIFromRI(componentId, userId, requirementDef, componentTypeEnum);
387 if (actionResponse.isRight()) {
388 return buildErrorResponse(actionResponse.right().value());
390 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
392 } catch (Exception e) {
393 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Dissociate Resource Instance");
394 log.debug("dissociate resource instance from service failed with exception", e);
395 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
400 @Path("/{containerComponentType}/{componentId}/resourceInstance/createAndAssociate")
401 @Consumes(MediaType.APPLICATION_JSON)
402 @Produces(MediaType.APPLICATION_JSON)
403 @ApiOperation(value = "Create RI and associate RI to RI", httpMethod = "POST", notes = "Returns created RI and RelationshipInfo", response = Response.class)
404 @ApiResponses(value = { @ApiResponse(code = 201, message = "RI created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
405 @ApiResponse(code = 409, message = "Relationship already exist") })
406 public Response createAndAssociateRIToRI(@PathParam("componentId") final String componentId,
407 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
408 @Context final HttpServletRequest request) {
409 ServletContext context = request.getSession().getServletContext();
411 String url = request.getMethod() + " " + request.getRequestURI();
412 log.debug("Start handle request of {}", url);
415 log.debug("Start handle request of {}", url);
417 InputStream inputStream = request.getInputStream();
419 byte[] bytes = IOUtils.toByteArray(inputStream);
421 if (bytes == null || bytes.length == 0) {
422 log.info("Empty body was sent.");
423 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
426 String userId = request.getHeader(Constants.USER_ID_HEADER);
428 String data = new String(bytes);
430 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
431 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
432 if (componentInstanceLogic == null) {
433 log.debug("Unsupported component type {}", containerComponentType);
434 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
437 Either<CreateAndAssotiateInfo, ActionStatus> convertStatus = convertJsonToObject(data, CreateAndAssotiateInfo.class);
438 if (convertStatus.isRight()) {
439 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - createAndAssociateRIToRI");
440 log.debug("Failed to convert received data to BE format.");
441 Either<Object, ResponseFormat> formattedResponse = Either.right(getComponentsUtils().getResponseFormat(convertStatus.right().value()));
442 return buildErrorResponse(formattedResponse.right().value());
445 CreateAndAssotiateInfo createAndAssotiateInfo = convertStatus.left().value();
446 Either<CreateAndAssotiateInfo, ResponseFormat> actionResponse = componentInstanceLogic.createAndAssociateRIToRI(containerComponentType, componentId, userId, createAndAssotiateInfo);
448 if (actionResponse.isRight()) {
449 return buildErrorResponse(actionResponse.right().value());
451 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), actionResponse.left().value());
452 } catch (Exception e) {
453 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create and Associate Resource Instance");
454 log.debug("create and associate RI failed with exception", e);
455 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
460 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/properties")
461 @Consumes(MediaType.APPLICATION_JSON)
462 @Produces(MediaType.APPLICATION_JSON)
463 @ApiOperation(value = "Update resource instance property", httpMethod = "POST", notes = "Returns updated resource instance property", response = Response.class)
464 @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
465 public Response updateResourceInstanceProperties(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
466 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
467 @ApiParam(value = "resource instance id") @PathParam("componentInstanceId") final String componentInstanceId, @ApiParam(value = "id of user initiating the operation") @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
468 @Context final HttpServletRequest request, @ApiParam(value = "Component Instance Properties JSON Array", required = true) final String componentInstancePropertiesJsonArray) {
470 String url = request.getMethod() + " " + request.getRequestURI();
471 log.debug("Start handle request of {}", url);
474 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
475 List<ComponentInstanceProperty> propertiesToUpdate = new ArrayList<>();
476 if (errorWrapper.isEmpty()) {
477 Either<List<ComponentInstanceProperty>, ResponseFormat> propertiesToUpdateEither = convertMultipleProperties(componentInstancePropertiesJsonArray);
478 if (propertiesToUpdateEither.isRight()) {
479 errorWrapper.setInnerElement(propertiesToUpdateEither.right().value());
481 propertiesToUpdate = propertiesToUpdateEither.left().value();
485 if (!errorWrapper.isEmpty()) {
486 return buildErrorResponse(errorWrapper.getInnerElement());
489 log.debug("Start handle request of updateResourceInstanceProperty. Received property is {}", propertiesToUpdate);
491 ServletContext context = request.getSession().getServletContext();
493 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
494 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
495 if (componentInstanceLogic == null) {
496 log.debug("Unsupported component type {}", containerComponentType);
497 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
500 Either<List<ComponentInstanceProperty>, ResponseFormat> actionResponse = componentInstanceLogic.createOrUpdatePropertiesValues(componentTypeEnum, componentId, componentInstanceId, propertiesToUpdate, userId);
502 if (actionResponse.isRight()) {
503 return buildErrorResponse(actionResponse.right().value());
506 List<ComponentInstanceProperty> resourceInstanceProperties = actionResponse.left().value();
507 ObjectMapper mapper = new ObjectMapper();
508 String result = mapper.writeValueAsString(resourceInstanceProperties);
509 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
511 } catch (Exception e) {
512 log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
513 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
519 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/inputs")
520 @Consumes(MediaType.APPLICATION_JSON)
521 @Produces(MediaType.APPLICATION_JSON)
522 @ApiOperation(value = "Update resource instance property", httpMethod = "POST", notes = "Returns updated resource instance property", response = Response.class)
523 @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
524 public Response updateResourceInstanceInput(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
525 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
526 @ApiParam(value = "resource instance id") @PathParam("componentInstanceId") final String componentInstanceId, @ApiParam(value = "id of user initiating the operation") @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
527 @Context final HttpServletRequest request, @ApiParam(value = "Component Instance Properties JSON Array", required = true) final String componentInstanceInputsJsonArray) {
529 String url = request.getMethod() + " " + request.getRequestURI();
530 log.debug("Start handle request of {}", url);
533 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
534 List<ComponentInstanceInput> inputsToUpdate = new ArrayList<>();
535 if (errorWrapper.isEmpty()) {
536 Either<List<ComponentInstanceInput>, ResponseFormat> inputsToUpdateEither = convertMultipleInputs(componentInstanceInputsJsonArray);
537 if (inputsToUpdateEither.isRight()) {
538 errorWrapper.setInnerElement(inputsToUpdateEither.right().value());
540 inputsToUpdate = inputsToUpdateEither.left().value();
543 if (!errorWrapper.isEmpty()) {
544 return buildErrorResponse(errorWrapper.getInnerElement());
547 log.debug("Start handle request of updateResourceInstanceProperty. Received property is {}", inputsToUpdate);
549 ServletContext context = request.getSession().getServletContext();
551 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
552 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
553 if (componentInstanceLogic == null) {
554 log.debug("Unsupported component type {}", containerComponentType);
555 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
558 Either<List<ComponentInstanceInput>, ResponseFormat> actionResponse =
559 componentInstanceLogic.createOrUpdateInstanceInputValues(componentTypeEnum, componentId, componentInstanceId, inputsToUpdate, userId);
561 if (actionResponse.isRight()) {
562 return buildErrorResponse(actionResponse.right().value());
565 List<ComponentInstanceInput> resourceInstanceInput = actionResponse.left().value();
566 ObjectMapper mapper = new ObjectMapper();
567 String result = mapper.writeValueAsString(resourceInstanceInput);
568 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
570 } catch (Exception e) {
571 log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
572 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
578 * Updates ResourceInstance Attribute
581 * @param containerComponentType
582 * @param componentInstanceId
588 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/attribute")
589 @Consumes(MediaType.APPLICATION_JSON)
590 @Produces(MediaType.APPLICATION_JSON)
591 @ApiOperation(value = "Update resource instance attribute", httpMethod = "POST", notes = "Returns updated resource instance attribute", response = Response.class)
592 @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
593 public Response updateResourceInstanceAttribute(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
594 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
595 @ApiParam(value = "resource instance id") @PathParam("componentInstanceId") final String componentInstanceId, @ApiParam(value = "id of user initiating the operation") @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
596 @Context final HttpServletRequest request) {
598 String url = request.getMethod() + " " + request.getRequestURI();
599 log.debug("Start handle request of {}", url);
603 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
604 Wrapper<String> dataWrapper = new Wrapper<>();
605 Wrapper<ComponentInstanceProperty> attributeWrapper = new Wrapper<>();
606 Wrapper<ComponentInstanceBusinessLogic> blWrapper = new Wrapper<>();
608 validateInputStream(request, dataWrapper, errorWrapper);
610 if (errorWrapper.isEmpty()) {
611 validateClassParse(dataWrapper.getInnerElement(), attributeWrapper, () -> ComponentInstanceProperty.class, errorWrapper);
614 if (errorWrapper.isEmpty()) {
615 validateComponentInstanceBusinessLogic(request, containerComponentType, blWrapper, errorWrapper);
618 if (errorWrapper.isEmpty()) {
619 ComponentInstanceBusinessLogic componentInstanceLogic = blWrapper.getInnerElement();
620 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
621 log.debug("Start handle request of ComponentInstanceAttribute. Received attribute is {}", attributeWrapper.getInnerElement());
622 Either<ComponentInstanceProperty, ResponseFormat> eitherAttribute = componentInstanceLogic.createOrUpdateAttributeValue(componentTypeEnum, componentId, componentInstanceId, attributeWrapper.getInnerElement(), userId);
623 if (eitherAttribute.isRight()) {
624 errorWrapper.setInnerElement(eitherAttribute.right().value());
626 attributeWrapper.setInnerElement(eitherAttribute.left().value());
630 return buildResponseFromElement(errorWrapper, attributeWrapper);
632 } catch (Exception e) {
633 log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
634 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
640 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/property/{propertyId}")
641 @Consumes(MediaType.APPLICATION_JSON)
642 @Produces(MediaType.APPLICATION_JSON)
643 @ApiOperation(value = "Update resource instance", httpMethod = "DELETE", notes = "Returns deleted resource instance property", response = Response.class)
644 @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
645 public Response deleteResourceInstanceProperty(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
646 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
647 @ApiParam(value = "resource instance id") @PathParam("componentInstanceId") final String componentInstanceId, @ApiParam(value = "property id") @PathParam("propertyId") final String propertyId,
648 @ApiParam(value = "id of user initiating the operation") @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Context final HttpServletRequest request) {
650 ServletContext context = request.getSession().getServletContext();
652 String url = request.getMethod() + " " + request.getRequestURI();
653 log.debug("Start handle request of {}", url);
656 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
657 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
658 if (componentInstanceLogic == null) {
659 log.debug("Unsupported component type {}", containerComponentType);
660 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
663 Either<ComponentInstanceProperty, ResponseFormat> actionResponse = componentInstanceLogic.deletePropertyValue(componentTypeEnum, componentId, componentInstanceId, propertyId, userId);
664 if (actionResponse.isRight()) {
665 return buildErrorResponse(actionResponse.right().value());
667 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT), null);
668 } catch (Exception e) {
669 log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
670 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
676 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/changeVersion")
677 @Consumes(MediaType.APPLICATION_JSON)
678 @Produces(MediaType.APPLICATION_JSON)
679 @ApiOperation(value = "Update resource instance", httpMethod = "POST", notes = "Returns updated resource instance", response = Response.class)
680 @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
681 public Response changeResourceInstanceVersion(@PathParam("componentId") final String componentId, @PathParam("componentInstanceId") final String componentInstanceId,
682 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
683 @Context final HttpServletRequest request) {
684 ServletContext context = request.getSession().getServletContext();
686 String url = request.getMethod() + " " + request.getRequestURI();
687 log.debug("Start handle request of {}", url);
688 try ( InputStream inputStream = request.getInputStream()) {
690 byte[] bytes = IOUtils.toByteArray(inputStream);
692 if (bytes == null || bytes.length == 0) {
693 log.info("Empty body was sent.");
694 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
697 String userId = request.getHeader(Constants.USER_ID_HEADER);
699 String data = new String(bytes);
701 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
702 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
703 if (componentInstanceLogic == null) {
704 log.debug("Unsupported component type {}", containerComponentType);
705 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
708 Either<ComponentInstance, ResponseFormat> convertResponse = convertToResourceInstance(data);
710 if (convertResponse.isRight()) {
711 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - updateResourceInstance");
712 log.debug("Failed to convert received data to BE format.");
713 return buildErrorResponse(convertResponse.right().value());
716 ComponentInstance newResourceInstance = convertResponse.left().value();
717 Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.changeComponentInstanceVersion(containerComponentType, componentId, componentInstanceId, userId, newResourceInstance);
719 if (actionResponse.isRight()) {
720 return buildErrorResponse(actionResponse.right().value());
722 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
724 } catch (Exception e) {
725 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Resource Instance");
726 log.debug("update resource instance with exception", e);
727 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
733 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/groupInstance/{groupInstanceId}/property")
734 @Consumes(MediaType.APPLICATION_JSON)
735 @Produces(MediaType.APPLICATION_JSON)
736 @ApiOperation(value = "Update resource instance property", httpMethod = "POST", notes = "Returns updated resource instance property", response = Response.class)
737 @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
738 public Response updateGroupInstanceProperty(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
739 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
740 @ApiParam(value = "resource instance id") @PathParam("componentInstanceId") final String componentInstanceId, @ApiParam(value = "group instance id") @PathParam("groupInstanceId") final String groupInstanceId,
741 @ApiParam(value = "id of user initiating the operation") @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Context final HttpServletRequest request) {
743 String url = request.getMethod() + " " + request.getRequestURI();
744 log.debug("Start handle request of {}", url);
747 Wrapper<String> dataWrapper = new Wrapper<>();
748 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
749 Wrapper<ComponentInstanceProperty> propertyWrapper = new Wrapper<>();
751 validateInputStream(request, dataWrapper, errorWrapper);
753 if (errorWrapper.isEmpty()) {
754 validateClassParse(dataWrapper.getInnerElement(), propertyWrapper, () -> ComponentInstanceProperty.class, errorWrapper);
757 if (!errorWrapper.isEmpty()) {
758 return buildErrorResponse(errorWrapper.getInnerElement());
761 ComponentInstanceProperty property = propertyWrapper.getInnerElement();
763 log.debug("Start handle request of updateResourceInstanceProperty. Received property is {}", property);
765 ServletContext context = request.getSession().getServletContext();
767 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
768 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
769 if (componentInstanceLogic == null) {
770 log.debug("Unsupported component type {}", containerComponentType);
771 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
774 Either<ComponentInstanceProperty, ResponseFormat> actionResponse = componentInstanceLogic.createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, componentInstanceId, groupInstanceId, property, userId);
776 if (actionResponse.isRight()) {
777 return buildErrorResponse(actionResponse.right().value());
780 ComponentInstanceProperty resourceInstanceProperty = actionResponse.left().value();
781 ObjectMapper mapper = new ObjectMapper();
782 String result = mapper.writeValueAsString(resourceInstanceProperty);
783 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
785 } catch (Exception e) {
786 log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
787 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
793 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/groupInstance/{groupInstId}")
794 @Consumes(MediaType.APPLICATION_JSON)
795 @Produces(MediaType.APPLICATION_JSON)
796 @ApiOperation(value = "Get group artifacts ", httpMethod = "GET", notes = "Returns artifacts metadata according to groupInstId", response = Resource.class)
797 @ApiResponses(value = { @ApiResponse(code = 200, message = "group found"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Group not found") })
798 public Response getGroupArtifactById(@PathParam("containerComponentType") final String containerComponentType, @PathParam("componentId") final String componentId, @PathParam("componentInstanceId") final String componentInstanceId,
799 @PathParam("groupInstId") final String groupInstId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
800 ServletContext context = request.getSession().getServletContext();
801 String url = request.getMethod() + " " + request.getRequestURI();
802 log.debug("(GET) Start handle request of {}", url);
806 GroupBusinessLogic businessLogic = this.getGroupBL(context);
807 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
808 Either<GroupDefinitionInfo, ResponseFormat> actionResponse = businessLogic.getGroupInstWithArtifactsById(componentTypeEnum, componentId, componentInstanceId, groupInstId, userId, false);
810 if (actionResponse.isRight()) {
811 log.debug("failed to get all non abstract {}", containerComponentType);
812 return buildErrorResponse(actionResponse.right().value());
815 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
817 } catch (Exception e) {
818 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("getGroupArtifactById");
819 log.debug("getGroupArtifactById unexpected exception", e);
820 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
827 @Path("/{containerComponentType}/{containerComponentId}/componentInstances/{componentInstanceUniqueId}/properties")
828 @Consumes(MediaType.APPLICATION_JSON)
829 @Produces(MediaType.APPLICATION_JSON)
830 @ApiOperation(value = "Get component instance properties", httpMethod = "GET", notes = "Returns component instance properties", response = Response.class)
831 @ApiResponses(value = { @ApiResponse(code = 200, message = "Properties found"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Component/Component Instance - not found") })
832 public Response getInstancePropertiesById(@PathParam("containerComponentType") final String containerComponentType, @PathParam("containerComponentId") final String containerComponentId,
833 @PathParam("componentInstanceUniqueId") final String componentInstanceUniqueId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
835 ServletContext context = request.getSession().getServletContext();
836 String url = request.getMethod() + " " + request.getRequestURI();
837 log.debug("(GET) Start handle request of {}", url);
840 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
841 ComponentInstanceBusinessLogic componentInstanceBL = getComponentInstanceBL(context, componentTypeEnum);
843 Either<List<ComponentInstanceProperty>, ResponseFormat> componentInstancePropertiesById = componentInstanceBL.getComponentInstancePropertiesById(containerComponentType, containerComponentId, componentInstanceUniqueId, userId);
845 if (componentInstancePropertiesById.isRight()) {
846 log.debug("Failed to get properties of component instance ID: {} in {} with ID: {}", componentInstanceUniqueId, containerComponentType, containerComponentId);
847 return buildErrorResponse(componentInstancePropertiesById.right().value());
850 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), componentInstancePropertiesById.left().value());
851 } catch (Exception e) {
852 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("getGroupArtifactById");
853 log.debug("getGroupArtifactById unexpected exception", e);
854 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
861 @Path("/{containerComponentType}/{containerComponentId}/componentInstances/{componentInstanceUniqueId}/capability/{capabilityType}/capabilityName/{capabilityName}/ownerId/{ownerId}/properties")
862 @Consumes(MediaType.APPLICATION_JSON)
863 @Produces(MediaType.APPLICATION_JSON)
864 @ApiOperation(value = "Get component instance capability properties", httpMethod = "GET", notes = "Returns component instance capability properties", response = Response.class)
865 @ApiResponses(value = { @ApiResponse(code = 200, message = "Properties found"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Component/Component Instance/Capability - not found") })
866 public Response getInstanceCapabilityPropertiesById(@PathParam("containerComponentType") final String containerComponentType, @PathParam("containerComponentId") final String containerComponentId,
867 @PathParam("componentInstanceUniqueId") final String componentInstanceUniqueId, @PathParam("capabilityType") final String capabilityType, @PathParam("capabilityName") final String capabilityName, @PathParam("ownerId") final String ownerId, @Context final HttpServletRequest request,
868 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
870 ServletContext context = request.getSession().getServletContext();
871 String url = request.getMethod() + " " + request.getRequestURI();
872 log.debug("(GET) Start handle request of {}", url);
875 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
876 ComponentInstanceBusinessLogic componentInstanceBL = getComponentInstanceBL(context, componentTypeEnum);
878 Either<List<ComponentInstanceProperty>, ResponseFormat> componentInstancePropertiesById = componentInstanceBL.getComponentInstanceCapabilityPropertiesById(containerComponentType, containerComponentId, componentInstanceUniqueId,
879 capabilityType, capabilityName, ownerId, userId);
881 if (componentInstancePropertiesById.isRight()) {
882 log.debug("Failed to get properties of component instance ID: {} in {} with ID: {}", componentInstanceUniqueId, containerComponentType, containerComponentId);
883 return buildErrorResponse(componentInstancePropertiesById.right().value());
886 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), componentInstancePropertiesById.left().value());
887 } catch (Exception e) {
888 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("getGroupArtifactById");
889 log.debug("getGroupArtifactById unexpected exception", e);
890 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
897 @Path("/{containerComponentType}/{containerComponentId}/componentInstances/{componentInstanceUniqueId}/capability/{capabilityType}/capabilityName/{capabilityName}/ownerId/{ownerId}/properties")
898 @Consumes(MediaType.APPLICATION_JSON)
899 @Produces(MediaType.APPLICATION_JSON)
900 @ApiOperation(value = "Update Instance Capabilty Property", httpMethod = "PUT", notes = "Returns updated property", response = Response.class)
901 @ApiResponses(value = { @ApiResponse(code = 200, message = "Resource instance capabilty property updated"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
902 @ApiResponse(code = 404, message = "Component/Component Instance/Capability - not found") })
903 public Response updateInstanceCapabilityProperty(@PathParam("containerComponentType") final String containerComponentType, @PathParam("containerComponentId") final String containerComponentId,
904 @PathParam("componentInstanceUniqueId") final String componentInstanceUniqueId, @PathParam("capabilityType") final String capabilityType, @PathParam("capabilityName") final String capabilityName, @PathParam("ownerId") final String ownerId,
905 @ApiParam(value = "Instance capabilty property to update", required = true) String data, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
906 ServletContext context = request.getSession().getServletContext();
907 String url = request.getMethod() + " " + request.getRequestURI();
908 log.debug("(PUT) Start handle request of {}", url);
910 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
911 List<ComponentInstanceProperty> propertiesToUpdate = new ArrayList<>();
912 if (errorWrapper.isEmpty()) {
913 Either<List<ComponentInstanceProperty>, ResponseFormat> propertiesToUpdateEither = convertMultipleProperties(data);
914 if (propertiesToUpdateEither.isRight()) {
915 errorWrapper.setInnerElement(propertiesToUpdateEither.right().value());
917 propertiesToUpdate = propertiesToUpdateEither.left().value();
921 if (!errorWrapper.isEmpty()) {
922 return buildErrorResponse(errorWrapper.getInnerElement());
925 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
926 ComponentInstanceBusinessLogic componentInstanceBL = getComponentInstanceBL(context, componentTypeEnum);
928 Either<List<ComponentInstanceProperty>, ResponseFormat> updateCICapProperty = componentInstanceBL.updateInstanceCapabilityProperties(componentTypeEnum, containerComponentId, componentInstanceUniqueId, capabilityType, capabilityName, propertiesToUpdate, userId);
930 if (updateCICapProperty.isRight()) {
931 log.debug("Failed to get properties of component instance ID: {} in {} with ID: {}", componentInstanceUniqueId, containerComponentType, containerComponentId);
932 return buildErrorResponse(updateCICapProperty.right().value());
935 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), updateCICapProperty.left().value());
936 } catch (Exception e) {
937 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("getGroupArtifactById");
938 log.debug("getGroupArtifactById unexpected exception", e);
939 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
944 @Path("/{containerComponentType}/{containerComponentId}/serviceProxy")
945 @Consumes(MediaType.APPLICATION_JSON)
946 @Produces(MediaType.APPLICATION_JSON)
947 @ApiOperation(value = "Create service proxy", httpMethod = "POST", notes = "Returns created service proxy", response = Response.class)
948 @ApiResponses(value = { @ApiResponse(code = 201, message = "Service proxy created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
949 @ApiResponse(code = 409, message = "Service proxy already exist") })
950 public Response createServiceProxy(@ApiParam(value = "RI object to be created", required = true) String data, @PathParam("containerComponentId") final String containerComponentId,
951 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
952 @HeaderParam(value = Constants.USER_ID_HEADER) @ApiParam(value = "USER_ID of modifier user", required = true) String userId, @Context final HttpServletRequest request) {
953 ServletContext context = request.getSession().getServletContext();
957 ComponentInstance componentInstance = RepresentationUtils.fromRepresentation(data, ComponentInstance.class);
958 componentInstance.setInvariantName(null);
959 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
960 if (componentTypeEnum != ComponentTypeEnum.SERVICE) {
961 log.debug("Unsupported container component type {}", containerComponentType);
962 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
964 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
965 if (componentInstanceLogic == null) {
966 log.debug("Unsupported component type {}", containerComponentType);
967 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
969 Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.createServiceProxy(containerComponentType, containerComponentId, userId, componentInstance);
971 if (actionResponse.isRight()) {
972 return buildErrorResponse(actionResponse.right().value());
974 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), actionResponse.left().value());
976 } catch (Exception e) {
977 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create service proxy");
978 log.debug("Create service proxy failed with exception", e);
979 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
984 @Path("/{containerComponentType}/{containerComponentId}/serviceProxy/{serviceProxyId}")
985 @Consumes(MediaType.APPLICATION_JSON)
986 @Produces(MediaType.APPLICATION_JSON)
987 @ApiOperation(value = "Delete service proxy", httpMethod = "DELETE", notes = "Returns delete service proxy", response = Response.class)
988 @ApiResponses(value = { @ApiResponse(code = 201, message = "Service proxy deleted"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
989 public Response deleteServiceProxy(@PathParam("containerComponentId") final String containerComponentId, @PathParam("serviceProxyId") final String serviceProxyId,
990 @ApiParam(value = "valid values: resources / services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
991 + ComponentTypeEnum.PRODUCT_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
992 @Context final HttpServletRequest request) {
993 ServletContext context = request.getSession().getServletContext();
994 String url = request.getMethod() + " " + request.getRequestURI();
995 Response response = null;
997 log.debug("Start handle request of {}", url);
998 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
999 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
1000 if (componentInstanceLogic == null) {
1001 log.debug("Unsupported component type {}", containerComponentType);
1002 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
1004 String userId = request.getHeader(Constants.USER_ID_HEADER);
1005 Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.deleteServiceProxy(containerComponentType, containerComponentId, serviceProxyId, userId);
1007 if (actionResponse.isRight()) {
1008 response = buildErrorResponse(actionResponse.right().value());
1010 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT), null);
1013 } catch (Exception e) {
1014 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete service proxy");
1015 log.debug("Delete service proxy failed with exception", e);
1016 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
1021 @Path("/{containerComponentType}/{containerComponentId}/serviceProxy/{serviceProxyId}/changeVersion/{newServiceId}")
1022 @Consumes(MediaType.APPLICATION_JSON)
1023 @Produces(MediaType.APPLICATION_JSON)
1024 @ApiOperation(value = "Update service proxy with new version", httpMethod = "POST", notes = "Returns updated service proxy", response = Response.class)
1025 @ApiResponses(value = { @ApiResponse(code = 201, message = "Service proxy created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
1026 public Response changeServiceProxyVersion(@PathParam("containerComponentId") final String containerComponentId, @PathParam("serviceProxyId") final String serviceProxyId,
1027 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
1028 @Context final HttpServletRequest request) {
1029 ServletContext context = request.getSession().getServletContext();
1031 String url = request.getMethod() + " " + request.getRequestURI();
1032 log.debug("Start handle request of {}", url);
1035 String userId = request.getHeader(Constants.USER_ID_HEADER);
1037 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
1038 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
1039 if (componentInstanceLogic == null) {
1040 log.debug("Unsupported component type {}", containerComponentType);
1041 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
1043 Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.changeServiceProxyVersion(containerComponentType, containerComponentId, serviceProxyId, userId);
1045 if (actionResponse.isRight()) {
1046 return buildErrorResponse(actionResponse.right().value());
1048 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
1050 } catch (Exception e) {
1051 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update service proxy with new version");
1052 log.debug("Update service proxy with new version failed with exception", e);
1053 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
1057 * REST API GET relation by Id
1058 * Allows to get relation contained in specified component according to received Id
1059 * @param containerComponentType
1060 * @param componentId
1067 @Path("/{containerComponentType}/{componentId}/relationId/{relationId}")
1068 @Consumes(MediaType.APPLICATION_JSON)
1069 @Produces(MediaType.APPLICATION_JSON)
1070 @ApiOperation(value = "Get relation", httpMethod = "GET", notes = "Returns relation metadata according to relationId", response = Resource.class)
1071 @ApiResponses(value = { @ApiResponse(code = 200, message = "relation found"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Relation not found") })
1072 public Response getRelationById(@PathParam("containerComponentType") final String containerComponentType, @PathParam("componentId") final String componentId,
1073 @PathParam("relationId") final String relationId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
1075 ServletContext context = request.getSession().getServletContext();
1076 String url = request.getMethod() + " " + request.getRequestURI();
1077 log.debug("(GET) Start handle request of {}", url);
1079 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
1080 if (componentTypeEnum == null) {
1081 log.debug("Unsupported component type {}", containerComponentType);
1082 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
1084 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
1086 Either<RequirementCapabilityRelDef, ResponseFormat> actionResponse = componentInstanceLogic.getRelationById(componentId, relationId, userId, componentTypeEnum);
1087 if (actionResponse.isRight()) {
1088 return buildErrorResponse(actionResponse.right().value());
1090 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
1091 } catch (Exception e) {
1092 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("getRelationById");
1093 log.debug("getRelationById unexpected exception", e);
1094 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
1098 private Either<ComponentInstance, ResponseFormat> convertToResourceInstance(String data) {
1100 // Either<ComponentInstance, ActionStatus> convertStatus =
1101 // convertJsonToObject(data, ComponentInstance.class);
1102 Either<ComponentInstance, ResponseFormat> convertStatus = getComponentsUtils().convertJsonToObjectUsingObjectMapper(data, new User(), ComponentInstance.class, null, ComponentTypeEnum.RESOURCE_INSTANCE);
1103 if (convertStatus.isRight()) {
1104 return Either.right(convertStatus.right().value());
1106 ComponentInstance resourceInstanceInfo = convertStatus.left().value();
1108 return Either.left(resourceInstanceInfo);
1111 private Either<List<ComponentInstance>, ResponseFormat> convertToMultipleResourceInstance(String dataList) {
1113 Either<ComponentInstance[], ResponseFormat> convertStatus = getComponentsUtils().convertJsonToObjectUsingObjectMapper(dataList, new User(), ComponentInstance[].class, null, ComponentTypeEnum.RESOURCE_INSTANCE);
1114 if (convertStatus.isRight()) {
1115 return Either.right(convertStatus.right().value());
1118 return Either.left(Arrays.asList(convertStatus.left().value()));
1121 private Either<List<ComponentInstanceProperty>, ResponseFormat> convertMultipleProperties(String dataList) {
1122 if (StringUtils.isEmpty(dataList)) {
1123 return Either.right(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
1125 Either<ComponentInstanceProperty[], ResponseFormat> convertStatus = getComponentsUtils().convertJsonToObjectUsingObjectMapper(dataList, new User(), ComponentInstanceProperty[].class, null, ComponentTypeEnum.RESOURCE_INSTANCE);
1126 if (convertStatus.isRight()) {
1127 return Either.right(convertStatus.right().value());
1129 return Either.left(Arrays.asList(convertStatus.left().value()));
1132 private Either<List<ComponentInstanceInput>, ResponseFormat> convertMultipleInputs(String dataList) {
1133 if (StringUtils.isEmpty(dataList)) {
1134 return Either.right(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
1136 Either<ComponentInstanceInput[], ResponseFormat> convertStatus = getComponentsUtils().convertJsonToObjectUsingObjectMapper(dataList, new User(), ComponentInstanceInput[].class, null, ComponentTypeEnum.RESOURCE_INSTANCE);
1137 if (convertStatus.isRight()) {
1138 return Either.right(convertStatus.right().value());
1140 return Either.left(Arrays.asList(convertStatus.left().value()));
1144 private Either<RequirementCapabilityRelDef, ResponseFormat> convertToRequirementCapabilityRelDef(String data) {
1146 Either<RequirementCapabilityRelDef, ActionStatus> convertStatus = convertJsonToObject(data, RequirementCapabilityRelDef.class);
1147 if (convertStatus.isRight()) {
1148 return Either.right(getComponentsUtils().getResponseFormat(convertStatus.right().value()));
1150 RequirementCapabilityRelDef requirementCapabilityRelDef = convertStatus.left().value();
1151 return Either.left(requirementCapabilityRelDef);
1154 private <T> Either<T, ActionStatus> convertJsonToObject(String data, Class<T> clazz) {
1156 log.trace("convert json to object. json=\n {}", data);
1158 t = gson.fromJson(data, clazz);
1160 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("convertJsonToObject");
1161 log.debug("object is null after converting from json");
1162 return Either.right(ActionStatus.INVALID_CONTENT);
1164 return Either.left(t);
1165 } catch (Exception e) {
1167 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("convertJsonToObject");
1168 log.debug("failed to convert from json", e);
1169 return Either.right(ActionStatus.INVALID_CONTENT);
1175 @Path("/{containerComponentType}/{componentId}/paths-to-delete")
1176 @Produces(MediaType.APPLICATION_JSON)
1177 @ApiOperation(value = "Check if forwarding path to delete on version change", httpMethod = "GET", notes = "Returns forwarding paths to delete",
1178 response = Response.class)
1179 public Response changeResourceInstanceVersion( @PathParam("componentId") String componentId,
1180 @QueryParam("componentInstanceId") final String oldComponentInstanceId,
1181 @QueryParam("newComponentInstanceId") final String newComponentInstanceId,
1182 @ApiParam(value = "valid values: resources / services",
1183 allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME)
1184 @PathParam("containerComponentType") final String containerComponentType,
1185 @Context final HttpServletRequest request) {
1186 if (oldComponentInstanceId == null){
1187 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.MISSING_OLD_COMPONENT_INSTANCE));
1189 if (newComponentInstanceId == null){
1190 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.MISSING_NEW_COMPONENT_INSTANCE));
1192 ServletContext context = request.getSession().getServletContext();
1194 String url = request.getMethod() + " " + request.getRequestURI();
1195 log.debug("Start handle request of {}", url);
1196 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
1197 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
1198 if (componentInstanceLogic == null) {
1199 log.debug("Unsupported component type {}", containerComponentType);
1200 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
1202 ComponentInstance newComponentInstance;
1203 if(StringUtils.isNotEmpty(newComponentInstanceId)){
1204 newComponentInstance=new ComponentInstance();
1205 newComponentInstance.setToscaPresentationValue(JsonPresentationFields.CI_COMPONENT_UID,newComponentInstanceId);
1207 log.error("missing component id");
1208 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.MISSING_DATA));
1210 Either<Set<String>,ResponseFormat> actionResponse= componentInstanceLogic.forwardingPathOnVersionChange(
1211 containerComponentType,componentId,oldComponentInstanceId,newComponentInstance);
1212 if (actionResponse.isRight()) {
1213 return buildErrorResponse(actionResponse.right().value());
1215 ForwardingPaths forwardingPaths=new ForwardingPaths();
1216 forwardingPaths.setForwardingPathToDelete(actionResponse.left().value());
1217 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), forwardingPaths);