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