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.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;
47 public class CpsRestController implements CpsRestApi {
50 private CpService cpService;
53 public ResponseEntity<Object> createAnchor(@Valid MultipartFile multipartFile, String dataspaceName) {
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);
66 public ResponseEntity<Object> createNode(@Valid MultipartFile multipartFile, String dataspaceName) {
71 public ResponseEntity<Object> deleteAnchor(String dataspaceName, String anchorName) {
76 public ResponseEntity<Object> deleteDataspace(String dataspaceName) {
81 public ResponseEntity<Object> getAnchor(String dataspaceName, String anchorName) {
86 public ResponseEntity<Object> getAnchors(String dataspaceName) {
91 public ResponseEntity<Object> getModule(String dataspaceName, @Valid String namespaceName, @Valid String revision) {
96 public ResponseEntity<Object> getNode(String dataspaceName) {
101 public ResponseEntity<Object> getNodeByDataspaceAndAnchor(String dataspaceName, String anchorName) {
106 Old rest endpoints before contract first approach (Need to be removed).
110 * Upload a JSON file.
112 * @param uploadedFile the JSON Multipart file.
113 * @return a ResponseEntity.
115 @PostMapping("/upload-yang-json-data-file")
116 public final ResponseEntity<String> uploadYangJsonDataFile(
117 @RequestParam("file") MultipartFile uploadedFile) {
118 validateJsonStructure(uploadedFile);
119 final int persistenceObjectId = cpService.storeJsonStructure(getJsonString(uploadedFile));
120 return new ResponseEntity<String>(
121 "Object stored in CPS with identity: " + persistenceObjectId, HttpStatus.OK);
126 * Read a JSON Object using the object identifier.
128 * @param jsonObjectId the JSON object identifier.
129 * @return a ResponseEntity.
131 @GetMapping("/json-object/{id}")
132 public final ResponseEntity<String> getJsonObjectById(
133 @PathVariable("id") final int jsonObjectId) {
134 return new ResponseEntity<String>(cpService.getJsonById(jsonObjectId), HttpStatus.OK);
138 * Delete a JSON Object using the object identifier.
140 * @param jsonObjectId the JSON object identifier.
141 * @return a ResponseEntity.
143 @DeleteMapping("json-object/{id}")
144 public final ResponseEntity<Object> deleteJsonObjectById(
145 @PathVariable("id") final int jsonObjectId) {
146 cpService.deleteJsonById(jsonObjectId);
147 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
150 private static void validateJsonStructure(final MultipartFile multipartFile) {
152 final Gson gson = new Gson();
153 gson.fromJson(getJsonString(multipartFile), Object.class);
154 } catch (JsonSyntaxException e) {
155 throw new CpsValidationException("Not a valid JSON file.", e);
159 private static File saveToFile(final MultipartFile multipartFile) {
161 final File file = File.createTempFile("tempFile", ".yang");
164 try (OutputStream outputStream = new FileOutputStream(file)) {
165 outputStream.write(multipartFile.getBytes());
169 } catch (IOException e) {
170 throw new CpsException(e);
174 private static String getJsonString(final MultipartFile multipartFile) {
176 return new String(multipartFile.getBytes());
177 } catch (IOException e) {
178 throw new CpsException(e);