VSE: Create an anchor in the given dataspace
[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.modelmapper.ModelMapper;
31 import org.onap.cps.api.CpService;
32 import org.onap.cps.api.model.AnchorDetails;
33 import org.onap.cps.exceptions.CpsException;
34 import org.onap.cps.exceptions.CpsValidationException;
35 import org.onap.cps.rest.api.CpsRestApi;
36 import org.onap.cps.rest.model.Anchor;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.http.HttpStatus;
40 import org.springframework.http.ResponseEntity;
41 import org.springframework.web.bind.annotation.DeleteMapping;
42 import org.springframework.web.bind.annotation.GetMapping;
43 import org.springframework.web.bind.annotation.PathVariable;
44 import org.springframework.web.bind.annotation.PostMapping;
45 import org.springframework.web.bind.annotation.RequestParam;
46 import org.springframework.web.bind.annotation.RestController;
47 import org.springframework.web.multipart.MultipartFile;
48
49 @RestController
50 public class CpsRestController implements CpsRestApi {
51
52     @Autowired
53     private CpService cpService;
54
55     @Autowired
56     private ModelMapper modelMapper;
57
58     /**
59      * Create a new anchor.
60      *
61      * @param anchor the anchor details object.
62      * @param dataspaceName the dataspace name.
63      * @return a ResponseEntity with the anchor name.
64      */
65     @Override
66     public final ResponseEntity<String> createAnchor(@Valid Anchor anchor, String dataspaceName) {
67         final AnchorDetails anchorDetails = modelMapper.map(anchor, AnchorDetails.class);
68         anchorDetails.setDataspace(dataspaceName);
69         final String anchorName = cpService.createAnchor(anchorDetails);
70         return new ResponseEntity<String>(anchorName, HttpStatus.CREATED);
71     }
72
73     @Override
74     public ResponseEntity<Object> createModules(@Valid MultipartFile multipartFile, String dataspaceName) {
75         final File fileToParse = saveToFile(multipartFile);
76         final SchemaContext schemaContext = cpService.parseAndValidateModel(fileToParse);
77         cpService.storeSchemaContext(schemaContext, dataspaceName);
78         return new ResponseEntity<>("Resource successfully created", HttpStatus.CREATED);
79     }
80
81     @Override
82     public ResponseEntity<Object> createNode(@Valid MultipartFile multipartFile, String dataspaceName) {
83         return null;
84     }
85
86     @Override
87     public ResponseEntity<Object> deleteAnchor(String dataspaceName, String anchorName) {
88         return null;
89     }
90
91     @Override
92     public ResponseEntity<Object> deleteDataspace(String dataspaceName) {
93         return null;
94     }
95
96     @Override
97     public ResponseEntity<Object> getAnchor(String dataspaceName, String anchorName) {
98         return null;
99     }
100
101     @Override
102     public ResponseEntity<Object> getAnchors(String dataspaceName) {
103         return null;
104     }
105
106     @Override
107     public ResponseEntity<Object> getModule(String dataspaceName, @Valid String namespaceName, @Valid String revision) {
108         return null;
109     }
110
111     @Override
112     public ResponseEntity<Object> getNode(String dataspaceName) {
113         return null;
114     }
115
116     @Override
117     public ResponseEntity<Object> getNodeByDataspaceAndAnchor(String dataspaceName, String anchorName) {
118         return null;
119     }
120
121     /*
122     Old rest endpoints before contract first approach (Need to be removed).
123      */
124
125     /**
126      * Upload a JSON file.
127      *
128      * @param uploadedFile the JSON Multipart file.
129      * @return a ResponseEntity.
130      */
131     @PostMapping("/upload-yang-json-data-file")
132     public final ResponseEntity<String> uploadYangJsonDataFile(
133         @RequestParam("file") MultipartFile uploadedFile) {
134         validateJsonStructure(uploadedFile);
135         final int persistenceObjectId = cpService.storeJsonStructure(getJsonString(uploadedFile));
136         return new ResponseEntity<String>(
137             "Object stored in CPS with identity: " + persistenceObjectId, HttpStatus.OK);
138
139     }
140
141     /**
142      * Read a JSON Object using the object identifier.
143      *
144      * @param jsonObjectId the JSON object identifier.
145      * @return a ResponseEntity.
146      */
147     @GetMapping("/json-object/{id}")
148     public final ResponseEntity<String> getJsonObjectById(
149         @PathVariable("id") final int jsonObjectId) {
150         return new ResponseEntity<String>(cpService.getJsonById(jsonObjectId), HttpStatus.OK);
151     }
152
153     /**
154      * Delete a JSON Object using the object identifier.
155      *
156      * @param jsonObjectId the JSON object identifier.
157      * @return a ResponseEntity.
158      */
159     @DeleteMapping("json-object/{id}")
160     public final ResponseEntity<Object> deleteJsonObjectById(
161         @PathVariable("id") final int jsonObjectId) {
162         cpService.deleteJsonById(jsonObjectId);
163         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
164     }
165
166     private static void validateJsonStructure(final MultipartFile multipartFile) {
167         try {
168             final Gson gson = new Gson();
169             gson.fromJson(getJsonString(multipartFile), Object.class);
170         } catch (final JsonSyntaxException e) {
171             throw new CpsValidationException("Not a valid JSON file.", e);
172         }
173     }
174
175     private static File saveToFile(final MultipartFile multipartFile) {
176         try {
177             final File file = File.createTempFile("tempFile", ".yang");
178             file.deleteOnExit();
179             try (OutputStream outputStream = new FileOutputStream(file)) {
180                 outputStream.write(multipartFile.getBytes());
181             }
182             return file;
183
184         } catch (final IOException e) {
185             throw new CpsException(e);
186         }
187     }
188
189     private static String getJsonString(final MultipartFile multipartFile) {
190         try {
191             return new String(multipartFile.getBytes());
192         } catch (final IOException e) {
193             throw new CpsException(e);
194         }
195     }
196 }