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.Arrays;
26 import java.util.List;
28 import javax.inject.Singleton;
29 import javax.servlet.ServletContext;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.ws.rs.Consumes;
32 import javax.ws.rs.DELETE;
33 import javax.ws.rs.GET;
34 import javax.ws.rs.HeaderParam;
35 import javax.ws.rs.POST;
36 import javax.ws.rs.PUT;
37 import javax.ws.rs.Path;
38 import javax.ws.rs.PathParam;
39 import javax.ws.rs.Produces;
40 import javax.ws.rs.core.Context;
41 import javax.ws.rs.core.MediaType;
42 import javax.ws.rs.core.Response;
44 import org.apache.commons.io.IOUtils;
45 import org.codehaus.jackson.map.ObjectMapper;
46 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
47 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
48 import org.openecomp.sdc.be.config.BeEcompErrorManager;
49 import org.openecomp.sdc.be.dao.api.ActionStatus;
50 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
51 import org.openecomp.sdc.be.info.CreateAndAssotiateInfo;
52 import org.openecomp.sdc.be.info.GroupDefinitionInfo;
53 import org.openecomp.sdc.be.model.ComponentInstance;
54 import org.openecomp.sdc.be.model.ComponentInstanceInput;
55 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
56 import org.openecomp.sdc.be.model.PropertyConstraint;
57 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
58 import org.openecomp.sdc.be.model.Resource;
59 import org.openecomp.sdc.be.model.User;
60 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation.PropertyConstraintDeserialiser;
61 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
62 import org.openecomp.sdc.common.api.Constants;
63 import org.openecomp.sdc.common.datastructure.Wrapper;
64 import org.openecomp.sdc.exception.ResponseFormat;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
68 import com.google.gson.Gson;
69 import com.google.gson.GsonBuilder;
70 import com.google.gson.reflect.TypeToken;
71 import com.jcabi.aspects.Loggable;
73 import fj.data.Either;
74 import io.swagger.annotations.Api;
75 import io.swagger.annotations.ApiOperation;
76 import io.swagger.annotations.ApiParam;
77 import io.swagger.annotations.ApiResponse;
78 import io.swagger.annotations.ApiResponses;
81 * Root resource (exposed at "/" path)
84 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
86 @Api(value = "Resource Instance Servlet", description = "Resource Instance Servlet")
88 public class ComponentInstanceServlet extends AbstractValidationsServlet {
90 private static Logger log = LoggerFactory.getLogger(ComponentInstanceServlet.class.getName());
92 Type constraintType = new TypeToken<PropertyConstraint>() {
95 Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyConstraintDeserialiser()).create();
98 @Path("/{containerComponentType}/{componentId}/resourceInstance")
99 @Consumes(MediaType.APPLICATION_JSON)
100 @Produces(MediaType.APPLICATION_JSON)
101 @ApiOperation(value = "Create ComponentInstance", httpMethod = "POST", notes = "Returns created ComponentInstance", response = Response.class)
102 @ApiResponses(value = { @ApiResponse(code = 201, message = "Component created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
103 @ApiResponse(code = 409, message = "Component instance already exist") })
104 public Response createComponentInstance(@ApiParam(value = "RI object to be created", required = true) String data, @PathParam("componentId") final String containerComponentId,
105 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
106 @HeaderParam(value = Constants.USER_ID_HEADER) @ApiParam(value = "USER_ID of modifier user", required = true) String userId, @Context final HttpServletRequest request) {
107 ServletContext context = request.getSession().getServletContext();
111 ComponentInstance componentInstance = RepresentationUtils.fromRepresentation(data, ComponentInstance.class);
112 componentInstance.setInvariantName(null);
113 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
114 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
115 if (componentInstanceLogic == null) {
116 log.debug("Unsupported component type {}", containerComponentType);
117 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
119 Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.createComponentInstance(containerComponentType, containerComponentId, userId, componentInstance);
121 if (actionResponse.isRight()) {
122 return buildErrorResponse(actionResponse.right().value());
124 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), actionResponse.left().value());
126 } catch (Exception e) {
127 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create Component Instance");
128 log.debug("create component instance failed with exception", e);
129 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
134 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}")
135 @Consumes(MediaType.APPLICATION_JSON)
136 @Produces(MediaType.APPLICATION_JSON)
137 @ApiOperation(value = "Update resource instance", httpMethod = "POST", notes = "Returns updated resource instance", response = Response.class)
138 @ApiResponses(value = { @ApiResponse(code = 200, message = "Resource instance updated"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
139 public Response updateComponentInstanceMetadata(@PathParam("componentId") final String componentId, @PathParam("componentInstanceId") final String componentInstanceId,
140 @ApiParam(value = "valid values: resources / services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
141 + ComponentTypeEnum.PRODUCT_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
142 @Context final HttpServletRequest request) {
143 ServletContext context = request.getSession().getServletContext();
145 String url = request.getMethod() + " " + request.getRequestURI();
146 log.debug("Start handle request of {}", url);
149 log.debug("Start handle request of {}", url);
151 InputStream inputStream = request.getInputStream();
153 byte[] bytes = IOUtils.toByteArray(inputStream);
155 if (bytes == null || bytes.length == 0) {
156 log.info("Empty body was sent.");
157 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
160 String userId = request.getHeader(Constants.USER_ID_HEADER);
162 String data = new String(bytes);
163 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
164 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
165 if (componentInstanceLogic == null) {
166 log.debug("Unsupported component type {}", containerComponentType);
167 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
169 Either<ComponentInstance, ResponseFormat> convertResponse = convertToResourceInstance(data);
171 if (convertResponse.isRight()) {
172 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - updateResourceInstance");
173 log.debug("Failed to convert received data to BE format.");
174 return buildErrorResponse(convertResponse.right().value());
177 ComponentInstance resourceInstance = convertResponse.left().value();
178 Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.updateComponentInstanceMetadata(containerComponentType, componentId, componentInstanceId, userId, resourceInstance);
180 if (actionResponse.isRight()) {
181 return buildErrorResponse(actionResponse.right().value());
183 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
185 } catch (Exception e) {
186 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Resource Instance");
187 log.debug("update resource instance with exception", e);
188 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
194 @Path("/{containerComponentType}/{componentId}/resourceInstance/multipleComponentInstance")
195 @Consumes(MediaType.APPLICATION_JSON)
196 @Produces(MediaType.APPLICATION_JSON)
197 @ApiOperation(value = "Update resource instance multiple component", httpMethod = "POST", notes = "Returns updated resource instance", response = Response.class)
198 @ApiResponses(value = { @ApiResponse(code = 200, message = "Resource instance updated"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
199 public Response updateMultipleComponentInstance(@PathParam("componentId") final String componentId,
200 @ApiParam(value = "valid values: resources / services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
201 + ComponentTypeEnum.PRODUCT_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
202 @Context final HttpServletRequest request, @ApiParam(value = "Component Instance JSON Array", required = true) final String componentInstanceJsonArray) {
204 ServletContext context = request.getSession().getServletContext();
205 String url = request.getMethod() + " " + request.getRequestURI();
206 log.debug("Start handle request of {}", url);
209 log.debug("Start handle request of {}", url);
211 if (componentInstanceJsonArray == null || componentInstanceJsonArray.length() == 0) {
212 log.info("Empty JSON list was sent.");
213 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
216 String userId = request.getHeader(Constants.USER_ID_HEADER);
218 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
219 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
220 if (componentInstanceLogic == null) {
221 log.debug("Unsupported component type {}", containerComponentType);
222 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
225 Either<List<ComponentInstance>, ResponseFormat> convertResponse = convertToMultipleResourceInstance(componentInstanceJsonArray);
227 if (convertResponse.isRight()) {
228 // Using both ECOMP error methods, show to Sofer
229 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - updateResourceInstance");
231 * BeEcompErrorManager.getInstance().processEcompError( EcompErrorName.BeSystemError, "Resource Instance - updateResourceInstance");
233 log.debug("Failed to convert received data to BE format.");
234 return buildErrorResponse(convertResponse.right().value());
237 List<ComponentInstance> componentInstanceList = convertResponse.left().value();
239 Either<List<ComponentInstance>, ResponseFormat> actionResponse = componentInstanceLogic.updateComponentInstance(containerComponentType, componentId, userId, componentInstanceList, true, true);
241 if (actionResponse.isRight()) {
242 return buildErrorResponse(actionResponse.right().value());
245 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
247 } catch (Exception e) {
249 * BeEcompErrorManager.getInstance().processEcompError( EcompErrorName.BeRestApiGeneralError, "Update Resource Instance" );
251 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Resource Instance");
252 log.debug("update resource instance with exception", e);
253 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
259 @Path("/{containerComponentType}/{componentId}/resourceInstance/{resourceInstanceId}")
260 @Consumes(MediaType.APPLICATION_JSON)
261 @Produces(MediaType.APPLICATION_JSON)
262 @ApiOperation(value = "Delete ResourceInstance", httpMethod = "DELETE", notes = "Returns delete resourceInstance", response = Response.class)
263 @ApiResponses(value = { @ApiResponse(code = 201, message = "ResourceInstance deleted"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
264 public Response deleteResourceInstance(@PathParam("componentId") final String componentId, @PathParam("resourceInstanceId") final String resourceInstanceId,
265 @ApiParam(value = "valid values: resources / services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
266 + ComponentTypeEnum.PRODUCT_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
267 @Context final HttpServletRequest request) {
268 ServletContext context = request.getSession().getServletContext();
269 String url = request.getMethod() + " " + request.getRequestURI();
270 Response response = null;
272 log.debug("Start handle request of {}", url);
273 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
274 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
275 if (componentInstanceLogic == null) {
276 log.debug("Unsupported component type {}", containerComponentType);
277 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
279 String userId = request.getHeader(Constants.USER_ID_HEADER);
280 Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.deleteComponentInstance(containerComponentType, componentId, resourceInstanceId, userId);
282 if (actionResponse.isRight()) {
283 response = buildErrorResponse(actionResponse.right().value());
285 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT), null);
288 } catch (Exception e) {
289 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Resource Instance");
290 log.debug("delete resource instance with exception", e);
291 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
295 @ApiParam(value = "allowed values are resources /services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + "," + ComponentTypeEnum.PRODUCT_PARAM_NAME, required = true)
297 @Path("/{containerComponentType}/{componentId}/resourceInstance/associate")
298 @Consumes(MediaType.APPLICATION_JSON)
299 @Produces(MediaType.APPLICATION_JSON)
300 @ApiOperation(value = "Associate RI to RI", httpMethod = "POST", notes = "Returns created RelationshipInfo", response = Response.class)
301 @ApiResponses(value = { @ApiResponse(code = 201, message = "Relationship created"), @ApiResponse(code = 403, message = "Missing information"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
302 @ApiResponse(code = 409, message = "Relationship already exist") })
303 public Response associateRIToRI(@ApiParam(value = "unique id of the container component") @PathParam("componentId") final String componentId,
304 @ApiParam(value = "allowed values are resources /services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
305 + ComponentTypeEnum.PRODUCT_PARAM_NAME, required = true) @PathParam("containerComponentType") final String containerComponentType,
306 @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @ApiParam(value = "RelationshipInfo", required = true) String data, @Context final HttpServletRequest request) {
307 ServletContext context = request.getSession().getServletContext();
309 String url = request.getMethod() + " " + request.getRequestURI();
310 log.debug("Start handle request of {}", url);
311 Response response = null;
315 log.debug("Start handle request of {}", url);
317 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
318 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
319 if (componentInstanceLogic == null) {
320 log.debug("Unsupported component type {}", containerComponentType);
321 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
324 Either<RequirementCapabilityRelDef, ResponseFormat> regInfoW = convertToRequirementCapabilityRelDef(data);
326 Either<RequirementCapabilityRelDef, ResponseFormat> resultOp;
327 if (regInfoW.isRight()) {
328 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - associateRIToRI");
329 log.debug("Failed to convert received data to BE format.");
330 resultOp = Either.right(regInfoW.right().value());
332 RequirementCapabilityRelDef requirementDef = regInfoW.left().value();
333 resultOp = componentInstanceLogic.associateRIToRI(componentId, userId, requirementDef, componentTypeEnum);
336 Either<RequirementCapabilityRelDef, ResponseFormat> actionResponse = resultOp;
338 if (actionResponse.isRight()) {
339 response = buildErrorResponse(actionResponse.right().value());
341 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
345 } catch (Exception e) {
346 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Associate Resource Instance");
347 log.debug("associate resource instance to another RI with exception", e);
348 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
353 @Path("/{containerComponentType}/{componentId}/resourceInstance/dissociate")
354 @Consumes(MediaType.APPLICATION_JSON)
355 @Produces(MediaType.APPLICATION_JSON)
356 @ApiOperation(value = "Dissociate RI from RI", httpMethod = "PUT", notes = "Returns deleted RelationshipInfo", response = Response.class)
357 @ApiResponses(value = { @ApiResponse(code = 201, message = "Relationship deleted"), @ApiResponse(code = 403, message = "Missing information"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
358 public Response dissociateRIFromRI(
359 @ApiParam(value = "allowed values are resources /services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
360 + ComponentTypeEnum.PRODUCT_PARAM_NAME, required = true) @PathParam("containerComponentType") final String containerComponentType,
361 @ApiParam(value = "unique id of the container component") @PathParam("componentId") final String componentId, @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @ApiParam(value = "RelationshipInfo", required = true) String data,
362 @Context final HttpServletRequest request) {
363 ServletContext context = request.getSession().getServletContext();
365 String url = request.getMethod() + " " + request.getRequestURI();
366 log.debug("Start handle request of {}", url);
370 log.debug("Start handle request of {}", url);
372 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
373 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
374 if (componentInstanceLogic == null) {
375 log.debug("Unsupported component type {}", containerComponentType);
376 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
379 Either<RequirementCapabilityRelDef, ResponseFormat> regInfoW = convertToRequirementCapabilityRelDef(data);
380 if (regInfoW.isRight()) {
381 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - dissociateRIFromRI");
382 log.debug("Failed to convert received data to BE format.");
383 return buildErrorResponse(regInfoW.right().value());
386 RequirementCapabilityRelDef requirementDef = regInfoW.left().value();
387 Either<RequirementCapabilityRelDef, ResponseFormat> actionResponse = componentInstanceLogic.dissociateRIFromRI(componentId, userId, requirementDef, componentTypeEnum);
389 if (actionResponse.isRight()) {
390 return buildErrorResponse(actionResponse.right().value());
392 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
394 } catch (Exception e) {
395 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Dissociate Resource Instance");
396 log.debug("dissociate resource instance from service failed with exception", e);
397 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
402 @Path("/{containerComponentType}/{componentId}/resourceInstance/createAndAssociate")
403 @Consumes(MediaType.APPLICATION_JSON)
404 @Produces(MediaType.APPLICATION_JSON)
405 @ApiOperation(value = "Create RI and associate RI to RI", httpMethod = "POST", notes = "Returns created RI and RelationshipInfo", response = Response.class)
406 @ApiResponses(value = { @ApiResponse(code = 201, message = "RI created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
407 @ApiResponse(code = 409, message = "Relationship already exist") })
408 public Response createAndAssociateRIToRI(@PathParam("componentId") final String componentId,
409 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
410 @Context final HttpServletRequest request) {
411 ServletContext context = request.getSession().getServletContext();
413 String url = request.getMethod() + " " + request.getRequestURI();
414 log.debug("Start handle request of {}", url);
417 log.debug("Start handle request of {}", url);
419 InputStream inputStream = request.getInputStream();
421 byte[] bytes = IOUtils.toByteArray(inputStream);
423 if (bytes == null || bytes.length == 0) {
424 log.info("Empty body was sent.");
425 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
428 String userId = request.getHeader(Constants.USER_ID_HEADER);
430 String data = new String(bytes);
432 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
433 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
434 if (componentInstanceLogic == null) {
435 log.debug("Unsupported component type {}", containerComponentType);
436 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
439 Either<CreateAndAssotiateInfo, ActionStatus> convertStatus = convertJsonToObject(data, CreateAndAssotiateInfo.class);
440 if (convertStatus.isRight()) {
441 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - createAndAssociateRIToRI");
442 log.debug("Failed to convert received data to BE format.");
443 Either<Object, ResponseFormat> formattedResponse = Either.right(getComponentsUtils().getResponseFormat(convertStatus.right().value()));
444 return buildErrorResponse(formattedResponse.right().value());
447 CreateAndAssotiateInfo createAndAssotiateInfo = convertStatus.left().value();
448 Either<CreateAndAssotiateInfo, ResponseFormat> actionResponse = componentInstanceLogic.createAndAssociateRIToRI(containerComponentType, componentId, userId, createAndAssotiateInfo);
450 if (actionResponse.isRight()) {
451 return buildErrorResponse(actionResponse.right().value());
453 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), actionResponse.left().value());
454 } catch (Exception e) {
455 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create and Associate Resource Instance");
456 log.debug("create and associate RI failed with exception", e);
457 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
462 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/property")
463 @Consumes(MediaType.APPLICATION_JSON)
464 @Produces(MediaType.APPLICATION_JSON)
465 @ApiOperation(value = "Update resource instance property", httpMethod = "POST", notes = "Returns updated resource instance property", response = Response.class)
466 @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
467 public Response updateResourceInstanceProperty(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
468 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
469 @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,
470 @Context final HttpServletRequest request) {
472 String url = request.getMethod() + " " + request.getRequestURI();
473 log.debug("Start handle request of {}", url);
476 Wrapper<String> dataWrapper = new Wrapper<>();
477 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
478 Wrapper<ComponentInstanceProperty> propertyWrapper = new Wrapper<>();
480 validateInputStream(request, dataWrapper, errorWrapper);
482 if (errorWrapper.isEmpty()) {
483 validateClassParse(dataWrapper.getInnerElement(), propertyWrapper, () -> ComponentInstanceProperty.class, errorWrapper);
486 if (!errorWrapper.isEmpty()) {
487 return buildErrorResponse(errorWrapper.getInnerElement());
490 ComponentInstanceProperty property = propertyWrapper.getInnerElement();
492 log.debug("Start handle request of updateResourceInstanceProperty. Received property is {}", property);
494 ServletContext context = request.getSession().getServletContext();
496 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
497 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
498 if (componentInstanceLogic == null) {
499 log.debug("Unsupported component type {}", containerComponentType);
500 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
503 Either<ComponentInstanceProperty, ResponseFormat> actionResponse = componentInstanceLogic.createOrUpdatePropertyValue(componentTypeEnum, componentId, componentInstanceId, property, userId);
505 if (actionResponse.isRight()) {
506 return buildErrorResponse(actionResponse.right().value());
509 ComponentInstanceProperty resourceInstanceProperty = actionResponse.left().value();
510 ObjectMapper mapper = new ObjectMapper();
511 String result = mapper.writeValueAsString(resourceInstanceProperty);
512 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
514 } catch (Exception e) {
515 log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
516 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
522 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/input")
523 @Consumes(MediaType.APPLICATION_JSON)
524 @Produces(MediaType.APPLICATION_JSON)
525 @ApiOperation(value = "Update resource instance property", httpMethod = "POST", notes = "Returns updated resource instance property", response = Response.class)
526 @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
527 public Response updateResourceInstanceInput(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
528 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
529 @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,
530 @Context final HttpServletRequest request) {
532 String url = request.getMethod() + " " + request.getRequestURI();
533 log.debug("Start handle request of {}", url);
536 Wrapper<String> dataWrapper = new Wrapper<>();
537 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
538 Wrapper<ComponentInstanceInput> propertyWrapper = new Wrapper<>();
540 validateInputStream(request, dataWrapper, errorWrapper);
541 ComponentInstanceInput property = null;
543 if (errorWrapper.isEmpty()) {
544 User modifier = new User();
545 modifier.setUserId(userId);
546 log.debug("modifier id is {}", userId);
548 Either<ComponentInstanceInput, ResponseFormat> inputEither = getComponentsUtils().convertJsonToObjectUsingObjectMapper(dataWrapper.getInnerElement(), modifier, ComponentInstanceInput.class, AuditingActionEnum.UPDATE_RESOURCE_METADATA, ComponentTypeEnum.SERVICE);;
549 if(inputEither.isRight()){
550 log.debug("Failed to convert data to input definition. Status is {}", inputEither.right().value());
551 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
553 property = inputEither.left().value();
557 if (property == null) {
558 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
562 log.debug("Start handle request of updateResourceInstanceProperty. Received property is {}", property);
564 ServletContext context = request.getSession().getServletContext();
566 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
567 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
568 if (componentInstanceLogic == null) {
569 log.debug("Unsupported component type {}", containerComponentType);
570 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
573 Either<ComponentInstanceInput, ResponseFormat> actionResponse = componentInstanceLogic.createOrUpdateInstanceInputValue(componentTypeEnum, componentId, componentInstanceId, property, userId);
575 if (actionResponse.isRight()) {
576 return buildErrorResponse(actionResponse.right().value());
579 ComponentInstanceInput resourceInstanceProperty = actionResponse.left().value();
580 ObjectMapper mapper = new ObjectMapper();
581 String result = mapper.writeValueAsString(resourceInstanceProperty);
582 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
584 } catch (Exception e) {
585 log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
586 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
592 * Updates ResourceInstance Attribute
595 * @param containerComponentType
596 * @param componentInstanceId
602 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/attribute")
603 @Consumes(MediaType.APPLICATION_JSON)
604 @Produces(MediaType.APPLICATION_JSON)
605 @ApiOperation(value = "Update resource instance attribute", httpMethod = "POST", notes = "Returns updated resource instance attribute", response = Response.class)
606 @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
607 public Response updateResourceInstanceAttribute(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
608 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
609 @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,
610 @Context final HttpServletRequest request) {
612 String url = request.getMethod() + " " + request.getRequestURI();
613 log.debug("Start handle request of {}", url);
617 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
618 Wrapper<String> dataWrapper = new Wrapper<>();
619 Wrapper<ComponentInstanceProperty> attributeWrapper = new Wrapper<>();
620 Wrapper<ComponentInstanceBusinessLogic> blWrapper = new Wrapper<>();
622 validateInputStream(request, dataWrapper, errorWrapper);
624 if (errorWrapper.isEmpty()) {
625 validateClassParse(dataWrapper.getInnerElement(), attributeWrapper, () -> ComponentInstanceProperty.class, errorWrapper);
628 if (errorWrapper.isEmpty()) {
629 validateComponentInstanceBusinessLogic(request, containerComponentType, blWrapper, errorWrapper);
632 if (errorWrapper.isEmpty()) {
633 ComponentInstanceBusinessLogic componentInstanceLogic = blWrapper.getInnerElement();
634 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
635 log.debug("Start handle request of ComponentInstanceAttribute. Received attribute is {}", attributeWrapper.getInnerElement());
636 Either<ComponentInstanceProperty, ResponseFormat> eitherAttribute = componentInstanceLogic.createOrUpdateAttributeValue(componentTypeEnum, componentId, componentInstanceId, attributeWrapper.getInnerElement(), userId);
637 if (eitherAttribute.isRight()) {
638 errorWrapper.setInnerElement(eitherAttribute.right().value());
640 attributeWrapper.setInnerElement(eitherAttribute.left().value());
644 return buildResponseFromElement(errorWrapper, attributeWrapper);
646 } catch (Exception e) {
647 log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
648 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
654 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/property/{propertyId}")
655 @Consumes(MediaType.APPLICATION_JSON)
656 @Produces(MediaType.APPLICATION_JSON)
657 @ApiOperation(value = "Update resource instance", httpMethod = "DELETE", notes = "Returns deleted resource instance property", response = Response.class)
658 @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
659 public Response deleteResourceInstanceProperty(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
660 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
661 @ApiParam(value = "resource instance id") @PathParam("componentInstanceId") final String componentInstanceId, @ApiParam(value = "property id") @PathParam("propertyId") final String propertyId,
662 @ApiParam(value = "id of user initiating the operation") @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Context final HttpServletRequest request) {
664 ServletContext context = request.getSession().getServletContext();
666 String url = request.getMethod() + " " + request.getRequestURI();
667 log.debug("Start handle request of {}", url);
670 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
671 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
672 if (componentInstanceLogic == null) {
673 log.debug("Unsupported component type {}", containerComponentType);
674 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
677 Either<ComponentInstanceProperty, ResponseFormat> actionResponse = componentInstanceLogic.deletePropertyValue(componentTypeEnum, componentId, componentInstanceId, propertyId, userId);
678 if (actionResponse.isRight()) {
679 return buildErrorResponse(actionResponse.right().value());
681 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT), null);
682 } catch (Exception e) {
683 log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
684 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
690 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/changeVersion")
691 @Consumes(MediaType.APPLICATION_JSON)
692 @Produces(MediaType.APPLICATION_JSON)
693 @ApiOperation(value = "Update resource instance", httpMethod = "POST", notes = "Returns updated resource instance", response = Response.class)
694 @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
695 public Response changeResourceInstanceVersion(@PathParam("componentId") final String componentId, @PathParam("componentInstanceId") final String componentInstanceId,
696 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
697 @Context final HttpServletRequest request) {
698 ServletContext context = request.getSession().getServletContext();
700 String url = request.getMethod() + " " + request.getRequestURI();
701 log.debug("Start handle request of {}", url);
703 InputStream inputStream = request.getInputStream();
705 byte[] bytes = IOUtils.toByteArray(inputStream);
707 if (bytes == null || bytes.length == 0) {
708 log.info("Empty body was sent.");
709 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
712 String userId = request.getHeader(Constants.USER_ID_HEADER);
714 String data = new String(bytes);
716 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
717 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
718 if (componentInstanceLogic == null) {
719 log.debug("Unsupported component type {}", containerComponentType);
720 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
723 Either<ComponentInstance, ResponseFormat> convertResponse = convertToResourceInstance(data);
725 if (convertResponse.isRight()) {
726 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - updateResourceInstance");
727 log.debug("Failed to convert received data to BE format.");
728 return buildErrorResponse(convertResponse.right().value());
731 ComponentInstance newResourceInstance = convertResponse.left().value();
732 Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.changeComponentInstanceVersion(containerComponentType, componentId, componentInstanceId, userId, newResourceInstance);
734 if (actionResponse.isRight()) {
735 return buildErrorResponse(actionResponse.right().value());
737 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
739 } catch (Exception e) {
740 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Resource Instance");
741 log.debug("update resource instance with exception", e);
742 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
748 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/groupInstance/{groupInstanceId}/property")
749 @Consumes(MediaType.APPLICATION_JSON)
750 @Produces(MediaType.APPLICATION_JSON)
751 @ApiOperation(value = "Update resource instance property", httpMethod = "POST", notes = "Returns updated resource instance property", response = Response.class)
752 @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
753 public Response updateGroupInstanceProperty(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
754 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
755 @ApiParam(value = "resource instance id") @PathParam("componentInstanceId") final String componentInstanceId, @ApiParam(value = "group instance id") @PathParam("groupInstanceId") final String groupInstanceId, @ApiParam(value = "id of user initiating the operation") @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
756 @Context final HttpServletRequest request) {
758 String url = request.getMethod() + " " + request.getRequestURI();
759 log.debug("Start handle request of {}", url);
762 Wrapper<String> dataWrapper = new Wrapper<>();
763 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
764 Wrapper<ComponentInstanceProperty> propertyWrapper = new Wrapper<>();
766 validateInputStream(request, dataWrapper, errorWrapper);
768 if (errorWrapper.isEmpty()) {
769 validateClassParse(dataWrapper.getInnerElement(), propertyWrapper, () -> ComponentInstanceProperty.class, errorWrapper);
772 if (!errorWrapper.isEmpty()) {
773 return buildErrorResponse(errorWrapper.getInnerElement());
776 ComponentInstanceProperty property = propertyWrapper.getInnerElement();
778 log.debug("Start handle request of updateResourceInstanceProperty. Received property is {}", property);
780 ServletContext context = request.getSession().getServletContext();
782 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
783 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
784 if (componentInstanceLogic == null) {
785 log.debug("Unsupported component type {}", containerComponentType);
786 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
789 Either<ComponentInstanceProperty, ResponseFormat> actionResponse = componentInstanceLogic.createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, componentInstanceId, groupInstanceId, property, userId);
791 if (actionResponse.isRight()) {
792 return buildErrorResponse(actionResponse.right().value());
795 ComponentInstanceProperty resourceInstanceProperty = actionResponse.left().value();
796 ObjectMapper mapper = new ObjectMapper();
797 String result = mapper.writeValueAsString(resourceInstanceProperty);
798 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
800 } catch (Exception e) {
801 log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
802 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
808 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/groupInstance/{groupInstId}")
809 @Consumes(MediaType.APPLICATION_JSON)
810 @Produces(MediaType.APPLICATION_JSON)
811 @ApiOperation(value = "Get group artifacts ", httpMethod = "GET", notes = "Returns artifacts metadata according to groupInstId", response = Resource.class)
812 @ApiResponses(value = { @ApiResponse(code = 200, message = "group found"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Group not found") })
813 public Response getGroupArtifactById(@PathParam("containerComponentType") final String containerComponentType, @PathParam("componentId") final String componentId, @PathParam("componentInstanceId") final String componentInstanceId, @PathParam("groupInstId") final String groupInstId,
814 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
815 ServletContext context = request.getSession().getServletContext();
816 String url = request.getMethod() + " " + request.getRequestURI();
817 log.debug("(GET) Start handle request of {}", url);
821 GroupBusinessLogic businessLogic = this.getGroupBL(context);
822 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
823 Either<GroupDefinitionInfo, ResponseFormat> actionResponse = businessLogic.getGroupInstWithArtifactsById(componentTypeEnum, componentId, componentInstanceId, groupInstId, userId, false);
825 if (actionResponse.isRight()) {
826 log.debug("failed to get all non abstract {}", containerComponentType);
827 return buildErrorResponse(actionResponse.right().value());
830 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
832 } catch (Exception e) {
833 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("getGroupArtifactById");
834 log.debug("getGroupArtifactById unexpected exception", e);
835 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
842 @Path("/{containerComponentType}/{containerComponentId}/componentInstances/{componentInstanceUniqueId}/properties")
843 @Consumes(MediaType.APPLICATION_JSON)
844 @Produces(MediaType.APPLICATION_JSON)
845 @ApiOperation(value = "Get component instance properties", httpMethod = "GET",
846 notes = "Returns component instance properties", response = Response.class)
847 @ApiResponses(value = { @ApiResponse(code = 200, message = "Properties found"),
848 @ApiResponse(code = 403, message = "Restricted operation"),
849 @ApiResponse(code = 404, message = "Component/Component Instance - not found") })
850 public Response getInstancePropertiesById(@PathParam("containerComponentType") final String containerComponentType,
851 @PathParam("containerComponentId") final String containerComponentId,
852 @PathParam("componentInstanceUniqueId") final String componentInstanceUniqueId,
853 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
855 ServletContext context = request.getSession().getServletContext();
856 String url = request.getMethod() + " " + request.getRequestURI();
857 log.debug("(GET) Start handle request of {}", url);
860 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
861 ComponentInstanceBusinessLogic componentInstanceBL = getComponentInstanceBL(context, componentTypeEnum);
863 Either<List<ComponentInstanceProperty>, ResponseFormat> componentInstancePropertiesById = componentInstanceBL
864 .getComponentInstancePropertiesById(containerComponentType, containerComponentId,
865 componentInstanceUniqueId, userId);
867 if (componentInstancePropertiesById.isRight()) {
868 log.debug("Failed to get properties of component instance ID: {} in {} with ID: {}",
869 componentInstanceUniqueId, containerComponentType, containerComponentId);
870 return buildErrorResponse(componentInstancePropertiesById.right().value());
873 return buildOkResponse(getComponentsUtils().
874 getResponseFormat(ActionStatus.OK), componentInstancePropertiesById.left().value());
875 } catch (Exception e) {
876 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("getGroupArtifactById");
877 log.debug("getGroupArtifactById unexpected exception", e);
878 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
884 @Path("/{containerComponentType}/{containerComponentId}/serviceProxy")
885 @Consumes(MediaType.APPLICATION_JSON)
886 @Produces(MediaType.APPLICATION_JSON)
887 @ApiOperation(value = "Create service proxy", httpMethod = "POST", notes = "Returns created service proxy", response = Response.class)
888 @ApiResponses(value = { @ApiResponse(code = 201, message = "Service proxy created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
889 @ApiResponse(code = 409, message = "Service proxy already exist") })
890 public Response createServiceProxy(@ApiParam(value = "RI object to be created", required = true) String data, @PathParam("containerComponentId") final String containerComponentId,
891 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
892 @HeaderParam(value = Constants.USER_ID_HEADER) @ApiParam(value = "USER_ID of modifier user", required = true) String userId, @Context final HttpServletRequest request) {
893 ServletContext context = request.getSession().getServletContext();
897 ComponentInstance componentInstance = RepresentationUtils.fromRepresentation(data, ComponentInstance.class);
898 componentInstance.setInvariantName(null);
899 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
900 if(componentTypeEnum != ComponentTypeEnum.SERVICE){
901 log.debug("Unsupported container component type {}", containerComponentType);
902 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
904 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
905 if (componentInstanceLogic == null) {
906 log.debug("Unsupported component type {}", containerComponentType);
907 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
909 Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.createServiceProxy(containerComponentType, containerComponentId, userId, componentInstance);
911 if (actionResponse.isRight()) {
912 return buildErrorResponse(actionResponse.right().value());
914 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), actionResponse.left().value());
916 } catch (Exception e) {
917 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create service proxy");
918 log.debug("Create service proxy failed with exception", e);
919 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
924 @Path("/{containerComponentType}/{containerComponentId}/serviceProxy/{serviceProxyId}")
925 @Consumes(MediaType.APPLICATION_JSON)
926 @Produces(MediaType.APPLICATION_JSON)
927 @ApiOperation(value = "Delete service proxy", httpMethod = "DELETE", notes = "Returns delete service proxy", response = Response.class)
928 @ApiResponses(value = { @ApiResponse(code = 201, message = "Service proxy deleted"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
929 public Response deleteServiceProxy(@PathParam("containerComponentId") final String containerComponentId, @PathParam("serviceProxyId") final String serviceProxyId,
930 @ApiParam(value = "valid values: resources / services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
931 + ComponentTypeEnum.PRODUCT_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
932 @Context final HttpServletRequest request) {
933 ServletContext context = request.getSession().getServletContext();
934 String url = request.getMethod() + " " + request.getRequestURI();
935 Response response = null;
937 log.debug("Start handle request of {}", url);
938 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
939 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
940 if (componentInstanceLogic == null) {
941 log.debug("Unsupported component type {}", containerComponentType);
942 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
944 String userId = request.getHeader(Constants.USER_ID_HEADER);
945 Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.deleteServiceProxy(containerComponentType, containerComponentId, serviceProxyId, userId);
947 if (actionResponse.isRight()) {
948 response = buildErrorResponse(actionResponse.right().value());
950 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT), null);
953 } catch (Exception e) {
954 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete service proxy");
955 log.debug("Delete service proxy failed with exception", e);
956 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
961 @Path("/{containerComponentType}/{containerComponentId}/serviceProxy/{serviceProxyId}/changeVersion/{newServiceId}")
962 @Consumes(MediaType.APPLICATION_JSON)
963 @Produces(MediaType.APPLICATION_JSON)
964 @ApiOperation(value = "Update service proxy with new version", httpMethod = "POST", notes = "Returns updated service proxy", response = Response.class)
965 @ApiResponses(value = { @ApiResponse(code = 201, message = "Service proxy created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
966 public Response changeServiceProxyVersion(@PathParam("containerComponentId") final String containerComponentId, @PathParam("serviceProxyId") final String serviceProxyId,
967 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
968 @Context final HttpServletRequest request) {
969 ServletContext context = request.getSession().getServletContext();
971 String url = request.getMethod() + " " + request.getRequestURI();
972 log.debug("Start handle request of {}", url);
975 String userId = request.getHeader(Constants.USER_ID_HEADER);
977 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
978 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context, componentTypeEnum);
979 if (componentInstanceLogic == null) {
980 log.debug("Unsupported component type {}", containerComponentType);
981 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
983 Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.changeServiceProxyVersion(containerComponentType, containerComponentId, serviceProxyId, userId);
985 if (actionResponse.isRight()) {
986 return buildErrorResponse(actionResponse.right().value());
988 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
990 } catch (Exception e) {
991 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update service proxy with new version");
992 log.debug("Update service proxy with new version failed with exception", e);
993 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
997 private Either<ComponentInstance, ResponseFormat> convertToResourceInstance(String data) {
999 // Either<ComponentInstance, ActionStatus> convertStatus =
1000 // convertJsonToObject(data, ComponentInstance.class);
1001 Either<ComponentInstance, ResponseFormat> convertStatus = getComponentsUtils().convertJsonToObjectUsingObjectMapper(data, new User(), ComponentInstance.class, null, ComponentTypeEnum.RESOURCE_INSTANCE);
1002 if (convertStatus.isRight()) {
1003 return Either.right(convertStatus.right().value());
1005 ComponentInstance resourceInstanceInfo = convertStatus.left().value();
1007 return Either.left(resourceInstanceInfo);
1010 private Either<List<ComponentInstance>, ResponseFormat> convertToMultipleResourceInstance(String dataList) {
1012 Either<ComponentInstance[], ResponseFormat> convertStatus = getComponentsUtils().convertJsonToObjectUsingObjectMapper(dataList, new User(), ComponentInstance[].class, null, ComponentTypeEnum.RESOURCE_INSTANCE);
1013 if (convertStatus.isRight()) {
1014 return Either.right(convertStatus.right().value());
1017 return Either.left(Arrays.asList(convertStatus.left().value()));
1020 private Either<RequirementCapabilityRelDef, ResponseFormat> convertToRequirementCapabilityRelDef(String data) {
1022 Either<RequirementCapabilityRelDef, ActionStatus> convertStatus = convertJsonToObject(data, RequirementCapabilityRelDef.class);
1023 if (convertStatus.isRight()) {
1024 return Either.right(getComponentsUtils().getResponseFormat(convertStatus.right().value()));
1026 RequirementCapabilityRelDef requirementCapabilityRelDef = convertStatus.left().value();
1027 return Either.left(requirementCapabilityRelDef);
1030 private <T> Either<T, ActionStatus> convertJsonToObject(String data, Class<T> clazz) {
1032 log.trace("convert json to object. json=\n {}", data);
1034 t = gson.fromJson(data, clazz);
1036 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("convertJsonToObject");
1037 log.debug("object is null after converting from json");
1038 return Either.right(ActionStatus.INVALID_CONTENT);
1040 return Either.left(t);
1041 } catch (Exception e) {
1043 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("convertJsonToObject");
1044 log.debug("failed to convert from json", e);
1045 return Either.right(ActionStatus.INVALID_CONTENT);