2 * Copyright (c) 2018 Orange
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.nbi.apis.servicecatalog;
20 import java.util.ArrayList;
21 import java.util.LinkedHashMap;
22 import java.util.List;
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;
38 import javax.validation.Valid;
41 @RequestMapping(OnapComponentsUrlPaths.SERVICE_SPECIFICATION_PATH)
42 public class ServiceSpecificationResource extends ResourceManagement {
45 ServiceSpecificationService serviceSpecificationService;
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);
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");
59 response.put("resourceSpecification", resourseSpecificationMap);
62 JsonRepresentation filter = new JsonRepresentation(params);
63 if (response.get("serviceSpecCharacteristic") != null) {
64 return this.getResponse(response, filter);
66 return this.getPartialResponse(response, filter);
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);
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);
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!"));
92 if (result.hasErrors()) {
93 throw new ValidationException(result.getAllErrors());
95 Map serviceCatalogResponse = serviceSpecificationService.create(userId, serviceSpecificationRequest);
97 return createResponse(serviceCatalogResponse);
104 private ResponseEntity<Object> createResponse(final Map resource) {
105 URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(resource.get("id"))
107 return ResponseEntity.created(location).body(resource);