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(@Valid String body, String dataspaceName) {
101 public ResponseEntity<Object> getNodeByDataspaceAndAnchor(@Valid String body, String dataspaceName,
107 Old rest endpoints before contract first approach (Need to be removed).
111 * Upload a JSON file.
113 * @param uploadedFile the JSON Multipart file.
114 * @return a ResponseEntity.
116 @PostMapping("/upload-yang-json-data-file")
117 public final ResponseEntity<String> uploadYangJsonDataFile(
118 @RequestParam("file") MultipartFile uploadedFile) {
119 validateJsonStructure(uploadedFile);
120 final int persistenceObjectId = cpService.storeJsonStructure(getJsonString(uploadedFile));
121 return new ResponseEntity<String>(
122 "Object stored in CPS with identity: " + persistenceObjectId, HttpStatus.OK);
127 * Read a JSON Object using the object identifier.
129 * @param jsonObjectId the JSON object identifier.
130 * @return a ResponseEntity.
132 @GetMapping("/json-object/{id}")
133 public final ResponseEntity<String> getJsonObjectById(
134 @PathVariable("id") final int jsonObjectId) {
135 return new ResponseEntity<String>(cpService.getJsonById(jsonObjectId), HttpStatus.OK);
139 * Delete a JSON Object using the object identifier.
141 * @param jsonObjectId the JSON object identifier.
142 * @return a ResponseEntity.
144 @DeleteMapping("json-object/{id}")
145 public final ResponseEntity<Object> deleteJsonObjectById(
146 @PathVariable("id") final int jsonObjectId) {
147 cpService.deleteJsonById(jsonObjectId);
148 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
151 private static void validateJsonStructure(final MultipartFile multipartFile) {
153 final Gson gson = new Gson();
154 gson.fromJson(getJsonString(multipartFile), Object.class);
155 } catch (JsonSyntaxException e) {
156 throw new CpsValidationException("Not a valid JSON file.", e);
160 private static File saveToFile(final MultipartFile multipartFile) {
162 final File file = File.createTempFile("tempFile", ".yang");
165 try (OutputStream outputStream = new FileOutputStream(file)) {
166 outputStream.write(multipartFile.getBytes());
170 } catch (IOException e) {
171 throw new CpsException(e);
175 private static String getJsonString(final MultipartFile multipartFile) {
177 return new String(multipartFile.getBytes());
178 } catch (IOException e) {
179 throw new CpsException(e);