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
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.rest.controller;
23 import com.google.gson.Gson;
24 import com.google.gson.JsonSyntaxException;
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;
50 public class CpsRestController implements CpsRestApi {
53 private CpService cpService;
56 private ModelMapper modelMapper;
59 * Create a new anchor.
61 * @param anchor the anchor details object.
62 * @param dataspaceName the dataspace name.
63 * @return a ResponseEntity with the anchor name.
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);
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);
82 public ResponseEntity<Object> createNode(@Valid final MultipartFile multipartFile, final String dataspaceName) {
87 public ResponseEntity<Object> deleteAnchor(final String dataspaceName, final String anchorName) {
92 public ResponseEntity<Object> deleteDataspace(final String dataspaceName) {
97 public ResponseEntity<Object> getAnchor(final String dataspaceName, final String anchorName) {
102 public ResponseEntity<Object> getAnchors(final String dataspaceName) {
107 public ResponseEntity<Object> getModule(final String dataspaceName, @Valid final String namespaceName,
108 @Valid final String revision) {
113 public ResponseEntity<Object> getNode(final String dataspaceName) {
118 public ResponseEntity<Object> getNodeByDataspaceAndAnchor(final String dataspaceName, final String anchorName) {
123 Old rest endpoints before contract first approach (Need to be removed).
127 * Upload a JSON file.
129 * @param uploadedFile the JSON Multipart file.
130 * @return a ResponseEntity.
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);
141 * Read a JSON Object using the object identifier.
143 * @param jsonObjectId the JSON object identifier.
144 * @return a ResponseEntity.
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);
153 * Delete a JSON Object using the object identifier.
155 * @param jsonObjectId the JSON object identifier.
156 * @return a ResponseEntity.
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);
165 private static void validateJsonStructure(final MultipartFile multipartFile) {
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);
174 private static File saveToFile(final MultipartFile multipartFile) {
176 final File file = File.createTempFile("tempFile", ".yang");
178 try (OutputStream outputStream = new FileOutputStream(file)) {
179 outputStream.write(multipartFile.getBytes());
183 } catch (final IOException e) {
184 throw new CpsException(e);
188 private static String getJsonString(final MultipartFile multipartFile) {
190 return new String(multipartFile.getBytes());
191 } catch (final IOException e) {
192 throw new CpsException(e);