Merge "Refactor, fix code formatting and add unittests"
[dcaegen2/platform.git] / mod2 / catalog-service / src / main / java / org / onap / dcaegen2 / platform / mod / web / controller / DeploymentArtifactController.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  org.onap.dcae
4  *  ================================================================================
5  *  Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *       http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dcaegen2.platform.mod.web.controller;
22
23 import org.onap.dcaegen2.platform.mod.model.deploymentartifact.DeploymentArtifact;
24 import org.onap.dcaegen2.platform.mod.model.deploymentartifact.DeploymentArtifactSearch;
25 import org.onap.dcaegen2.platform.mod.model.deploymentartifact.DeploymentArtifactStatus;
26 import org.onap.dcaegen2.platform.mod.model.exceptions.deploymentartifact.BlueprintFileNameCreateException;
27 import org.onap.dcaegen2.platform.mod.model.exceptions.deploymentartifact.StatusChangeNotValidException;
28 import org.onap.dcaegen2.platform.mod.model.restapi.DeploymentArtifactPatchRequest;
29 import org.onap.dcaegen2.platform.mod.model.restapi.ErrorResponse;
30 import org.onap.dcaegen2.platform.mod.model.restapi.SuccessResponse;
31 import org.onap.dcaegen2.platform.mod.web.service.deploymentartifact.DeploymentArtifactService;
32 import io.swagger.annotations.Api;
33 import lombok.extern.slf4j.Slf4j;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.http.HttpStatus;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.web.bind.annotation.*;
38
39 import java.util.Arrays;
40 import java.util.List;
41
42 import static org.onap.dcaegen2.platform.mod.web.controller.DeploymentArtifactController.DEPLOYMENT_ARTIFACTS_BASE_URL;
43
44 /**
45  * Controller class to manage DeploymentArtifact's REST endpoints
46  */
47 @RestController
48 @CrossOrigin
49 @RequestMapping(DEPLOYMENT_ARTIFACTS_BASE_URL)
50 @Slf4j
51 @Api(tags = "Deployment Artifact", value = "APIs to manage Deployment Artifacts")
52 public class DeploymentArtifactController {
53
54     public static final String DEPLOYMENT_ARTIFACTS_BASE_URL = "/api/deployment-artifact";
55
56     public static final String GET_STATUSES = "/statuses";
57
58     @Autowired
59     private DeploymentArtifactService service;
60
61     @PostMapping("/{msInstanceId}")
62     @ResponseStatus(HttpStatus.CREATED)
63     public DeploymentArtifact generateDeploymentArtifactForMSInstance(@PathVariable String  msInstanceId,
64                                                                       @RequestParam String user ){
65         return service.generateDeploymentArtifact(msInstanceId, user);
66     }
67
68     @GetMapping
69     @ResponseStatus(HttpStatus.OK)
70     public List<DeploymentArtifact> getAllDeploymentArtifacts(){
71         return service.getAllDeploymentArtifacts();
72     }
73
74     @PostMapping("/search")
75     @ResponseStatus(HttpStatus.OK)
76     public List<DeploymentArtifact> searchDeploymentArtifacts(@RequestBody DeploymentArtifactSearch searchRequest){
77         log.info("Search on deployment artifacts: {}", searchRequest);
78         return service.searchDeploymentArtifacts(searchRequest);
79     }
80
81     @GetMapping(GET_STATUSES)
82     @ResponseStatus(HttpStatus.OK)
83     public List<DeploymentArtifactStatus> getDeploymentArtifactStatuses(){
84         return Arrays.asList(DeploymentArtifactStatus.values());
85     }
86
87     @PatchMapping(value = "/{deploymentArtifactId}", params = {"user!="})
88     public ResponseEntity<SuccessResponse> patchDeploymentArtifact(@PathVariable("deploymentArtifactId") String id,
89                                                      @RequestBody DeploymentArtifactPatchRequest deploymentArtifactPatchRequest,
90                                                      @RequestParam("user") String user){
91         log.info("***Received request {} to update DeploymentArtifact id {} by {}", deploymentArtifactPatchRequest, id, user);
92         service.updateDeploymentArtifact(id, deploymentArtifactPatchRequest, user);
93         return new ResponseEntity<>(new SuccessResponse("Deployment Artifact was updated."),HttpStatus.OK);
94     }
95
96     @DeleteMapping( value = "/{deploymentArtifactId}", params = {"user!="})
97     public ResponseEntity<SuccessResponse> deleteDeploymentArtifact(@PathVariable("deploymentArtifactId") String id,
98                                                       @RequestParam("user") String user){
99         log.info("***Received request to delete DeploymentArtifact id {} by {}", id, user);
100         service.deleteDeploymentArtifact(id);
101         return new ResponseEntity<>(new SuccessResponse("Deployment Artifact was deleted"), HttpStatus.OK);
102     }
103
104     @ExceptionHandler
105     public ResponseEntity<ErrorResponse> resolveBaseMsServiceNameRegex(BlueprintFileNameCreateException ex) {
106         return new ResponseEntity<>(new ErrorResponse(ex.getMessage()),
107                 HttpStatus.BAD_REQUEST);
108     }
109
110     @ExceptionHandler
111     public ResponseEntity<ErrorResponse> resolveStatusValidationFailure(StatusChangeNotValidException ex) {
112         return new ResponseEntity<>(new ErrorResponse(ex.getMessage()),
113                 HttpStatus.BAD_REQUEST);
114     }
115 }