ddc2b624a01a89cc4f5a5a01152d760393648456
[integration/csit.git] / plans / usecases-pnf-sw-upgrade / pnf-sw-upgrade / sorch / simulator / aai-simulator / src / main / java / org / onap / aaisimulator / controller / ServiceDesignAndCreationController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aaisimulator.controller;
21
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.springframework.beans.factory.annotation.Value;
25 import org.springframework.http.HttpStatus;
26 import org.springframework.http.MediaType;
27 import org.springframework.http.ResponseEntity;
28 import org.springframework.web.bind.annotation.GetMapping;
29 import org.springframework.web.bind.annotation.PathVariable;
30 import org.springframework.web.bind.annotation.RequestMapping;
31 import org.springframework.web.bind.annotation.RestController;
32
33 import java.io.IOException;
34 import java.nio.charset.StandardCharsets;
35 import java.nio.file.Files;
36 import java.nio.file.Path;
37 import java.nio.file.Paths;
38
39 import static org.onap.aaisimulator.utils.Constants.SERVICE_DESIGN_AND_CREATION_URL;
40
41 /**
42  * @author Eliezio Oliveira (eliezio.oliveira@est.tech)
43  */
44 @RestController
45 @RequestMapping(path = SERVICE_DESIGN_AND_CREATION_URL)
46 public class ServiceDesignAndCreationController {
47
48     private static final Logger LOGGER = LoggerFactory.getLogger(ServiceDesignAndCreationController.class);
49
50     @Value("${SERVICE_DESIGN_AND_CREATION_RESPONSES_LOCATION:./}")
51     private String responsesLocation;
52
53     @GetMapping(path = "/models/model/{model-invariant-id}/model-vers",
54             produces = MediaType.APPLICATION_XML_VALUE)
55     public ResponseEntity<String> getModelVers(@PathVariable("model-invariant-id") String modelInvariantId) {
56         Path responsesPath = Paths.get(responsesLocation).toAbsolutePath();
57         LOGGER.info("Will get ModelVer for 'model-invariant-id': {}, looking under {}",
58                 modelInvariantId, responsesPath.toString());
59
60         Path responsePath = responsesPath.resolve(modelInvariantId + ".xml");
61         if (!responsePath.toFile().exists()) {
62             LOGGER.error("{} not found", responsePath.toString());
63             return ResponseEntity.notFound().build();
64         }
65         try {
66             String content = new String(Files.readAllBytes(responsePath), StandardCharsets.UTF_8);
67             LOGGER.info("{} found with {} characters", responsePath.toString(), content.length());
68             return ResponseEntity.ok().body(content);
69         } catch (IOException e) {
70             LOGGER.error("Failed to read response from {}: {}}", responsePath.toString(), e.getMessage());
71             return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
72         }
73     }
74 }