Chore: Fix typo in INFO
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / servicecatalog / ServiceSpecificationResource.java
1 /**
2  *     Copyright (c) 2018 Orange
3  *
4  *     Licensed under the Apache License, Version 2.0 (the "License");
5  *     you may not use this file except in compliance with the License.
6  *     You may obtain a copy of the License at
7  *
8  *         http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *     Unless required by applicable law or agreed to in writing, software
11  *     distributed under the License is distributed on an "AS IS" BASIS,
12  *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *     See the License for the specific language governing permissions and
14  *     limitations under the License.
15  */
16
17 package org.onap.nbi.apis.servicecatalog;
18
19 import java.net.URI;
20 import java.util.ArrayList;
21 import java.util.LinkedHashMap;
22 import java.util.List;
23 import java.util.Map;
24 import org.onap.nbi.OnapComponentsUrlPaths;
25 import org.onap.nbi.apis.servicecatalog.model.ServiceSpecificationRequest;
26 import org.onap.nbi.commons.JsonRepresentation;
27 import org.onap.nbi.commons.ResourceManagement;
28 import org.onap.nbi.exceptions.ValidationException;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.http.MediaType;
31 import org.springframework.http.ResponseEntity;
32 import org.springframework.util.MultiValueMap;
33 import org.springframework.validation.BindingResult;
34 import org.springframework.validation.ObjectError;
35 import org.springframework.web.bind.annotation.*;
36 import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
37
38 import javax.validation.Valid;
39
40 @RestController
41 @RequestMapping(OnapComponentsUrlPaths.SERVICE_SPECIFICATION_PATH)
42 public class ServiceSpecificationResource extends ResourceManagement {
43
44     @Autowired
45     ServiceSpecificationService serviceSpecificationService;
46
47     @GetMapping(value = "/{serviceSpecId}", produces = MediaType.APPLICATION_JSON_VALUE)
48     public ResponseEntity<Object> getServiceSpecification(@PathVariable String serviceSpecId,
49                                                           @RequestParam MultiValueMap<String, String> params) {
50         Map response = serviceSpecificationService.get(serviceSpecId);
51
52         if (response != null) {
53             ArrayList<Map<String, Object>> resourseSpecificationMap= (ArrayList<Map<String, Object>>) response.get("resourceSpecification");
54             for (Map<String, Object> map : resourseSpecificationMap) {
55                 map.remove("childResourceSpecification");
56                 map.remove("serviceInstanceParams");
57                 map.remove("InstanceSpecification");
58             }
59             response.put("resourceSpecification", resourseSpecificationMap);
60         }
61
62         JsonRepresentation filter = new JsonRepresentation(params);
63         if (response.get("serviceSpecCharacteristic") != null) {
64             return this.getResponse(response, filter);
65         } else {
66             return this.getPartialResponse(response, filter);
67
68         }
69     }
70
71     @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
72     public ResponseEntity<Object> findServiceSpecification(@RequestParam MultiValueMap<String, String> params) {
73         List<LinkedHashMap> response = serviceSpecificationService.find(params);
74         JsonRepresentation filter = new JsonRepresentation(params);
75         return this.findResponse(response, filter, null);
76     }
77
78     @GetMapping(value = "/{serviceSpecId}/specificationInputSchema", produces = MediaType.APPLICATION_JSON_VALUE)
79     public ResponseEntity<Object> findSpecificationInputSchema(@PathVariable String serviceSpecId,
80                                                                @RequestParam MultiValueMap<String, String> params) {
81         String response = serviceSpecificationService.getInputSchema(serviceSpecId);
82         JsonRepresentation filter = new JsonRepresentation(params);
83         return this.getResponse(response, filter);
84     }
85
86     @PostMapping(value = "", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
87     public Object createServiceSpecification(@RequestHeader(value = "USER_ID", required = true) String userId,
88                                              @Valid @RequestBody ServiceSpecificationRequest serviceSpecificationRequest, BindingResult result) {
89         if (null == userId || userId.isEmpty()) {
90             result.addError(new ObjectError("USER_ID", "USER_ID is missing in header!"));
91         }
92         if (result.hasErrors()) {
93             throw new ValidationException(result.getAllErrors());
94         }
95         Map serviceCatalogResponse = serviceSpecificationService.create(userId, serviceSpecificationRequest);
96
97         return createResponse(serviceCatalogResponse);
98     }
99     /**
100      *
101      * @param resource
102      * @return
103      */
104     private ResponseEntity<Object> createResponse(final Map resource) {
105         URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(resource.get("id"))
106                 .toUri();
107         return ResponseEntity.created(location).body(resource);
108
109     }
110
111 }