Move cps files to root dir
[cps.git] / cps-rest / src / main / java / org / onap / cps / rest / controller / CpsRestController.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation
4  *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
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
21 package org.onap.cps.rest.controller;
22
23 import com.google.gson.Gson;
24 import com.google.gson.JsonSyntaxException;
25 import java.io.File;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.OutputStream;
29 import javax.validation.Valid;
30 import org.onap.cps.api.CpService;
31 import org.onap.cps.exceptions.CpsException;
32 import org.onap.cps.exceptions.CpsValidationException;
33 import org.onap.cps.rest.api.CpsRestApi;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.http.HttpStatus;
37 import org.springframework.http.ResponseEntity;
38 import org.springframework.web.bind.annotation.DeleteMapping;
39 import org.springframework.web.bind.annotation.GetMapping;
40 import org.springframework.web.bind.annotation.PathVariable;
41 import org.springframework.web.bind.annotation.PostMapping;
42 import org.springframework.web.bind.annotation.RequestParam;
43 import org.springframework.web.bind.annotation.RestController;
44 import org.springframework.web.multipart.MultipartFile;
45
46 @RestController
47 public class CpsRestController implements CpsRestApi {
48
49     @Autowired
50     private CpService cpService;
51
52     @Override
53     public ResponseEntity<Object> createAnchor(@Valid MultipartFile multipartFile, String dataspaceName) {
54         return null;
55     }
56
57     @Override
58     public ResponseEntity<Object> createModules(@Valid MultipartFile multipartFile, String dataspaceName) {
59         final File fileToParse = saveToFile(multipartFile);
60         final SchemaContext schemaContext = cpService.parseAndValidateModel(fileToParse);
61         cpService.storeSchemaContext(schemaContext, dataspaceName);
62         return new ResponseEntity<>("Resource successfully created", HttpStatus.CREATED);
63     }
64
65     @Override
66     public ResponseEntity<Object> createNode(@Valid MultipartFile multipartFile, String dataspaceName) {
67         return null;
68     }
69
70     @Override
71     public ResponseEntity<Object> deleteAnchor(String dataspaceName, String anchorName) {
72         return null;
73     }
74
75     @Override
76     public ResponseEntity<Object> deleteDataspace(String dataspaceName) {
77         return null;
78     }
79
80     @Override
81     public ResponseEntity<Object> getAnchor(String dataspaceName, String anchorName) {
82         return null;
83     }
84
85     @Override
86     public ResponseEntity<Object> getAnchors(String dataspaceName) {
87         return null;
88     }
89
90     @Override
91     public ResponseEntity<Object> getModule(String dataspaceName, @Valid String namespaceName, @Valid String revision) {
92         return null;
93     }
94
95     @Override
96     public ResponseEntity<Object> getNode(@Valid String body, String dataspaceName) {
97         return null;
98     }
99
100     @Override
101     public ResponseEntity<Object> getNodeByDataspaceAndAnchor(@Valid String body, String dataspaceName,
102         String anchorName) {
103         return null;
104     }
105
106     /*
107     Old rest endpoints before contract first approach (Need to be removed).
108      */
109
110     /**
111      * Upload a JSON file.
112      *
113      * @param uploadedFile the JSON Multipart file.
114      * @return a ResponseEntity.
115      */
116     @PostMapping("/upload-yang-json-data-file")
117     public final ResponseEntity<String> uploadYangJsonDataFile(
118         @RequestParam("file") MultipartFile uploadedFile) {
119         validateJsonStructure(uploadedFile);
120         final int persistenceObjectId = cpService.storeJsonStructure(getJsonString(uploadedFile));
121         return new ResponseEntity<String>(
122             "Object stored in CPS with identity: " + persistenceObjectId, HttpStatus.OK);
123
124     }
125
126     /**
127      * Read a JSON Object using the object identifier.
128      *
129      * @param jsonObjectId the JSON object identifier.
130      * @return a ResponseEntity.
131      */
132     @GetMapping("/json-object/{id}")
133     public final ResponseEntity<String> getJsonObjectById(
134         @PathVariable("id") final int jsonObjectId) {
135         return new ResponseEntity<String>(cpService.getJsonById(jsonObjectId), HttpStatus.OK);
136     }
137
138     /**
139      * Delete a JSON Object using the object identifier.
140      *
141      * @param jsonObjectId the JSON object identifier.
142      * @return a ResponseEntity.
143      */
144     @DeleteMapping("json-object/{id}")
145     public final ResponseEntity<Object> deleteJsonObjectById(
146         @PathVariable("id") final int jsonObjectId) {
147         cpService.deleteJsonById(jsonObjectId);
148         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
149     }
150
151     private static void validateJsonStructure(final MultipartFile multipartFile) {
152         try {
153             final Gson gson = new Gson();
154             gson.fromJson(getJsonString(multipartFile), Object.class);
155         } catch (JsonSyntaxException e) {
156             throw new CpsValidationException("Not a valid JSON file.", e);
157         }
158     }
159
160     private static File saveToFile(final MultipartFile multipartFile) {
161         try {
162             final File file = File.createTempFile("tempFile", ".yang");
163             file.deleteOnExit();
164
165             try (OutputStream outputStream = new FileOutputStream(file)) {
166                 outputStream.write(multipartFile.getBytes());
167             }
168             return file;
169
170         } catch (IOException e) {
171             throw new CpsException(e);
172         }
173     }
174
175     private static String getJsonString(final MultipartFile multipartFile) {
176         try {
177             return new String(multipartFile.getBytes());
178         } catch (IOException e) {
179             throw new CpsException(e);
180         }
181     }
182 }