2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia. 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.onap.pnfsimulator.rest;
23 import java.time.Instant;
24 import java.util.List;
25 import java.util.Optional;
26 import javax.validation.Valid;
28 import org.onap.pnfsimulator.db.Storage;
29 import org.onap.pnfsimulator.rest.model.TemplateRequest;
30 import org.onap.pnfsimulator.rest.model.SearchExp;
31 import org.onap.pnfsimulator.template.Template;
32 import org.onap.pnfsimulator.template.search.IllegalJsonValueException;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.http.HttpHeaders;
37 import org.springframework.http.HttpStatus;
38 import org.springframework.http.MediaType;
39 import org.springframework.http.ResponseEntity;
40 import org.springframework.web.bind.annotation.GetMapping;
41 import org.springframework.web.bind.annotation.PathVariable;
42 import org.springframework.web.bind.annotation.PostMapping;
43 import org.springframework.web.bind.annotation.RequestBody;
44 import org.springframework.web.bind.annotation.RequestMapping;
45 import org.springframework.web.bind.annotation.RequestParam;
46 import org.springframework.web.bind.annotation.RestController;
47 import org.springframework.web.server.ResponseStatusException;
51 @RequestMapping("/template")
52 public class TemplateController {
53 static final String TEMPLATE_NOT_FOUND_MSG = "A template with given name does not exist";
54 static final String CANNOT_OVERRIDE_TEMPLATE_MSG = "Cannot overwrite existing template. Use override=true to override";
55 private final Storage<Template> service;
56 private static final Logger LOG = LoggerFactory.getLogger(TemplateController.class);
59 public TemplateController(Storage<Template> service) {
60 this.service = service;
64 public ResponseEntity<?> list() {
65 return new ResponseEntity<>(service.getAll(), HttpStatus.OK);
68 @GetMapping("get/{templateName}")
69 public ResponseEntity<?> get(@PathVariable String templateName) {
70 Optional<Template> template = service.get(templateName);
71 if (!template.isPresent()) {
72 HttpHeaders headers = new HttpHeaders();
73 headers.setContentType(MediaType.TEXT_PLAIN);
74 return new ResponseEntity<>(TEMPLATE_NOT_FOUND_MSG, headers, HttpStatus.NOT_FOUND);
76 return new ResponseEntity<>(template, HttpStatus.OK);
79 @PostMapping("upload")
80 public ResponseEntity<?> upload(
81 @RequestBody @Valid TemplateRequest templateRequest,
82 @RequestParam(required = false) boolean override) {
84 HttpStatus status = HttpStatus.CREATED;
85 Template template = new Template(templateRequest.getName(), templateRequest.getTemplate(), Instant.now().getNano());
86 if (!service.tryPersistOrOverwrite(template, override)) {
87 status = HttpStatus.CONFLICT;
88 msg = CANNOT_OVERRIDE_TEMPLATE_MSG;
90 return new ResponseEntity<>(msg, status);
93 @PostMapping("search")
94 public ResponseEntity<?> searchByCriteria(@RequestBody SearchExp queryJson) {
96 List<String> templateNames = service.getIdsByContentCriteria(queryJson.getSearchExpr());
97 return new ResponseEntity<>(templateNames, HttpStatus.OK);
98 } catch (IllegalJsonValueException ex) {
99 throw new ResponseStatusException(HttpStatus.BAD_REQUEST, String.format("Try again with correct parameters. Cause: %s", ex.getMessage()), ex);