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 Anchor anchor, 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 MultipartFile multipartFile, 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 MultipartFile multipartFile, String dataspaceName) {
87 public ResponseEntity<Object> deleteAnchor(String dataspaceName, String anchorName) {
92 public ResponseEntity<Object> deleteDataspace(String dataspaceName) {
97 public ResponseEntity<Object> getAnchor(String dataspaceName, String anchorName) {
102 public ResponseEntity<Object> getAnchors(String dataspaceName) {
107 public ResponseEntity<Object> getModule(String dataspaceName, @Valid String namespaceName, @Valid String revision) {
112 public ResponseEntity<Object> getNode(String dataspaceName) {
117 public ResponseEntity<Object> getNodeByDataspaceAndAnchor(String dataspaceName, String anchorName) {
122 Old rest endpoints before contract first approach (Need to be removed).
126 * Upload a JSON file.
128 * @param uploadedFile the JSON Multipart file.
129 * @return a ResponseEntity.
131 @PostMapping("/upload-yang-json-data-file")
132 public final ResponseEntity<String> uploadYangJsonDataFile(
133 @RequestParam("file") MultipartFile uploadedFile) {
134 validateJsonStructure(uploadedFile);
135 final int persistenceObjectId = cpService.storeJsonStructure(getJsonString(uploadedFile));
136 return new ResponseEntity<String>(
137 "Object stored in CPS with identity: " + persistenceObjectId, HttpStatus.OK);
142 * Read a JSON Object using the object identifier.
144 * @param jsonObjectId the JSON object identifier.
145 * @return a ResponseEntity.
147 @GetMapping("/json-object/{id}")
148 public final ResponseEntity<String> getJsonObjectById(
149 @PathVariable("id") final int jsonObjectId) {
150 return new ResponseEntity<String>(cpService.getJsonById(jsonObjectId), HttpStatus.OK);
154 * Delete a JSON Object using the object identifier.
156 * @param jsonObjectId the JSON object identifier.
157 * @return a ResponseEntity.
159 @DeleteMapping("json-object/{id}")
160 public final ResponseEntity<Object> deleteJsonObjectById(
161 @PathVariable("id") final int jsonObjectId) {
162 cpService.deleteJsonById(jsonObjectId);
163 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
166 private static void validateJsonStructure(final MultipartFile multipartFile) {
168 final Gson gson = new Gson();
169 gson.fromJson(getJsonString(multipartFile), Object.class);
170 } catch (final JsonSyntaxException e) {
171 throw new CpsValidationException("Not a valid JSON file.", e);
175 private static File saveToFile(final MultipartFile multipartFile) {
177 final File file = File.createTempFile("tempFile", ".yang");
179 try (OutputStream outputStream = new FileOutputStream(file)) {
180 outputStream.write(multipartFile.getBytes());
184 } catch (final IOException e) {
185 throw new CpsException(e);
189 private static String getJsonString(final MultipartFile multipartFile) {
191 return new String(multipartFile.getBytes());
192 } catch (final IOException e) {
193 throw new CpsException(e);