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.reflect.TypeToken;
25 import com.jcabi.aspects.Loggable;
26 import fj.data.Either;
27 import io.swagger.annotations.Api;
28 import io.swagger.annotations.ApiOperation;
29 import io.swagger.annotations.ApiParam;
30 import io.swagger.annotations.ApiResponse;
31 import io.swagger.annotations.ApiResponses;
32 import javax.inject.Inject;
33 import javax.ws.rs.Consumes;
34 import javax.ws.rs.DELETE;
35 import javax.ws.rs.GET;
36 import javax.ws.rs.HeaderParam;
37 import javax.ws.rs.POST;
38 import javax.ws.rs.PUT;
39 import javax.ws.rs.Path;
40 import javax.ws.rs.PathParam;
41 import javax.ws.rs.Produces;
42 import org.apache.http.HttpStatus;
43 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
44 import org.openecomp.sdc.be.components.impl.ResourceBusinessLogic;
45 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
46 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
47 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction;
48 import org.openecomp.sdc.be.config.BeEcompErrorManager;
49 import org.openecomp.sdc.be.dao.api.ActionStatus;
50 import org.openecomp.sdc.be.datamodel.ServiceRelations;
51 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
52 import org.openecomp.sdc.be.impl.ComponentsUtils;
53 import org.openecomp.sdc.be.impl.ServletUtils;
54 import org.openecomp.sdc.be.model.DistributionStatusEnum;
55 import org.openecomp.sdc.be.model.GroupInstanceProperty;
56 import org.openecomp.sdc.be.model.Resource;
57 import org.openecomp.sdc.be.model.Service;
58 import org.openecomp.sdc.be.model.User;
59 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
60 import org.openecomp.sdc.be.user.UserBusinessLogic;
61 import org.openecomp.sdc.common.api.Constants;
62 import org.openecomp.sdc.common.datastructure.Wrapper;
63 import org.openecomp.sdc.common.log.wrappers.Logger;
64 import org.openecomp.sdc.exception.ResponseFormat;
66 import javax.inject.Singleton;
67 import javax.servlet.ServletContext;
68 import javax.servlet.http.HttpServletRequest;
69 import javax.ws.rs.core.Context;
70 import javax.ws.rs.core.MediaType;
71 import javax.ws.rs.core.Response;
72 import java.lang.reflect.Type;
73 import java.util.ArrayList;
74 import java.util.List;
77 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
79 @Api(value = "Service Catalog", description = "Service Servlet")
81 public class ServiceServlet extends AbstractValidationsServlet {
83 private static final Logger log = Logger.getLogger(ServiceServlet.class);
84 private final ServiceBusinessLogic serviceBusinessLogic;
85 private final ResourceBusinessLogic resourceBusinessLogic;
88 public ServiceServlet(UserBusinessLogic userBusinessLogic,
89 ComponentInstanceBusinessLogic componentInstanceBL,
90 ComponentsUtils componentsUtils, ServletUtils servletUtils,
91 ResourceImportManager resourceImportManager,
92 ServiceBusinessLogic serviceBusinessLogic,
93 ResourceBusinessLogic resourceBusinessLogic) {
94 super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
95 this.serviceBusinessLogic = serviceBusinessLogic;
96 this.resourceBusinessLogic = resourceBusinessLogic;
101 @Consumes(MediaType.APPLICATION_JSON)
102 @Produces(MediaType.APPLICATION_JSON)
103 @ApiOperation(value = "Create Service", httpMethod = "POST", notes = "Returns created service", response = Service.class)
104 @ApiResponses(value = { @ApiResponse(code = 201, message = "Service created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
105 @ApiResponse(code = 409, message = "Service already exist") })
106 public Response createService(@ApiParam(value = "Service object to be created", required = true) String data, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
108 String url = request.getMethod() + " " + request.getRequestURI();
109 log.debug("Start handle request of {}", url);
111 User modifier = new User();
112 modifier.setUserId(userId);
113 log.debug("modifier id is {}", userId);
115 Response response = null;
117 Either<Service, ResponseFormat> convertResponse = parseToService(data, modifier);
118 if (convertResponse.isRight()) {
119 log.debug("failed to parse service");
120 response = buildErrorResponse(convertResponse.right().value());
124 Service service = convertResponse.left().value();
125 Either<Service, ResponseFormat> actionResponse = serviceBusinessLogic.createService(service, modifier);
127 if (actionResponse.isRight()) {
128 log.debug("Failed to create service");
129 response = buildErrorResponse(actionResponse.right().value());
133 Object result = RepresentationUtils.toRepresentation(actionResponse.left().value());
134 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), result);
137 } catch (Exception e) {
138 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create Service");
139 log.debug("create service failed with exception", e);
140 response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
145 public Either<Service, ResponseFormat> parseToService(String serviceJson, User user) {
146 return getComponentsUtils().convertJsonToObjectUsingObjectMapper(serviceJson, user, Service.class, AuditingActionEnum.CREATE_RESOURCE, ComponentTypeEnum.SERVICE);
150 @Path("/services/validate-name/{serviceName}")
151 @Consumes(MediaType.APPLICATION_JSON)
152 @Produces(MediaType.APPLICATION_JSON)
153 @ApiOperation(value = "validate service name", httpMethod = "GET", notes = "checks if the chosen service name is available ", response = Response.class)
154 @ApiResponses(value = { @ApiResponse(code = 200, message = "Service found"), @ApiResponse(code = 403, message = "Restricted operation") })
155 public Response validateServiceName(@PathParam("serviceName") final String serviceName, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
156 String url = request.getMethod() + " " + request.getRequestURI();
157 log.debug("Start handle request of {}", url);
160 User modifier = new User();
161 modifier.setUserId(userId);
162 log.debug("modifier id is {}", userId);
163 Response response = null;
165 Either<Map<String, Boolean>, ResponseFormat> actionResponse = serviceBusinessLogic.validateServiceNameExists(serviceName, userId);
167 if (actionResponse.isRight()) {
168 log.debug("failed to get validate service name");
169 response = buildErrorResponse(actionResponse.right().value());
172 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
173 } catch (Exception e) {
174 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Validate Service Name");
175 log.debug("validate service name failed with exception", e);
176 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
181 @Path("/audit-records/{componentType}/{componentUniqueId}")
182 @Consumes(MediaType.APPLICATION_JSON)
183 @Produces(MediaType.APPLICATION_JSON)
184 @ApiOperation(value = "get component audit records", httpMethod = "GET", notes = "get audit records for a service or a resource", response = Response.class)
185 @ApiResponses(value = { @ApiResponse(code = 200, message = "Service found"), @ApiResponse(code = 403, message = "Restricted operation") })
186 public Response getComponentAuditRecords(@PathParam("componentType") final String componentType, @PathParam("componentUniqueId") final String componentUniqueId, @Context final HttpServletRequest request,
187 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
189 ServletContext context = request.getSession().getServletContext();
190 String url = request.getMethod() + " " + request.getRequestURI();
191 log.debug("Start handle request of {}", url);
193 User modifier = new User();
194 modifier.setUserId(userId);
195 log.debug("modifier id is {}", userId);
196 Wrapper<Response> responseWrapper = new Wrapper<>();
197 Wrapper<String> uuidWrapper = new Wrapper<>();
198 Wrapper<String> versionWrapper = new Wrapper<>();
199 Wrapper<User> userWrapper = new Wrapper<>();
200 Wrapper<ComponentTypeEnum> componentWrapper = new Wrapper<>();
202 validateUserExist(responseWrapper, userWrapper, userId);
204 if (responseWrapper.isEmpty()) {
205 validateComponentType(responseWrapper, componentWrapper, componentType);
208 if (responseWrapper.isEmpty()) {
209 fillUUIDAndVersion(responseWrapper, uuidWrapper, versionWrapper, userWrapper.getInnerElement(), componentWrapper.getInnerElement(), componentUniqueId, context);
212 if (responseWrapper.isEmpty()) {
213 Either<List<Map<String, Object>>, ResponseFormat> eitherServiceAudit = serviceBusinessLogic.getComponentAuditRecords(versionWrapper.getInnerElement(), uuidWrapper.getInnerElement(), userId);
215 if (eitherServiceAudit.isRight()) {
216 Response errorResponse = buildErrorResponse(eitherServiceAudit.right().value());
217 responseWrapper.setInnerElement(errorResponse);
219 List<Map<String, Object>> auditRecords = eitherServiceAudit.left().value();
220 Response okResponse = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), auditRecords);
221 responseWrapper.setInnerElement(okResponse);
226 } catch (Exception e) {
227 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Validate Service Name");
228 log.debug("get Service Audit Records failed with exception", e);
229 Response errorResponse = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
230 responseWrapper.setInnerElement(errorResponse);
232 return responseWrapper.getInnerElement();
235 private void fillUUIDAndVersion(Wrapper<Response> responseWrapper, Wrapper<String> uuidWrapper, Wrapper<String> versionWrapper, User user, final ComponentTypeEnum componentTypeEnum, final String componentUniqueId, ServletContext context) {
237 if (componentTypeEnum == ComponentTypeEnum.RESOURCE) {
238 Either<Resource, ResponseFormat> eitherResource = resourceBusinessLogic.getResource(componentUniqueId, user);
239 if (eitherResource.isLeft()) {
240 uuidWrapper.setInnerElement(eitherResource.left().value().getUUID());
241 versionWrapper.setInnerElement(eitherResource.left().value().getVersion());
243 responseWrapper.setInnerElement(buildErrorResponse(eitherResource.right().value()));
247 Either<Service, ResponseFormat> eitherService = serviceBusinessLogic.getService(componentUniqueId, user);
248 if (eitherService.isLeft()) {
249 uuidWrapper.setInnerElement(eitherService.left().value().getUUID());
250 versionWrapper.setInnerElement(eitherService.left().value().getVersion());
252 responseWrapper.setInnerElement(buildErrorResponse(eitherService.right().value()));
259 @Path("/services/{serviceId}")
260 public Response deleteService(@PathParam("serviceId") final String serviceId, @Context final HttpServletRequest request) {
261 String url = request.getMethod() + " " + request.getRequestURI();
262 log.debug("Start handle request of {}", url);
265 String userId = request.getHeader(Constants.USER_ID_HEADER);
266 User modifier = new User();
267 modifier.setUserId(userId);
268 log.debug("modifier id is {}", userId);
270 Response response = null;
273 String serviceIdLower = serviceId.toLowerCase();
274 ResponseFormat actionResponse = serviceBusinessLogic.deleteService(serviceIdLower, modifier);
276 if (actionResponse.getStatus() != HttpStatus.SC_NO_CONTENT) {
277 log.debug("failed to delete service");
278 response = buildErrorResponse(actionResponse);
281 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT), null);
284 } catch (Exception e) {
285 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Service");
286 log.debug("delete service failed with exception", e);
287 response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
294 @Path("/services/{serviceName}/{version}")
295 public Response deleteServiceByNameAndVersion(@PathParam("serviceName") final String serviceName, @PathParam("version") final String version, @Context final HttpServletRequest request) {
296 String url = request.getMethod() + " " + request.getRequestURI();
297 log.debug("Start handle request of {}", url);
300 String userId = request.getHeader(Constants.USER_ID_HEADER);
301 User modifier = new User();
302 modifier.setUserId(userId);
303 log.debug("modifier id is {}", userId);
305 Response response = null;
308 ResponseFormat actionResponse = serviceBusinessLogic.deleteServiceByNameAndVersion(serviceName, version, modifier);
310 if (actionResponse.getStatus() != HttpStatus.SC_NO_CONTENT) {
311 log.debug("failed to delete service");
312 response = buildErrorResponse(actionResponse);
315 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT), null);
318 } catch (Exception e) {
319 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Service");
320 log.debug("delete service failed with exception", e);
321 response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
328 @Path("/services/{serviceId}/metadata")
329 @Consumes(MediaType.APPLICATION_JSON)
330 @Produces(MediaType.APPLICATION_JSON)
331 @ApiOperation(value = "Update Service Metadata", httpMethod = "PUT", notes = "Returns updated service", response = Service.class)
332 @ApiResponses(value = { @ApiResponse(code = 200, message = "Service Updated"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
333 public Response updateServiceMetadata(@PathParam("serviceId") final String serviceId, @ApiParam(value = "Service object to be Updated", required = true) String data, @Context final HttpServletRequest request,
334 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
336 ServletContext context = request.getSession().getServletContext();
337 String url = request.getMethod() + " " + request.getRequestURI();
338 log.debug("Start handle request of {}", url);
340 User modifier = new User();
341 modifier.setUserId(userId);
342 log.debug("modifier id is {}", userId);
344 Response response = null;
347 String serviceIdLower = serviceId.toLowerCase();
349 Either<Service, ResponseFormat> convertResponse = parseToService(data, modifier);
350 if (convertResponse.isRight()) {
351 log.debug("failed to parse service");
352 response = buildErrorResponse(convertResponse.right().value());
355 Service updatedService = convertResponse.left().value();
356 Either<Service, ResponseFormat> actionResponse = serviceBusinessLogic.updateServiceMetadata(serviceIdLower, updatedService, modifier);
358 if (actionResponse.isRight()) {
359 log.debug("failed to update service");
360 response = buildErrorResponse(actionResponse.right().value());
364 Service service = actionResponse.left().value();
365 Object result = RepresentationUtils.toRepresentation(service);
366 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
368 } catch (Exception e) {
369 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Service Metadata");
370 log.debug("update service metadata failed with exception", e);
371 response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
377 * updates group instance property values
378 * Note, than in case of group instance updated successfully, related resourceInstance and containing component modification time will be updated
380 * @param componentInstanceId
381 * @param groupInstanceId
388 @Path("/{containerComponentType}/{serviceId}/resourceInstance/{componentInstanceId}/groupInstance/{groupInstanceId}")
389 @Consumes(MediaType.APPLICATION_JSON)
390 @Produces(MediaType.APPLICATION_JSON)
391 @ApiOperation(value = "Update Group Instance Property Values", httpMethod = "PUT", notes = "Returns updated group instance", response = Service.class)
392 @ApiResponses(value = { @ApiResponse(code = 200, message = "Group Instance Property Values Updated"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
393 public Response updateGroupInstancePropertyValues(@PathParam("serviceId") final String serviceId,@PathParam("componentInstanceId") final String componentInstanceId, @PathParam("groupInstanceId") final String groupInstanceId, @ApiParam(value = "Group instance object to be Updated", required = true) String data, @Context final HttpServletRequest request,
394 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
396 Response response = null;
397 String url = request.getMethod() + " " + request.getRequestURI();
398 log.debug("Start handle request of {}", url);
400 User modifier = new User();
401 modifier.setUserId(userId);
402 log.debug("modifier id is {}",userId);
404 Either<List<GroupInstanceProperty>, ResponseFormat> actionResponse = null;
406 List<GroupInstanceProperty> updatedProperties;
407 Type listType = new TypeToken<ArrayList<GroupInstanceProperty>>(){}.getType();
408 ArrayList<GroupInstanceProperty> newProperties = gson.fromJson(data, listType);
409 if (newProperties == null) {
410 actionResponse = Either.right(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
412 if(actionResponse == null){
413 log.debug("Start handle update group instance property values request. Received group instance is {}", groupInstanceId);
414 actionResponse = serviceBusinessLogic.updateGroupInstancePropertyValues(modifier, serviceId, componentInstanceId, groupInstanceId, newProperties);
415 if(actionResponse.isRight()){
416 actionResponse = Either.right(actionResponse.right().value());
419 if(actionResponse.isLeft()){
420 updatedProperties = actionResponse.left().value();
421 ObjectMapper mapper = new ObjectMapper();
422 String result = mapper.writeValueAsString(updatedProperties);
423 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
426 response = buildErrorResponse(actionResponse.right().value());
428 } catch (Exception e) {
429 log.error("Exception occured during update Group Instance property values: {}", e.getMessage(), e);
430 response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
436 @Path("/services/{serviceId}")
437 @Consumes(MediaType.APPLICATION_JSON)
438 @Produces(MediaType.APPLICATION_JSON)
439 @ApiOperation(value = "Retrieve Service", httpMethod = "GET", notes = "Returns service according to serviceId", response = Service.class)
440 @ApiResponses(value = { @ApiResponse(code = 200, message = "Service found"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Service not found") })
441 public Response getServiceById(@PathParam("serviceId") final String serviceId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
443 String url = request.getMethod() + " " + request.getRequestURI();
444 log.debug("Start handle request of {}", url);
447 User modifier = new User();
448 modifier.setUserId(userId);
449 log.debug("modifier id is {}", userId);
451 Response response = null;
453 String serviceIdLower = serviceId.toLowerCase();
454 log.debug("get service with id {}", serviceId);
455 Either<Service, ResponseFormat> actionResponse = serviceBusinessLogic.getService(serviceIdLower, modifier);
457 if (actionResponse.isRight()) {
458 log.debug("failed to get service");
459 response = buildErrorResponse(actionResponse.right().value());
463 Service service = actionResponse.left().value();
464 Object result = RepresentationUtils.toRepresentation(service);
466 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
468 } catch (Exception e) {
469 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Service");
470 log.debug("get service failed with exception", e);
471 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
477 @Path("/services/serviceName/{serviceName}/serviceVersion/{serviceVersion}")
478 @Consumes(MediaType.APPLICATION_JSON)
479 @Produces(MediaType.APPLICATION_JSON)
480 @ApiOperation(value = "Retrieve Service", httpMethod = "GET", notes = "Returns service according to name and version", response = Service.class)
481 @ApiResponses(value = { @ApiResponse(code = 200, message = "Service found"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Service not found") })
482 public Response getServiceByNameAndVersion(@PathParam("serviceName") final String serviceName, @PathParam("serviceVersion") final String serviceVersion, @Context final HttpServletRequest request,
483 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
486 User modifier = new User();
487 modifier.setUserId(userId);
488 log.debug("modifier id is {}", userId);
490 Response response = null;
492 Either<Service, ResponseFormat> actionResponse = serviceBusinessLogic.getServiceByNameAndVersion(serviceName, serviceVersion, userId);
494 if (actionResponse.isRight()) {
495 response = buildErrorResponse(actionResponse.right().value());
499 Service service = actionResponse.left().value();
500 Object result = RepresentationUtils.toRepresentation(service);
502 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
504 } catch (Exception e) {
505 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Service by name and version");
506 log.debug("get service failed with exception", e);
507 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
513 @Path("/services/{serviceId}/distribution-state/{state}")
514 @Consumes(MediaType.APPLICATION_JSON)
515 @Produces(MediaType.APPLICATION_JSON)
516 @ApiOperation(value = "Update Service Distribution State", httpMethod = "POST", notes = "service with the changed distribution status")
517 @ApiResponses(value = { @ApiResponse(code = 200, message = "Service distribution state changed"), @ApiResponse(code = 409, message = "Restricted operation"), @ApiResponse(code = 403, message = "Service is not available for distribution"),
518 @ApiResponse(code = 400, message = "Invalid content / Missing content"), @ApiResponse(code = 404, message = "Requested service was not found"), @ApiResponse(code = 500, message = "Internal Server Error. Please try again later.") })
519 public Response updateServiceDistributionState(@ApiParam(value = "DistributionChangeInfo - get comment out of body", required = true) LifecycleChangeInfoWithAction jsonChangeInfo, @PathParam("serviceId") final String serviceId,
520 @ApiParam(allowableValues = "approve, reject", required = true) @PathParam("state") final String state, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
522 String url = request.getMethod() + " " + request.getRequestURI();
523 log.debug("Start handle request of {}", url);
525 User modifier = new User();
526 modifier.setUserId(userId);
527 log.debug("modifier id is {}", userId);
529 Response response = null;
531 Either<Service, ResponseFormat> actionResponse = serviceBusinessLogic.changeServiceDistributionState(serviceId, state, jsonChangeInfo, modifier);
533 if (actionResponse.isRight()) {
534 log.debug("failed to Update Service Distribution State");
535 response = buildErrorResponse(actionResponse.right().value());
538 Service service = actionResponse.left().value();
539 Object result = RepresentationUtils.toRepresentation(service);
541 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
542 } catch (Exception e) {
543 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Service Distribution State");
544 log.debug("updateServiceDistributionState failed with exception", e);
545 response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
551 @Path("/services/{serviceId}/distribution/{env}/activate")
552 @Consumes(MediaType.APPLICATION_JSON)
553 @Produces(MediaType.APPLICATION_JSON)
554 @ApiOperation(value = "Activate distribution", httpMethod = "POST", notes = "activate distribution")
555 @ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 409, message = "Service cannot be distributed due to missing deployment artifacts"), @ApiResponse(code = 404, message = "Requested service was not found"),
556 @ApiResponse(code = 500, message = "Internal Server Error. Please try again later.") })
557 public Response activateDistribution(@PathParam("serviceId") final String serviceId, @PathParam("env") final String env, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
559 String url = request.getMethod() + " " + request.getRequestURI();
560 log.debug("Start handle request of {}", url);
562 User modifier = new User();
563 modifier.setUserId(userId);
564 log.debug("modifier id is {}", userId);
566 Response response = null;
568 Either<Service, ResponseFormat> distResponse = serviceBusinessLogic.activateDistribution(serviceId, env, modifier, request);
570 if (distResponse.isRight()) {
571 log.debug("failed to activate service distribution");
572 response = buildErrorResponse(distResponse.right().value());
575 Service service = distResponse.left().value();
576 Object result = RepresentationUtils.toRepresentation(service);
577 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
578 } catch (Exception e) {
579 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Activate Distribution");
580 log.debug("activate distribution failed with exception", e);
581 response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
587 @Path("/services/{serviceId}/distribution/{did}/markDeployed")
588 @Consumes(MediaType.APPLICATION_JSON)
589 @Produces(MediaType.APPLICATION_JSON)
590 @ApiOperation(value = "Mark distribution as deployed", httpMethod = "POST", notes = "relevant audit record will be created")
591 @ApiResponses(value = { @ApiResponse(code = 200, message = "Service was marked as deployed"), @ApiResponse(code = 409, message = "Restricted operation"), @ApiResponse(code = 403, message = "Service is not available"),
592 @ApiResponse(code = 400, message = "Invalid content / Missing content"), @ApiResponse(code = 404, message = "Requested service was not found"), @ApiResponse(code = 500, message = "Internal Server Error. Please try again later.") })
593 public Response markDistributionAsDeployed(@PathParam("serviceId") final String serviceId, @PathParam("did") final String did, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
595 String url = request.getMethod() + " " + request.getRequestURI();
596 log.debug("Start handle request of {}", url);
598 User modifier = new User();
599 modifier.setUserId(userId);
600 log.debug("modifier id is {}", userId);
602 Response response = null;
604 Either<Service, ResponseFormat> distResponse = serviceBusinessLogic.markDistributionAsDeployed(serviceId, did, modifier);
606 if (distResponse.isRight()) {
607 log.debug("failed to mark distribution as deployed");
608 response = buildErrorResponse(distResponse.right().value());
611 Service service = distResponse.left().value();
612 Object result = RepresentationUtils.toRepresentation(service);
613 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
614 } catch (Exception e) {
615 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Mark Distribution As Deployed");
616 log.debug("mark distribution as deployed failed with exception", e);
617 response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
623 @Path("/services/{serviceId}/tempUrlToBeDeleted")
624 @Consumes(MediaType.APPLICATION_JSON)
625 @Produces(MediaType.APPLICATION_JSON)
626 @ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 500, message = "Internal Server Error. Please try again later.") })
627 public Response tempUrlToBeDeleted(@PathParam("serviceId") final String serviceId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
629 ServletContext context = request.getSession().getServletContext();
630 String url = request.getMethod() + " " + request.getRequestURI();
631 log.debug("Start handle request of {}", url);
633 User modifier = new User();
634 modifier.setUserId(userId);
635 log.debug("modifier id is {}", userId);
639 Service service = (serviceBusinessLogic.getService(serviceId, modifier)).left().value();
640 Either<Service, ResponseFormat> res = serviceBusinessLogic.updateDistributionStatusForActivation(service, modifier, DistributionStatusEnum.DISTRIBUTED);
643 response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
645 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), null);
646 } catch (Exception e) {
647 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("tempUrlToBeDeleted");
648 log.debug("failed with exception", e);
649 response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
656 @Path("/services/{serviceId}/linksMap")
657 @Consumes(MediaType.APPLICATION_JSON)
658 @Produces(MediaType.APPLICATION_JSON)
659 @ApiOperation(value = "Retrieve Service component relations map", httpMethod = "GET", notes = "Returns service components relations", response = ServiceRelations.class)
660 @ApiResponses(value = { @ApiResponse(code = 200, message = "Service found"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Service not found") })
661 public Response getServiceComponentRelationMap(@PathParam("serviceId") final String serviceId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
663 String url = request.getMethod() + " " + request.getRequestURI();
664 log.debug("Start handle request of {}", url);
667 User modifier = new User();
668 modifier.setUserId(userId);
669 log.debug("modifier id is {}", userId);
671 Response response = null;
673 String serviceIdLower = serviceId.toLowerCase();
674 log.debug("get service components relations with id {}", serviceId);
675 Either<ServiceRelations, ResponseFormat> actionResponse = serviceBusinessLogic.getServiceComponentsRelations(serviceIdLower, modifier);
677 if (actionResponse.isRight()) {
678 log.debug("failed to get service relations data");
679 response = buildErrorResponse(actionResponse.right().value());
683 ServiceRelations serviceRelations = actionResponse.left().value();
684 Object result = RepresentationUtils.toRepresentation(serviceRelations);
686 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
688 } catch (Exception e) {
689 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Service");
690 log.debug("get service relations data failed with exception", e);
691 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));