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.CpsAdminService;
33 import org.onap.cps.api.CpsModuleService;
34 import org.onap.cps.exceptions.CpsException;
35 import org.onap.cps.exceptions.CpsValidationException;
36 import org.onap.cps.rest.api.CpsRestApi;
37 import org.onap.cps.spi.model.Anchor;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.http.HttpStatus;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.web.bind.annotation.DeleteMapping;
43 import org.springframework.web.bind.annotation.GetMapping;
44 import org.springframework.web.bind.annotation.PathVariable;
45 import org.springframework.web.bind.annotation.PostMapping;
46 import org.springframework.web.bind.annotation.RequestParam;
47 import org.springframework.web.bind.annotation.RestController;
48 import org.springframework.web.multipart.MultipartFile;
51 public class CpsRestController implements CpsRestApi {
54 private CpService cpService;
57 private CpsModuleService cpsModuleService;
60 private CpsAdminService cpsAdminService;
63 private ModelMapper modelMapper;
66 * Create a new anchor.
68 * @param anchor the anchor details object.
69 * @param dataspaceName the dataspace name.
70 * @return a ResponseEntity with the anchor name.
73 public ResponseEntity<String> createAnchor(final org.onap.cps.rest.model.@Valid Anchor anchor,
74 final String dataspaceName) {
75 final Anchor anchorDetails = modelMapper.map(anchor, Anchor.class);
76 anchorDetails.setDataspaceName(dataspaceName);
77 final String anchorName = cpsAdminService.createAnchor(anchorDetails);
78 return new ResponseEntity<String>(anchorName, HttpStatus.CREATED);
82 public ResponseEntity<Object> createModules(@Valid final MultipartFile multipartFile, final String dataspaceName) {
83 final File fileToParse = saveToFile(multipartFile);
84 final SchemaContext schemaContext = cpsModuleService.parseAndValidateModel(fileToParse);
85 cpsModuleService.storeSchemaContext(schemaContext, dataspaceName);
86 return new ResponseEntity<>("Resource successfully created", HttpStatus.CREATED);
90 public ResponseEntity<Object> createNode(@Valid final MultipartFile multipartFile, final String dataspaceName) {
95 public ResponseEntity<Object> deleteAnchor(final String dataspaceName, final String anchorName) {
100 public ResponseEntity<Object> deleteDataspace(final String dataspaceName) {
105 public ResponseEntity<Object> getAnchor(final String dataspaceName, final String anchorName) {
110 public ResponseEntity<Object> getAnchors(final String dataspaceName) {
115 public ResponseEntity<Object> getModule(final String dataspaceName, @Valid final String namespaceName,
116 @Valid final String revision) {
121 public ResponseEntity<Object> getNode(final String dataspaceName) {
126 public ResponseEntity<Object> getNodeByDataspaceAndAnchor(final String dataspaceName, final String anchorName) {
131 Old rest endpoints before contract first approach (Need to be removed).
135 * Upload a JSON file.
137 * @param uploadedFile the JSON Multipart file.
138 * @return a ResponseEntity.
140 @PostMapping("/upload-yang-json-data-file")
141 public final ResponseEntity<String> uploadYangJsonDataFile(@RequestParam("file") final MultipartFile uploadedFile) {
142 validateJsonStructure(uploadedFile);
143 final int persistenceObjectId = cpService.storeJsonStructure(getJsonString(uploadedFile));
144 return new ResponseEntity<>(
145 "Object stored in CPS with identity: " + persistenceObjectId, HttpStatus.OK);
149 * Read a JSON Object using the object identifier.
151 * @param jsonObjectId the JSON object identifier.
152 * @return a ResponseEntity.
154 @GetMapping("/json-object/{id}")
155 public final ResponseEntity<String> getJsonObjectById(
156 @PathVariable("id") final int jsonObjectId) {
157 return new ResponseEntity<String>(cpService.getJsonById(jsonObjectId), HttpStatus.OK);
161 * Delete a JSON Object using the object identifier.
163 * @param jsonObjectId the JSON object identifier.
164 * @return a ResponseEntity.
166 @DeleteMapping("json-object/{id}")
167 public final ResponseEntity<Object> deleteJsonObjectById(
168 @PathVariable("id") final int jsonObjectId) {
169 cpService.deleteJsonById(jsonObjectId);
170 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
173 private static void validateJsonStructure(final MultipartFile multipartFile) {
175 final Gson gson = new Gson();
176 gson.fromJson(getJsonString(multipartFile), Object.class);
177 } catch (final JsonSyntaxException e) {
178 throw new CpsValidationException("Not a valid JSON file.", e);
182 private static File saveToFile(final MultipartFile multipartFile) {
184 final File file = File.createTempFile("tempFile", ".yang");
186 try (OutputStream outputStream = new FileOutputStream(file)) {
187 outputStream.write(multipartFile.getBytes());
191 } catch (final IOException e) {
192 throw new CpsException(e);
196 private static String getJsonString(final MultipartFile multipartFile) {
198 return new String(multipartFile.getBytes());
199 } catch (final IOException e) {
200 throw new CpsException(e);