adding catalog-service for mod2
[dcaegen2/platform.git] / mod2 / catalog-service / src / main / java / org / onap / dcaegen2 / platform / mod / web / controller / MicroserviceInstanceController.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.exceptions.msinstance.MsInstanceAlreadyExistsException;
24 import org.onap.dcaegen2.platform.mod.model.microserviceinstance.MsInstance;
25 import org.onap.dcaegen2.platform.mod.model.restapi.ErrorResponse;
26 import org.onap.dcaegen2.platform.mod.model.restapi.MsInstanceRequest;
27 import org.onap.dcaegen2.platform.mod.model.restapi.MsInstanceUpdateRequest;
28 import org.onap.dcaegen2.platform.mod.web.service.microserviceinstance.MsInstanceService;
29 import io.swagger.annotations.Api;
30 import io.swagger.annotations.ApiOperation;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.http.HttpStatus;
33 import org.springframework.http.ResponseEntity;
34 import org.springframework.web.bind.annotation.*;
35
36 import java.util.List;
37
38 /**
39  * Controller class to manage MicroserviceInstance's REST endpoints
40  */
41 @CrossOrigin
42 @RestController
43 @Api(tags = "Microservice Instance", description = "APIs to manage Microservice Instance")
44 @RequestMapping("/api/microservice-instance")
45 public class MicroserviceInstanceController {
46
47     @Autowired
48     MsInstanceService msInstanceService;
49
50     @GetMapping
51     @ApiOperation("Get all Microservices Instances")
52     public List<MsInstance> getAll() {
53         return msInstanceService.getAll();
54     }
55
56
57     @PostMapping("/{msName}")
58     @ApiOperation("Create a Microservice Instance")
59     @ResponseStatus(HttpStatus.CREATED)
60     public MsInstance createMsInstance(@PathVariable String msName, @RequestBody MsInstanceRequest request) {
61         return msInstanceService.createMicroserviceInstance(msName, request);
62     }
63
64     @PatchMapping("/{msId}")
65     @ApiOperation("Patch a Microservice Instance")
66     @ResponseStatus(HttpStatus.OK)
67     public MsInstance patchMsInstance(@RequestBody MsInstanceUpdateRequest request, @PathVariable String msId){
68         return msInstanceService.updateMsInstance(request, msId);
69     }
70
71     @ExceptionHandler
72     public ResponseEntity<ErrorResponse> resolveMsInstanceConflict(MsInstanceAlreadyExistsException ex) {
73         return new ResponseEntity<>(new ErrorResponse("Microservice Instance for the given name and release already exists"), HttpStatus.CONFLICT);
74     }
75 }