CSIT Support for ServiceLevel PNF Software Upgrade
[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) 2020 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 com.google.gson.Gson;
23 import com.google.gson.GsonBuilder;
24 import javax.ws.rs.core.MediaType;
25 import org.onap.aaisimulator.models.ServiceModelVersion;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.beans.factory.annotation.Value;
29 import org.springframework.http.HttpStatus;
30 import org.springframework.http.ResponseEntity;
31 import org.springframework.web.bind.annotation.GetMapping;
32 import org.springframework.web.bind.annotation.PathVariable;
33 import org.springframework.web.bind.annotation.RequestMapping;
34 import org.springframework.web.bind.annotation.RestController;
35
36 import java.io.IOException;
37 import java.nio.charset.StandardCharsets;
38 import java.nio.file.Files;
39 import java.nio.file.Path;
40 import java.nio.file.Paths;
41
42 import static org.onap.aaisimulator.utils.Constants.SERVICE_DESIGN_AND_CREATION_URL;
43
44 /**
45  * @author Eliezio Oliveira (eliezio.oliveira@est.tech)
46  */
47 @RestController
48 @RequestMapping(path = SERVICE_DESIGN_AND_CREATION_URL)
49 public class ServiceDesignAndCreationController {
50
51     private static final Logger LOGGER = LoggerFactory.getLogger(ServiceDesignAndCreationController.class);
52
53     @Value("${SERVICE_DESIGN_AND_CREATION_RESPONSES_LOCATION:./}")
54     private String responsesLocation;
55
56     @GetMapping(path = "/models/model/{model-invariant-id}/model-vers",  produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
57     public ResponseEntity<String> getModelVers(@PathVariable("model-invariant-id") String modelInvariantId) {
58         Path responsesPath = Paths.get(responsesLocation).toAbsolutePath();
59         LOGGER.info("Will get ModelVer for 'model-invariant-id': {}, looking under {}",
60                 modelInvariantId, responsesPath.toString());
61
62         Path responsePath = responsesPath.resolve(modelInvariantId + ".json");
63         if (!responsePath.toFile().exists()) {
64             LOGGER.error("{} not found", responsePath.toString());
65             return ResponseEntity.notFound().build();
66         }
67         try {
68             String content = new String(Files.readAllBytes(responsePath), StandardCharsets.UTF_8);
69             Gson gson = new GsonBuilder().setPrettyPrinting().create();
70             ServiceModelVersion json = gson.fromJson(content, ServiceModelVersion.class);
71             String jsonInString = gson.toJson(json);
72             LOGGER.info("{} found with {} characters", responsePath.toString(), content.length());
73             return ResponseEntity.ok().body(jsonInString);
74         } catch (IOException e) {
75             LOGGER.error("Failed to read response from {}: {}}", responsePath.toString(), e.getMessage());
76             return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
77         }
78     }
79 }