2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.servlets;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import com.google.gson.Gson;
25 import com.google.gson.GsonBuilder;
26 import com.google.gson.reflect.TypeToken;
27 import com.jcabi.aspects.Loggable;
28 import fj.data.Either;
29 import io.swagger.annotations.Api;
30 import io.swagger.annotations.ApiOperation;
31 import io.swagger.annotations.ApiParam;
32 import io.swagger.annotations.ApiResponse;
33 import io.swagger.annotations.ApiResponses;
34 import org.apache.commons.io.IOUtils;
35 import org.apache.commons.lang.StringUtils;
36 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
37 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
38 import org.openecomp.sdc.be.config.BeEcompErrorManager;
39 import org.openecomp.sdc.be.dao.api.ActionStatus;
40 import org.openecomp.sdc.be.datamodel.ForwardingPaths;
41 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
42 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
43 import org.openecomp.sdc.be.info.CreateAndAssotiateInfo;
44 import org.openecomp.sdc.be.info.GroupDefinitionInfo;
45 import org.openecomp.sdc.be.model.ComponentInstance;
46 import org.openecomp.sdc.be.model.ComponentInstanceInput;
47 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
48 import org.openecomp.sdc.be.model.PropertyConstraint;
49 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
50 import org.openecomp.sdc.be.model.Resource;
51 import org.openecomp.sdc.be.model.User;
52 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation.PropertyConstraintDeserialiser;
53 import org.openecomp.sdc.common.api.Constants;
54 import org.openecomp.sdc.common.datastructure.Wrapper;
55 import org.openecomp.sdc.exception.ResponseFormat;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
59 import javax.inject.Singleton;
60 import javax.servlet.ServletContext;
61 import javax.servlet.http.HttpServletRequest;
62 import javax.ws.rs.Consumes;
63 import javax.ws.rs.DELETE;
64 import javax.ws.rs.GET;
65 import javax.ws.rs.HeaderParam;
66 import javax.ws.rs.POST;
67 import javax.ws.rs.PUT;
68 import javax.ws.rs.Path;
69 import javax.ws.rs.PathParam;
70 import javax.ws.rs.Produces;
71 import javax.ws.rs.QueryParam;
72 import javax.ws.rs.core.Context;
73 import javax.ws.rs.core.MediaType;
74 import javax.ws.rs.core.Response;
75 import java.io.InputStream;
76 import java.lang.reflect.Type;
77 import java.util.ArrayList;
78 import java.util.Arrays;
79 import java.util.List;
83 * Root resource (exposed at "/" path) .json
85 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
87 @Api(value = "Resource Instance Servlet")
89 public class ComponentInstanceServlet extends AbstractValidationsServlet {
91 private static final Logger log = LoggerFactory.getLogger(ComponentInstanceServlet.class);
93 Type constraintType = new TypeToken<PropertyConstraint>() {
96 Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyConstraintDeserialiser()).create();
99 @Path("/{containerComponentType}/{componentId}/resourceInstance")
100 @Consumes(MediaType.APPLICATION_JSON)
101 @Produces(MediaType.APPLICATION_JSON)
102 @ApiOperation(value = "Create ComponentInstance", httpMethod = "POST", notes = "Returns created ComponentInstance", response = Response.class)
103 @ApiResponses(value = { @ApiResponse(code = 201, message = "Component created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
104 @ApiResponse(code = 409, message = "Component instance already exist") })
105 public Response createComponentInstance(@ApiParam(value = "RI object to be created", required = true) String data, @PathParam("componentId") final String containerComponentId,
106 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
107 @HeaderParam(value = Constants.USER_ID_HEADER) @ApiParam(value = "USER_ID of modifier user", required = true) String userId, @Context final HttpServletRequest request) {
108 ServletContext context = request.getSession().getServletContext();
112 ComponentInstance componentInstance = RepresentationUtils.fromRepresentation(data, ComponentInstance.class);
113 componentInstance.setInvariantName(null);
114 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
115 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
116 if (componentInstanceLogic == null) {
117 log.debug("Unsupported component type {}", containerComponentType);
118 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
120 Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.createComponentInstance(containerComponentType, containerComponentId, userId, componentInstance);
122 if (actionResponse.isRight()) {
123 return buildErrorResponse(actionResponse.right().value());
125 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), actionResponse.left().value());
127 } catch (Exception e) {
128 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create Component Instance");
129 log.debug("create component instance failed with exception", e);
130 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
135 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}")
136 @Consumes(MediaType.APPLICATION_JSON)
137 @Produces(MediaType.APPLICATION_JSON)
138 @ApiOperation(value = "Update resource instance", httpMethod = "POST", notes = "Returns updated resource instance", response = Response.class)
139 @ApiResponses(value = { @ApiResponse(code = 200, message = "Resource instance updated"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
140 public Response updateComponentInstanceMetadata(@PathParam("componentId") final String componentId, @PathParam("componentInstanceId") final String componentInstanceId,
141 @ApiParam(value = "valid values: resources / services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
142 + ComponentTypeEnum.PRODUCT_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
143 @Context final HttpServletRequest request) {
144 ServletContext context = request.getSession().getServletContext();
146 String url = request.getMethod() + " " + request.getRequestURI();
147 log.debug("Start handle request of {}", url);
150 log.debug("Start handle request of {}", url);
152 InputStream inputStream = request.getInputStream();
154 byte[] bytes = IOUtils.toByteArray(inputStream);
156 if (bytes == null || bytes.length == 0) {
157 log.info("Empty body was sent.");
158 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
161 String userId = request.getHeader(Constants.USER_ID_HEADER);
163 String data = new String(bytes);
164 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
165 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
166 if (componentInstanceLogic == null) {
167 log.debug("Unsupported component type {}", containerComponentType);
168 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
170 Either<ComponentInstance, ResponseFormat> convertResponse = convertToResourceInstance(data);
172 if (convertResponse.isRight()) {
173 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - updateResourceInstance");
174 log.debug("Failed to convert received data to BE format.");
175 return buildErrorResponse(convertResponse.right().value());
178 ComponentInstance resourceInstance = convertResponse.left().value();
179 Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.updateComponentInstanceMetadata(containerComponentType, componentId, componentInstanceId, userId, resourceInstance);
181 if (actionResponse.isRight()) {
182 return buildErrorResponse(actionResponse.right().value());
184 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
186 } catch (Exception e) {
187 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Resource Instance");
188 log.debug("update resource instance with exception", e);
189 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
195 @Path("/{containerComponentType}/{componentId}/resourceInstance/multipleComponentInstance")
196 @Consumes(MediaType.APPLICATION_JSON)
197 @Produces(MediaType.APPLICATION_JSON)
198 @ApiOperation(value = "Update resource instance multiple component", httpMethod = "POST", notes = "Returns updated resource instance", response = Response.class)
199 @ApiResponses(value = { @ApiResponse(code = 200, message = "Resource instance updated"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
200 public Response updateMultipleComponentInstance(@PathParam("componentId") final String componentId,
201 @ApiParam(value = "valid values: resources / services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
202 + ComponentTypeEnum.PRODUCT_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
203 @Context final HttpServletRequest request, @ApiParam(value = "Component Instance JSON Array", required = true) final String componentInstanceJsonArray) {
205 ServletContext context = request.getSession().getServletContext();
206 String url = request.getMethod() + " " + request.getRequestURI();
207 log.debug("Start handle request of {}", url);
210 log.debug("Start handle request of {}", url);
212 if (componentInstanceJsonArray == null || componentInstanceJsonArray.length() == 0) {
213 log.info("Empty JSON list was sent.");
214 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
217 String userId = request.getHeader(Constants.USER_ID_HEADER);
219 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
220 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
221 if (componentInstanceLogic == null) {
222 log.debug("Unsupported component type {}", containerComponentType);
223 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
226 Either<List<ComponentInstance>, ResponseFormat> convertResponse = convertToMultipleResourceInstance(componentInstanceJsonArray);
228 if (convertResponse.isRight()) {
229 // Using both ECOMP error methods, show to Sofer
230 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - updateResourceInstance");
231 log.debug("Failed to convert received data to BE format.");
232 return buildErrorResponse(convertResponse.right().value());
235 List<ComponentInstance> componentInstanceList = convertResponse.left().value();
237 Either<List<ComponentInstance>, ResponseFormat> actionResponse = componentInstanceLogic.updateComponentInstance(containerComponentType, componentId, userId, componentInstanceList, true, true);
239 if (actionResponse.isRight()) {
240 return buildErrorResponse(actionResponse.right().value());
243 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
245 } catch (Exception e) {
246 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Resource Instance");
247 log.debug("update resource instance with exception", e);
248 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
254 @Path("/{containerComponentType}/{componentId}/resourceInstance/{resourceInstanceId}")
255 @Consumes(MediaType.APPLICATION_JSON)
256 @Produces(MediaType.APPLICATION_JSON)
257 @ApiOperation(value = "Delete ResourceInstance", httpMethod = "DELETE", notes = "Returns delete resourceInstance", response = Response.class)
258 @ApiResponses(value = { @ApiResponse(code = 201, message = "ResourceInstance deleted"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
259 public Response deleteResourceInstance(@PathParam("componentId") final String componentId, @PathParam("resourceInstanceId") final String resourceInstanceId,
260 @ApiParam(value = "valid values: resources / services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
261 + ComponentTypeEnum.PRODUCT_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
262 @Context final HttpServletRequest request) {
263 ServletContext context = request.getSession().getServletContext();
264 String url = request.getMethod() + " " + request.getRequestURI();
265 Response response = null;
267 log.debug("Start handle request of {}", url);
268 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
269 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
270 if (componentInstanceLogic == null) {
271 log.debug("Unsupported component type {}", containerComponentType);
272 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
274 String userId = request.getHeader(Constants.USER_ID_HEADER);
275 Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.deleteComponentInstance(containerComponentType, componentId, resourceInstanceId, userId);
277 if (actionResponse.isRight()) {
278 response = buildErrorResponse(actionResponse.right().value());
280 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT), null);
283 } catch (Exception e) {
284 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Resource Instance");
285 log.debug("delete resource instance with exception", e);
286 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
290 @ApiParam(value = "allowed values are resources /services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + "," + ComponentTypeEnum.PRODUCT_PARAM_NAME, required = true)
292 @Path("/{containerComponentType}/{componentId}/resourceInstance/associate")
293 @Consumes(MediaType.APPLICATION_JSON)
294 @Produces(MediaType.APPLICATION_JSON)
295 @ApiOperation(value = "Associate RI to RI", httpMethod = "POST", notes = "Returns created RelationshipInfo", response = Response.class)
296 @ApiResponses(value = { @ApiResponse(code = 201, message = "Relationship created"), @ApiResponse(code = 403, message = "Missing information"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
297 @ApiResponse(code = 409, message = "Relationship already exist") })
298 public Response associateRIToRI(@ApiParam(value = "unique id of the container component") @PathParam("componentId") final String componentId,
299 @ApiParam(value = "allowed values are resources /services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
300 + ComponentTypeEnum.PRODUCT_PARAM_NAME, required = true) @PathParam("containerComponentType") final String containerComponentType,
301 @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @ApiParam(value = "RelationshipInfo", required = true) String data, @Context final HttpServletRequest request) {
302 ServletContext context = request.getSession().getServletContext();
304 String url = request.getMethod() + " " + request.getRequestURI();
305 log.debug("Start handle request of {}", url);
306 Response response = null;
310 log.debug("Start handle request of {}", url);
312 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
313 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
314 if (componentInstanceLogic == null) {
315 log.debug("Unsupported component type {}", containerComponentType);
316 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
319 Either<RequirementCapabilityRelDef, ResponseFormat> regInfoW = convertToRequirementCapabilityRelDef(data);
321 Either<RequirementCapabilityRelDef, ResponseFormat> resultOp;
322 if (regInfoW.isRight()) {
323 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - associateRIToRI");
324 log.debug("Failed to convert received data to BE format.");
325 resultOp = Either.right(regInfoW.right().value());
327 RequirementCapabilityRelDef requirementDef = regInfoW.left().value();
328 resultOp = componentInstanceLogic.associateRIToRI(componentId, userId, requirementDef, componentTypeEnum);
331 Either<RequirementCapabilityRelDef, ResponseFormat> actionResponse = resultOp;
333 if (actionResponse.isRight()) {
334 response = buildErrorResponse(actionResponse.right().value());
336 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
340 } catch (Exception e) {
341 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Associate Resource Instance");
342 log.debug("associate resource instance to another RI with exception", e);
343 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
348 @Path("/{containerComponentType}/{componentId}/resourceInstance/dissociate")
349 @Consumes(MediaType.APPLICATION_JSON)
350 @Produces(MediaType.APPLICATION_JSON)
351 @ApiOperation(value = "Dissociate RI from RI", httpMethod = "PUT", notes = "Returns deleted RelationshipInfo", response = Response.class)
352 @ApiResponses(value = { @ApiResponse(code = 201, message = "Relationship deleted"), @ApiResponse(code = 403, message = "Missing information"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
353 public Response dissociateRIFromRI(
354 @ApiParam(value = "allowed values are resources /services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
355 + ComponentTypeEnum.PRODUCT_PARAM_NAME, required = true) @PathParam("containerComponentType") final String containerComponentType,
356 @ApiParam(value = "unique id of the container component") @PathParam("componentId") final String componentId, @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
357 @ApiParam(value = "RelationshipInfo", required = true) String data, @Context final HttpServletRequest request) {
358 ServletContext context = request.getSession().getServletContext();
360 String url = request.getMethod() + " " + request.getRequestURI();
361 log.debug("Start handle request of {}", url);
365 log.debug("Start handle request of {}", url);
367 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
368 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
369 if (componentInstanceLogic == null) {
370 log.debug("Unsupported component type {}", containerComponentType);
371 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
374 Either<RequirementCapabilityRelDef, ResponseFormat> regInfoW = convertToRequirementCapabilityRelDef(data);
375 if (regInfoW.isRight()) {
376 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - dissociateRIFromRI");
377 log.debug("Failed to convert received data to BE format.");
378 return buildErrorResponse(regInfoW.right().value());
381 RequirementCapabilityRelDef requirementDef = regInfoW.left().value();
382 Either<RequirementCapabilityRelDef, ResponseFormat> actionResponse = componentInstanceLogic.dissociateRIFromRI(componentId, userId, requirementDef, componentTypeEnum);
384 if (actionResponse.isRight()) {
385 return buildErrorResponse(actionResponse.right().value());
387 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
389 } catch (Exception e) {
390 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Dissociate Resource Instance");
391 log.debug("dissociate resource instance from service failed with exception", e);
392 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
397 @Path("/{containerComponentType}/{componentId}/resourceInstance/createAndAssociate")
398 @Consumes(MediaType.APPLICATION_JSON)
399 @Produces(MediaType.APPLICATION_JSON)
400 @ApiOperation(value = "Create RI and associate RI to RI", httpMethod = "POST", notes = "Returns created RI and RelationshipInfo", response = Response.class)
401 @ApiResponses(value = { @ApiResponse(code = 201, message = "RI created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
402 @ApiResponse(code = 409, message = "Relationship already exist") })
403 public Response createAndAssociateRIToRI(@PathParam("componentId") final String componentId,
404 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
405 @Context final HttpServletRequest request) {
406 ServletContext context = request.getSession().getServletContext();
408 String url = request.getMethod() + " " + request.getRequestURI();
409 log.debug("Start handle request of {}", url);
412 log.debug("Start handle request of {}", url);
414 InputStream inputStream = request.getInputStream();
416 byte[] bytes = IOUtils.toByteArray(inputStream);
418 if (bytes == null || bytes.length == 0) {
419 log.info("Empty body was sent.");
420 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
423 String userId = request.getHeader(Constants.USER_ID_HEADER);
425 String data = new String(bytes);
427 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
428 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
429 if (componentInstanceLogic == null) {
430 log.debug("Unsupported component type {}", containerComponentType);
431 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
434 Either<CreateAndAssotiateInfo, ActionStatus> convertStatus = convertJsonToObject(data, CreateAndAssotiateInfo.class);
435 if (convertStatus.isRight()) {
436 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - createAndAssociateRIToRI");
437 log.debug("Failed to convert received data to BE format.");
438 Either<Object, ResponseFormat> formattedResponse = Either.right(getComponentsUtils().getResponseFormat(convertStatus.right().value()));
439 return buildErrorResponse(formattedResponse.right().value());
442 CreateAndAssotiateInfo createAndAssotiateInfo = convertStatus.left().value();
443 Either<CreateAndAssotiateInfo, ResponseFormat> actionResponse = componentInstanceLogic.createAndAssociateRIToRI(containerComponentType, componentId, userId, createAndAssotiateInfo);
445 if (actionResponse.isRight()) {
446 return buildErrorResponse(actionResponse.right().value());
448 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), actionResponse.left().value());
449 } catch (Exception e) {
450 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create and Associate Resource Instance");
451 log.debug("create and associate RI failed with exception", e);
452 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
457 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/properties")
458 @Consumes(MediaType.APPLICATION_JSON)
459 @Produces(MediaType.APPLICATION_JSON)
460 @ApiOperation(value = "Update resource instance property", httpMethod = "POST", notes = "Returns updated resource instance property", response = Response.class)
461 @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
462 public Response updateResourceInstanceProperties(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
463 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
464 @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,
465 @Context final HttpServletRequest request, @ApiParam(value = "Component Instance Properties JSON Array", required = true) final String componentInstancePropertiesJsonArray) {
467 String url = request.getMethod() + " " + request.getRequestURI();
468 log.debug("Start handle request of {}", url);
471 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
472 List<ComponentInstanceProperty> propertiesToUpdate = new ArrayList<>();
473 if (errorWrapper.isEmpty()) {
474 Either<List<ComponentInstanceProperty>, ResponseFormat> propertiesToUpdateEither = convertMultipleProperties(componentInstancePropertiesJsonArray);
475 if (propertiesToUpdateEither.isRight()) {
476 errorWrapper.setInnerElement(propertiesToUpdateEither.right().value());
478 propertiesToUpdate = propertiesToUpdateEither.left().value();
482 if (!errorWrapper.isEmpty()) {
483 return buildErrorResponse(errorWrapper.getInnerElement());
486 log.debug("Start handle request of updateResourceInstanceProperty. Received property is {}", propertiesToUpdate);
488 ServletContext context = request.getSession().getServletContext();
490 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
491 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
492 if (componentInstanceLogic == null) {
493 log.debug("Unsupported component type {}", containerComponentType);
494 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
497 Either<List<ComponentInstanceProperty>, ResponseFormat> actionResponse = componentInstanceLogic.createOrUpdatePropertiesValues(componentTypeEnum, componentId, componentInstanceId, propertiesToUpdate, userId);
499 if (actionResponse.isRight()) {
500 return buildErrorResponse(actionResponse.right().value());
503 List<ComponentInstanceProperty> resourceInstanceProperties = actionResponse.left().value();
504 ObjectMapper mapper = new ObjectMapper();
505 String result = mapper.writeValueAsString(resourceInstanceProperties);
506 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
508 } catch (Exception e) {
509 log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
510 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
516 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/inputs")
517 @Consumes(MediaType.APPLICATION_JSON)
518 @Produces(MediaType.APPLICATION_JSON)
519 @ApiOperation(value = "Update resource instance property", httpMethod = "POST", notes = "Returns updated resource instance property", response = Response.class)
520 @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
521 public Response updateResourceInstanceInput(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
522 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
523 @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,
524 @Context final HttpServletRequest request, @ApiParam(value = "Component Instance Properties JSON Array", required = true) final String componentInstanceInputsJsonArray) {
526 String url = request.getMethod() + " " + request.getRequestURI();
527 log.debug("Start handle request of {}", url);
530 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
531 List<ComponentInstanceInput> inputsToUpdate = new ArrayList<>();
532 if (errorWrapper.isEmpty()) {
533 Either<List<ComponentInstanceInput>, ResponseFormat> inputsToUpdateEither = convertMultipleInputs(componentInstanceInputsJsonArray);
534 if (inputsToUpdateEither.isRight()) {
535 errorWrapper.setInnerElement(inputsToUpdateEither.right().value());
537 inputsToUpdate = inputsToUpdateEither.left().value();
540 if (!errorWrapper.isEmpty()) {
541 return buildErrorResponse(errorWrapper.getInnerElement());
544 log.debug("Start handle request of updateResourceInstanceProperty. Received property is {}", inputsToUpdate);
546 ServletContext context = request.getSession().getServletContext();
548 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
549 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
550 if (componentInstanceLogic == null) {
551 log.debug("Unsupported component type {}", containerComponentType);
552 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
555 Either<List<ComponentInstanceInput>, ResponseFormat> actionResponse =
556 componentInstanceLogic.createOrUpdateInstanceInputValues(componentTypeEnum, componentId, componentInstanceId, inputsToUpdate, userId);
558 if (actionResponse.isRight()) {
559 return buildErrorResponse(actionResponse.right().value());
562 List<ComponentInstanceInput> resourceInstanceInput = actionResponse.left().value();
563 ObjectMapper mapper = new ObjectMapper();
564 String result = mapper.writeValueAsString(resourceInstanceInput);
565 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
567 } catch (Exception e) {
568 log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
569 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
575 * Updates ResourceInstance Attribute
578 * @param containerComponentType
579 * @param componentInstanceId
585 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/attribute")
586 @Consumes(MediaType.APPLICATION_JSON)
587 @Produces(MediaType.APPLICATION_JSON)
588 @ApiOperation(value = "Update resource instance attribute", httpMethod = "POST", notes = "Returns updated resource instance attribute", response = Response.class)
589 @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
590 public Response updateResourceInstanceAttribute(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
591 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
592 @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,
593 @Context final HttpServletRequest request) {
595 String url = request.getMethod() + " " + request.getRequestURI();
596 log.debug("Start handle request of {}", url);
600 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
601 Wrapper<String> dataWrapper = new Wrapper<>();
602 Wrapper<ComponentInstanceProperty> attributeWrapper = new Wrapper<>();
603 Wrapper<ComponentInstanceBusinessLogic> blWrapper = new Wrapper<>();
605 validateInputStream(request, dataWrapper, errorWrapper);
607 if (errorWrapper.isEmpty()) {
608 validateClassParse(dataWrapper.getInnerElement(), attributeWrapper, () -> ComponentInstanceProperty.class, errorWrapper);
611 if (errorWrapper.isEmpty()) {
612 validateComponentInstanceBusinessLogic(request, containerComponentType, blWrapper, errorWrapper);
615 if (errorWrapper.isEmpty()) {
616 ComponentInstanceBusinessLogic componentInstanceLogic = blWrapper.getInnerElement();
617 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
618 log.debug("Start handle request of ComponentInstanceAttribute. Received attribute is {}", attributeWrapper.getInnerElement());
619 Either<ComponentInstanceProperty, ResponseFormat> eitherAttribute = componentInstanceLogic.createOrUpdateAttributeValue(componentTypeEnum, componentId, componentInstanceId, attributeWrapper.getInnerElement(), userId);
620 if (eitherAttribute.isRight()) {
621 errorWrapper.setInnerElement(eitherAttribute.right().value());
623 attributeWrapper.setInnerElement(eitherAttribute.left().value());
627 return buildResponseFromElement(errorWrapper, attributeWrapper);
629 } catch (Exception e) {
630 log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
631 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
637 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/property/{propertyId}")
638 @Consumes(MediaType.APPLICATION_JSON)
639 @Produces(MediaType.APPLICATION_JSON)
640 @ApiOperation(value = "Update resource instance", httpMethod = "DELETE", notes = "Returns deleted resource instance property", response = Response.class)
641 @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
642 public Response deleteResourceInstanceProperty(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
643 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
644 @ApiParam(value = "resource instance id") @PathParam("componentInstanceId") final String componentInstanceId, @ApiParam(value = "property id") @PathParam("propertyId") final String propertyId,
645 @ApiParam(value = "id of user initiating the operation") @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Context final HttpServletRequest request) {
647 ServletContext context = request.getSession().getServletContext();
649 String url = request.getMethod() + " " + request.getRequestURI();
650 log.debug("Start handle request of {}", url);
653 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
654 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
655 if (componentInstanceLogic == null) {
656 log.debug("Unsupported component type {}", containerComponentType);
657 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
660 Either<ComponentInstanceProperty, ResponseFormat> actionResponse = componentInstanceLogic.deletePropertyValue(componentTypeEnum, componentId, componentInstanceId, propertyId, userId);
661 if (actionResponse.isRight()) {
662 return buildErrorResponse(actionResponse.right().value());
664 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT), null);
665 } catch (Exception e) {
666 log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
667 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
673 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/changeVersion")
674 @Consumes(MediaType.APPLICATION_JSON)
675 @Produces(MediaType.APPLICATION_JSON)
676 @ApiOperation(value = "Update resource instance", httpMethod = "POST", notes = "Returns updated resource instance", response = Response.class)
677 @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
678 public Response changeResourceInstanceVersion(@PathParam("componentId") final String componentId, @PathParam("componentInstanceId") final String componentInstanceId,
679 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
680 @Context final HttpServletRequest request) {
681 ServletContext context = request.getSession().getServletContext();
683 String url = request.getMethod() + " " + request.getRequestURI();
684 log.debug("Start handle request of {}", url);
685 try ( InputStream inputStream = request.getInputStream()) {
687 byte[] bytes = IOUtils.toByteArray(inputStream);
689 if (bytes == null || bytes.length == 0) {
690 log.info("Empty body was sent.");
691 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
694 String userId = request.getHeader(Constants.USER_ID_HEADER);
696 String data = new String(bytes);
698 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
699 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
700 if (componentInstanceLogic == null) {
701 log.debug("Unsupported component type {}", containerComponentType);
702 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
705 Either<ComponentInstance, ResponseFormat> convertResponse = convertToResourceInstance(data);
707 if (convertResponse.isRight()) {
708 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - updateResourceInstance");
709 log.debug("Failed to convert received data to BE format.");
710 return buildErrorResponse(convertResponse.right().value());
713 ComponentInstance newResourceInstance = convertResponse.left().value();
714 Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.changeComponentInstanceVersion(containerComponentType, componentId, componentInstanceId, userId, newResourceInstance);
716 if (actionResponse.isRight()) {
717 return buildErrorResponse(actionResponse.right().value());
719 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
721 } catch (Exception e) {
722 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Resource Instance");
723 log.debug("update resource instance with exception", e);
724 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
730 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/groupInstance/{groupInstanceId}/property")
731 @Consumes(MediaType.APPLICATION_JSON)
732 @Produces(MediaType.APPLICATION_JSON)
733 @ApiOperation(value = "Update resource instance property", httpMethod = "POST", notes = "Returns updated resource instance property", response = Response.class)
734 @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
735 public Response updateGroupInstanceProperty(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
736 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
737 @ApiParam(value = "resource instance id") @PathParam("componentInstanceId") final String componentInstanceId, @ApiParam(value = "group instance id") @PathParam("groupInstanceId") final String groupInstanceId,
738 @ApiParam(value = "id of user initiating the operation") @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Context final HttpServletRequest request) {
740 String url = request.getMethod() + " " + request.getRequestURI();
741 log.debug("Start handle request of {}", url);
744 Wrapper<String> dataWrapper = new Wrapper<>();
745 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
746 Wrapper<ComponentInstanceProperty> propertyWrapper = new Wrapper<>();
748 validateInputStream(request, dataWrapper, errorWrapper);
750 if (errorWrapper.isEmpty()) {
751 validateClassParse(dataWrapper.getInnerElement(), propertyWrapper, () -> ComponentInstanceProperty.class, errorWrapper);
754 if (!errorWrapper.isEmpty()) {
755 return buildErrorResponse(errorWrapper.getInnerElement());
758 ComponentInstanceProperty property = propertyWrapper.getInnerElement();
760 log.debug("Start handle request of updateResourceInstanceProperty. Received property is {}", property);
762 ServletContext context = request.getSession().getServletContext();
764 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
765 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
766 if (componentInstanceLogic == null) {
767 log.debug("Unsupported component type {}", containerComponentType);
768 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
771 Either<ComponentInstanceProperty, ResponseFormat> actionResponse = componentInstanceLogic.createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, componentInstanceId, groupInstanceId, property, userId);
773 if (actionResponse.isRight()) {
774 return buildErrorResponse(actionResponse.right().value());
777 ComponentInstanceProperty resourceInstanceProperty = actionResponse.left().value();
778 ObjectMapper mapper = new ObjectMapper();
779 String result = mapper.writeValueAsString(resourceInstanceProperty);
780 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
782 } catch (Exception e) {
783 log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
784 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
790 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/groupInstance/{groupInstId}")
791 @Consumes(MediaType.APPLICATION_JSON)
792 @Produces(MediaType.APPLICATION_JSON)
793 @ApiOperation(value = "Get group artifacts ", httpMethod = "GET", notes = "Returns artifacts metadata according to groupInstId", response = Resource.class)
794 @ApiResponses(value = { @ApiResponse(code = 200, message = "group found"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Group not found") })
795 public Response getGroupArtifactById(@PathParam("containerComponentType") final String containerComponentType, @PathParam("componentId") final String componentId, @PathParam("componentInstanceId") final String componentInstanceId,
796 @PathParam("groupInstId") final String groupInstId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
797 ServletContext context = request.getSession().getServletContext();
798 String url = request.getMethod() + " " + request.getRequestURI();
799 log.debug("(GET) Start handle request of {}", url);
803 GroupBusinessLogic businessLogic = this.getGroupBL(context);
804 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
805 Either<GroupDefinitionInfo, ResponseFormat> actionResponse = businessLogic.getGroupInstWithArtifactsById(componentTypeEnum, componentId, componentInstanceId, groupInstId, userId, false);
807 if (actionResponse.isRight()) {
808 log.debug("failed to get all non abstract {}", containerComponentType);
809 return buildErrorResponse(actionResponse.right().value());
812 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
814 } catch (Exception e) {
815 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("getGroupArtifactById");
816 log.debug("getGroupArtifactById unexpected exception", e);
817 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
824 @Path("/{containerComponentType}/{containerComponentId}/componentInstances/{componentInstanceUniqueId}/properties")
825 @Consumes(MediaType.APPLICATION_JSON)
826 @Produces(MediaType.APPLICATION_JSON)
827 @ApiOperation(value = "Get component instance properties", httpMethod = "GET", notes = "Returns component instance properties", response = Response.class)
828 @ApiResponses(value = { @ApiResponse(code = 200, message = "Properties found"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Component/Component Instance - not found") })
829 public Response getInstancePropertiesById(@PathParam("containerComponentType") final String containerComponentType, @PathParam("containerComponentId") final String containerComponentId,
830 @PathParam("componentInstanceUniqueId") final String componentInstanceUniqueId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
832 ServletContext context = request.getSession().getServletContext();
833 String url = request.getMethod() + " " + request.getRequestURI();
834 log.debug("(GET) Start handle request of {}", url);
837 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
838 ComponentInstanceBusinessLogic componentInstanceBL = getComponentInstanceBL(context, componentTypeEnum);
840 Either<List<ComponentInstanceProperty>, ResponseFormat> componentInstancePropertiesById = componentInstanceBL.getComponentInstancePropertiesById(containerComponentType, containerComponentId, componentInstanceUniqueId, userId);
842 if (componentInstancePropertiesById.isRight()) {
843 log.debug("Failed to get properties of component instance ID: {} in {} with ID: {}", componentInstanceUniqueId, containerComponentType, containerComponentId);
844 return buildErrorResponse(componentInstancePropertiesById.right().value());
847 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), componentInstancePropertiesById.left().value());
848 } catch (Exception e) {
849 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("getGroupArtifactById");
850 log.debug("getGroupArtifactById unexpected exception", e);
851 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
858 @Path("/{containerComponentType}/{containerComponentId}/componentInstances/{componentInstanceUniqueId}/capability/{capabilityType}/capabilityName/{capabilityName}/ownerId/{ownerId}/properties")
859 @Consumes(MediaType.APPLICATION_JSON)
860 @Produces(MediaType.APPLICATION_JSON)
861 @ApiOperation(value = "Get component instance capability properties", httpMethod = "GET", notes = "Returns component instance capability properties", response = Response.class)
862 @ApiResponses(value = { @ApiResponse(code = 200, message = "Properties found"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Component/Component Instance/Capability - not found") })
863 public Response getInstanceCapabilityPropertiesById(@PathParam("containerComponentType") final String containerComponentType, @PathParam("containerComponentId") final String containerComponentId,
864 @PathParam("componentInstanceUniqueId") final String componentInstanceUniqueId, @PathParam("capabilityType") final String capabilityType, @PathParam("capabilityName") final String capabilityName, @PathParam("ownerId") final String ownerId, @Context final HttpServletRequest request,
865 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
867 ServletContext context = request.getSession().getServletContext();
868 String url = request.getMethod() + " " + request.getRequestURI();
869 log.debug("(GET) Start handle request of {}", url);
872 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
873 ComponentInstanceBusinessLogic componentInstanceBL = getComponentInstanceBL(context, componentTypeEnum);
875 Either<List<ComponentInstanceProperty>, ResponseFormat> componentInstancePropertiesById = componentInstanceBL.getComponentInstanceCapabilityPropertiesById(containerComponentType, containerComponentId, componentInstanceUniqueId,
876 capabilityType, capabilityName, ownerId, userId);
878 if (componentInstancePropertiesById.isRight()) {
879 log.debug("Failed to get properties of component instance ID: {} in {} with ID: {}", componentInstanceUniqueId, containerComponentType, containerComponentId);
880 return buildErrorResponse(componentInstancePropertiesById.right().value());
883 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), componentInstancePropertiesById.left().value());
884 } catch (Exception e) {
885 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("getGroupArtifactById");
886 log.debug("getGroupArtifactById unexpected exception", e);
887 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
894 @Path("/{containerComponentType}/{containerComponentId}/componentInstances/{componentInstanceUniqueId}/capability/{capabilityType}/capabilityName/{capabilityName}/ownerId/{ownerId}/properties")
895 @Consumes(MediaType.APPLICATION_JSON)
896 @Produces(MediaType.APPLICATION_JSON)
897 @ApiOperation(value = "Update Instance Capabilty Property", httpMethod = "PUT", notes = "Returns updated property", response = Response.class)
898 @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"),
899 @ApiResponse(code = 404, message = "Component/Component Instance/Capability - not found") })
900 public Response updateInstanceCapabilityProperty(@PathParam("containerComponentType") final String containerComponentType, @PathParam("containerComponentId") final String containerComponentId,
901 @PathParam("componentInstanceUniqueId") final String componentInstanceUniqueId, @PathParam("capabilityType") final String capabilityType, @PathParam("capabilityName") final String capabilityName, @PathParam("ownerId") final String ownerId,
902 @ApiParam(value = "Instance capabilty property to update", required = true) String data, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
903 ServletContext context = request.getSession().getServletContext();
904 String url = request.getMethod() + " " + request.getRequestURI();
905 log.debug("(PUT) Start handle request of {}", url);
907 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
908 List<ComponentInstanceProperty> propertiesToUpdate = new ArrayList<>();
909 if (errorWrapper.isEmpty()) {
910 Either<List<ComponentInstanceProperty>, ResponseFormat> propertiesToUpdateEither = convertMultipleProperties(data);
911 if (propertiesToUpdateEither.isRight()) {
912 errorWrapper.setInnerElement(propertiesToUpdateEither.right().value());
914 propertiesToUpdate = propertiesToUpdateEither.left().value();
918 if (!errorWrapper.isEmpty()) {
919 return buildErrorResponse(errorWrapper.getInnerElement());
922 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
923 ComponentInstanceBusinessLogic componentInstanceBL = getComponentInstanceBL(context, componentTypeEnum);
925 Either<List<ComponentInstanceProperty>, ResponseFormat> updateCICapProperty = componentInstanceBL.updateInstanceCapabilityProperties(componentTypeEnum, containerComponentId, componentInstanceUniqueId, capabilityType, capabilityName, propertiesToUpdate, userId);
927 if (updateCICapProperty.isRight()) {
928 log.debug("Failed to get properties of component instance ID: {} in {} with ID: {}", componentInstanceUniqueId, containerComponentType, containerComponentId);
929 return buildErrorResponse(updateCICapProperty.right().value());
932 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), updateCICapProperty.left().value());
933 } catch (Exception e) {
934 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("getGroupArtifactById");
935 log.debug("getGroupArtifactById unexpected exception", e);
936 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
941 @Path("/{containerComponentType}/{containerComponentId}/serviceProxy")
942 @Consumes(MediaType.APPLICATION_JSON)
943 @Produces(MediaType.APPLICATION_JSON)
944 @ApiOperation(value = "Create service proxy", httpMethod = "POST", notes = "Returns created service proxy", response = Response.class)
945 @ApiResponses(value = { @ApiResponse(code = 201, message = "Service proxy created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
946 @ApiResponse(code = 409, message = "Service proxy already exist") })
947 public Response createServiceProxy(@ApiParam(value = "RI object to be created", required = true) String data, @PathParam("containerComponentId") final String containerComponentId,
948 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
949 @HeaderParam(value = Constants.USER_ID_HEADER) @ApiParam(value = "USER_ID of modifier user", required = true) String userId, @Context final HttpServletRequest request) {
950 ServletContext context = request.getSession().getServletContext();
954 ComponentInstance componentInstance = RepresentationUtils.fromRepresentation(data, ComponentInstance.class);
955 componentInstance.setInvariantName(null);
956 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
957 if (componentTypeEnum != ComponentTypeEnum.SERVICE) {
958 log.debug("Unsupported container component type {}", containerComponentType);
959 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
961 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
962 if (componentInstanceLogic == null) {
963 log.debug("Unsupported component type {}", containerComponentType);
964 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
966 Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.createServiceProxy(containerComponentType, containerComponentId, userId, componentInstance);
968 if (actionResponse.isRight()) {
969 return buildErrorResponse(actionResponse.right().value());
971 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), actionResponse.left().value());
973 } catch (Exception e) {
974 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create service proxy");
975 log.debug("Create service proxy failed with exception", e);
976 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
981 @Path("/{containerComponentType}/{containerComponentId}/serviceProxy/{serviceProxyId}")
982 @Consumes(MediaType.APPLICATION_JSON)
983 @Produces(MediaType.APPLICATION_JSON)
984 @ApiOperation(value = "Delete service proxy", httpMethod = "DELETE", notes = "Returns delete service proxy", response = Response.class)
985 @ApiResponses(value = { @ApiResponse(code = 201, message = "Service proxy deleted"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
986 public Response deleteServiceProxy(@PathParam("containerComponentId") final String containerComponentId, @PathParam("serviceProxyId") final String serviceProxyId,
987 @ApiParam(value = "valid values: resources / services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
988 + ComponentTypeEnum.PRODUCT_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
989 @Context final HttpServletRequest request) {
990 ServletContext context = request.getSession().getServletContext();
991 String url = request.getMethod() + " " + request.getRequestURI();
992 Response response = null;
994 log.debug("Start handle request of {}", url);
995 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
996 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
997 if (componentInstanceLogic == null) {
998 log.debug("Unsupported component type {}", containerComponentType);
999 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
1001 String userId = request.getHeader(Constants.USER_ID_HEADER);
1002 Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.deleteServiceProxy(containerComponentType, containerComponentId, serviceProxyId, userId);
1004 if (actionResponse.isRight()) {
1005 response = buildErrorResponse(actionResponse.right().value());
1007 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT), null);
1010 } catch (Exception e) {
1011 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete service proxy");
1012 log.debug("Delete service proxy failed with exception", e);
1013 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
1018 @Path("/{containerComponentType}/{containerComponentId}/serviceProxy/{serviceProxyId}/changeVersion/{newServiceId}")
1019 @Consumes(MediaType.APPLICATION_JSON)
1020 @Produces(MediaType.APPLICATION_JSON)
1021 @ApiOperation(value = "Update service proxy with new version", httpMethod = "POST", notes = "Returns updated service proxy", response = Response.class)
1022 @ApiResponses(value = { @ApiResponse(code = 201, message = "Service proxy created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
1023 public Response changeServiceProxyVersion(@PathParam("containerComponentId") final String containerComponentId, @PathParam("serviceProxyId") final String serviceProxyId,
1024 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
1025 @Context final HttpServletRequest request) {
1026 ServletContext context = request.getSession().getServletContext();
1028 String url = request.getMethod() + " " + request.getRequestURI();
1029 log.debug("Start handle request of {}", url);
1032 String userId = request.getHeader(Constants.USER_ID_HEADER);
1034 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
1035 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
1036 if (componentInstanceLogic == null) {
1037 log.debug("Unsupported component type {}", containerComponentType);
1038 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
1040 Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.changeServiceProxyVersion(containerComponentType, containerComponentId, serviceProxyId, userId);
1042 if (actionResponse.isRight()) {
1043 return buildErrorResponse(actionResponse.right().value());
1045 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
1047 } catch (Exception e) {
1048 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update service proxy with new version");
1049 log.debug("Update service proxy with new version failed with exception", e);
1050 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
1054 * REST API GET relation by Id
1055 * Allows to get relation contained in specified component according to received Id
1056 * @param containerComponentType
1057 * @param componentId
1064 @Path("/{containerComponentType}/{componentId}/relationId/{relationId}")
1065 @Consumes(MediaType.APPLICATION_JSON)
1066 @Produces(MediaType.APPLICATION_JSON)
1067 @ApiOperation(value = "Get relation", httpMethod = "GET", notes = "Returns relation metadata according to relationId", response = Resource.class)
1068 @ApiResponses(value = { @ApiResponse(code = 200, message = "relation found"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Relation not found") })
1069 public Response getRelationById(@PathParam("containerComponentType") final String containerComponentType, @PathParam("componentId") final String componentId,
1070 @PathParam("relationId") final String relationId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
1072 ServletContext context = request.getSession().getServletContext();
1073 String url = request.getMethod() + " " + request.getRequestURI();
1074 log.debug("(GET) Start handle request of {}", url);
1076 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
1077 if (componentTypeEnum == null) {
1078 log.debug("Unsupported component type {}", containerComponentType);
1079 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
1081 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
1083 Either<RequirementCapabilityRelDef, ResponseFormat> actionResponse = componentInstanceLogic.getRelationById(componentId, relationId, userId, componentTypeEnum);
1084 if (actionResponse.isRight()) {
1085 return buildErrorResponse(actionResponse.right().value());
1087 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
1088 } catch (Exception e) {
1089 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("getRelationById");
1090 log.debug("getRelationById unexpected exception", e);
1091 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
1095 private Either<ComponentInstance, ResponseFormat> convertToResourceInstance(String data) {
1097 // Either<ComponentInstance, ActionStatus> convertStatus =
1098 // convertJsonToObject(data, ComponentInstance.class);
1099 Either<ComponentInstance, ResponseFormat> convertStatus = getComponentsUtils().convertJsonToObjectUsingObjectMapper(data, new User(), ComponentInstance.class, null, ComponentTypeEnum.RESOURCE_INSTANCE);
1100 if (convertStatus.isRight()) {
1101 return Either.right(convertStatus.right().value());
1103 ComponentInstance resourceInstanceInfo = convertStatus.left().value();
1105 return Either.left(resourceInstanceInfo);
1108 private Either<List<ComponentInstance>, ResponseFormat> convertToMultipleResourceInstance(String dataList) {
1110 Either<ComponentInstance[], ResponseFormat> convertStatus = getComponentsUtils().convertJsonToObjectUsingObjectMapper(dataList, new User(), ComponentInstance[].class, null, ComponentTypeEnum.RESOURCE_INSTANCE);
1111 if (convertStatus.isRight()) {
1112 return Either.right(convertStatus.right().value());
1115 return Either.left(Arrays.asList(convertStatus.left().value()));
1118 private Either<List<ComponentInstanceProperty>, ResponseFormat> convertMultipleProperties(String dataList) {
1119 if (StringUtils.isEmpty(dataList)) {
1120 return Either.right(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
1122 Either<ComponentInstanceProperty[], ResponseFormat> convertStatus = getComponentsUtils().convertJsonToObjectUsingObjectMapper(dataList, new User(), ComponentInstanceProperty[].class, null, ComponentTypeEnum.RESOURCE_INSTANCE);
1123 if (convertStatus.isRight()) {
1124 return Either.right(convertStatus.right().value());
1126 return Either.left(Arrays.asList(convertStatus.left().value()));
1129 private Either<List<ComponentInstanceInput>, ResponseFormat> convertMultipleInputs(String dataList) {
1130 if (StringUtils.isEmpty(dataList)) {
1131 return Either.right(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
1133 Either<ComponentInstanceInput[], ResponseFormat> convertStatus = getComponentsUtils().convertJsonToObjectUsingObjectMapper(dataList, new User(), ComponentInstanceInput[].class, null, ComponentTypeEnum.RESOURCE_INSTANCE);
1134 if (convertStatus.isRight()) {
1135 return Either.right(convertStatus.right().value());
1137 return Either.left(Arrays.asList(convertStatus.left().value()));
1141 private Either<RequirementCapabilityRelDef, ResponseFormat> convertToRequirementCapabilityRelDef(String data) {
1143 Either<RequirementCapabilityRelDef, ActionStatus> convertStatus = convertJsonToObject(data, RequirementCapabilityRelDef.class);
1144 if (convertStatus.isRight()) {
1145 return Either.right(getComponentsUtils().getResponseFormat(convertStatus.right().value()));
1147 RequirementCapabilityRelDef requirementCapabilityRelDef = convertStatus.left().value();
1148 return Either.left(requirementCapabilityRelDef);
1151 private <T> Either<T, ActionStatus> convertJsonToObject(String data, Class<T> clazz) {
1153 log.trace("convert json to object. json=\n {}", data);
1155 t = gson.fromJson(data, clazz);
1157 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("convertJsonToObject");
1158 log.debug("object is null after converting from json");
1159 return Either.right(ActionStatus.INVALID_CONTENT);
1161 return Either.left(t);
1162 } catch (Exception e) {
1164 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("convertJsonToObject");
1165 log.debug("failed to convert from json", e);
1166 return Either.right(ActionStatus.INVALID_CONTENT);
1172 @Path("/{containerComponentType}/{componentId}/paths-to-delete")
1173 @Produces(MediaType.APPLICATION_JSON)
1174 @ApiOperation(value = "Check if forwarding path to delete on version change", httpMethod = "GET", notes = "Returns forwarding paths to delete",
1175 response = Response.class)
1176 public Response changeResourceInstanceVersion( @PathParam("componentId") String componentId,
1177 @QueryParam("componentInstanceId") final String oldComponentInstanceId,
1178 @QueryParam("newComponentInstanceId") final String newComponentInstanceId,
1179 @ApiParam(value = "valid values: resources / services",
1180 allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME)
1181 @PathParam("containerComponentType") final String containerComponentType,
1182 @Context final HttpServletRequest request) {
1183 if (oldComponentInstanceId == null){
1184 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.MISSING_OLD_COMPONENT_INSTANCE));
1186 if (newComponentInstanceId == null){
1187 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.MISSING_NEW_COMPONENT_INSTANCE));
1189 ServletContext context = request.getSession().getServletContext();
1191 String url = request.getMethod() + " " + request.getRequestURI();
1192 log.debug("Start handle request of {}", url);
1193 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
1194 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
1195 if (componentInstanceLogic == null) {
1196 log.debug("Unsupported component type {}", containerComponentType);
1197 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
1199 ComponentInstance newComponentInstance;
1200 if(StringUtils.isNotEmpty(newComponentInstanceId)){
1201 newComponentInstance=new ComponentInstance();
1202 newComponentInstance.setToscaPresentationValue(JsonPresentationFields.CI_COMPONENT_UID,newComponentInstanceId);
1204 log.error("missing component id");
1205 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.MISSING_DATA));
1207 Either<Set<String>,ResponseFormat> actionResponse= componentInstanceLogic.forwardingPathOnVersionChange(
1208 containerComponentType,componentId,oldComponentInstanceId,newComponentInstance);
1209 if (actionResponse.isRight()) {
1210 return buildErrorResponse(actionResponse.right().value());
1212 ForwardingPaths forwardingPaths=new ForwardingPaths();
1213 forwardingPaths.setForwardingPathToDelete(actionResponse.left().value());
1214 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), forwardingPaths);