Fix checkstyle violations for "final" keyword
[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 final Anchor anchor, final 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 final MultipartFile multipartFile, final 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 final MultipartFile multipartFile, final String dataspaceName) {
83         return null;
84     }
85
86     @Override
87     public ResponseEntity<Object> deleteAnchor(final String dataspaceName, final String anchorName) {
88         return null;
89     }
90
91     @Override
92     public ResponseEntity<Object> deleteDataspace(final String dataspaceName) {
93         return null;
94     }
95
96     @Override
97     public ResponseEntity<Object> getAnchor(final String dataspaceName, final String anchorName) {
98         return null;
99     }
100
101     @Override
102     public ResponseEntity<Object> getAnchors(final String dataspaceName) {
103         return null;
104     }
105
106     @Override
107     public ResponseEntity<Object> getModule(final String dataspaceName, @Valid final String namespaceName,
108             @Valid final String revision) {
109         return null;
110     }
111
112     @Override
113     public ResponseEntity<Object> getNode(final String dataspaceName) {
114         return null;
115     }
116
117     @Override
118     public ResponseEntity<Object> getNodeByDataspaceAndAnchor(final String dataspaceName, final String anchorName) {
119         return null;
120     }
121
122     /*
123     Old rest endpoints before contract first approach (Need to be removed).
124      */
125
126     /**
127      * Upload a JSON file.
128      *
129      * @param uploadedFile the JSON Multipart file.
130      * @return a ResponseEntity.
131      */
132     @PostMapping("/upload-yang-json-data-file")
133     public final ResponseEntity<String> uploadYangJsonDataFile(@RequestParam("file") final MultipartFile uploadedFile) {
134         validateJsonStructure(uploadedFile);
135         final int persistenceObjectId = cpService.storeJsonStructure(getJsonString(uploadedFile));
136         return new ResponseEntity<>(
137             "Object stored in CPS with identity: " + persistenceObjectId, HttpStatus.OK);
138     }
139
140     /**
141      * Read a JSON Object using the object identifier.
142      *
143      * @param jsonObjectId the JSON object identifier.
144      * @return a ResponseEntity.
145      */
146     @GetMapping("/json-object/{id}")
147     public final ResponseEntity<String> getJsonObjectById(
148         @PathVariable("id") final int jsonObjectId) {
149         return new ResponseEntity<String>(cpService.getJsonById(jsonObjectId), HttpStatus.OK);
150     }
151
152     /**
153      * Delete a JSON Object using the object identifier.
154      *
155      * @param jsonObjectId the JSON object identifier.
156      * @return a ResponseEntity.
157      */
158     @DeleteMapping("json-object/{id}")
159     public final ResponseEntity<Object> deleteJsonObjectById(
160         @PathVariable("id") final int jsonObjectId) {
161         cpService.deleteJsonById(jsonObjectId);
162         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
163     }
164
165     private static void validateJsonStructure(final MultipartFile multipartFile) {
166         try {
167             final Gson gson = new Gson();
168             gson.fromJson(getJsonString(multipartFile), Object.class);
169         } catch (final JsonSyntaxException e) {
170             throw new CpsValidationException("Not a valid JSON file.", e);
171         }
172     }
173
174     private static File saveToFile(final MultipartFile multipartFile) {
175         try {
176             final File file = File.createTempFile("tempFile", ".yang");
177             file.deleteOnExit();
178             try (OutputStream outputStream = new FileOutputStream(file)) {
179                 outputStream.write(multipartFile.getBytes());
180             }
181             return file;
182
183         } catch (final IOException e) {
184             throw new CpsException(e);
185         }
186     }
187
188     private static String getJsonString(final MultipartFile multipartFile) {
189         try {
190             return new String(multipartFile.getBytes());
191         } catch (final IOException e) {
192             throw new CpsException(e);
193         }
194     }
195 }