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 java.util.Collection;
30 import javax.validation.Valid;
31 import org.modelmapper.ModelMapper;
32 import org.onap.cps.api.CpService;
33 import org.onap.cps.api.CpsAdminService;
34 import org.onap.cps.api.CpsModuleService;
35 import org.onap.cps.rest.api.CpsRestApi;
36 import org.onap.cps.spi.exceptions.CpsException;
37 import org.onap.cps.spi.exceptions.DataValidationException;
38 import org.onap.cps.spi.model.Anchor;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.web.bind.annotation.DeleteMapping;
44 import org.springframework.web.bind.annotation.GetMapping;
45 import org.springframework.web.bind.annotation.PathVariable;
46 import org.springframework.web.bind.annotation.PostMapping;
47 import org.springframework.web.bind.annotation.RequestParam;
48 import org.springframework.web.bind.annotation.RestController;
49 import org.springframework.web.multipart.MultipartFile;
52 public class CpsRestController implements CpsRestApi {
55 private CpService cpService;
58 private CpsModuleService cpsModuleService;
61 private CpsAdminService cpsAdminService;
64 private ModelMapper modelMapper;
67 * Create a new anchor.
69 * @param anchor the anchor details object.
70 * @param dataspaceName the dataspace name.
71 * @return a ResponseEntity with the anchor name.
74 public ResponseEntity<String> createAnchor(final org.onap.cps.rest.model.@Valid Anchor anchor,
75 final String dataspaceName) {
76 final Anchor anchorDetails = modelMapper.map(anchor, Anchor.class);
77 anchorDetails.setDataspaceName(dataspaceName);
78 final String anchorName = cpsAdminService.createAnchor(anchorDetails);
79 return new ResponseEntity<>(anchorName, HttpStatus.CREATED);
83 public ResponseEntity<Object> createModules(@Valid final MultipartFile multipartFile, final String dataspaceName) {
84 final File fileToParse = saveToFile(multipartFile);
85 final SchemaContext schemaContext = cpsModuleService.parseAndValidateModel(fileToParse);
86 cpsModuleService.storeSchemaContext(schemaContext, dataspaceName);
87 return new ResponseEntity<>("Resource successfully created", HttpStatus.CREATED);
91 public ResponseEntity<Object> createNode(@Valid final MultipartFile multipartFile, final String dataspaceName) {
96 public ResponseEntity<Object> deleteAnchor(final String dataspaceName, final String anchorName) {
101 public ResponseEntity<Object> deleteDataspace(final String dataspaceName) {
106 public ResponseEntity<Object> getAnchor(final String dataspaceName, final String anchorName) {
112 public ResponseEntity<Object> getAnchors(final String dataspaceName) {
113 final Collection<Anchor> anchorDetails = cpsAdminService.getAnchors(dataspaceName);
114 return new ResponseEntity<>(anchorDetails, HttpStatus.OK);
118 public ResponseEntity<Object> getModule(final String dataspaceName, @Valid final String namespaceName,
119 @Valid final String revision) {
124 public ResponseEntity<Object> getNode(final String dataspaceName) {
129 public ResponseEntity<Object> getNodeByDataspaceAndAnchor(final String dataspaceName, final String anchorName) {
134 Old rest endpoints before contract first approach (Need to be removed).
138 * Upload a JSON file.
140 * @param uploadedFile the JSON Multipart file.
141 * @return a ResponseEntity.
143 @PostMapping("/upload-yang-json-data-file")
144 public final ResponseEntity<String> uploadYangJsonDataFile(@RequestParam("file") final MultipartFile uploadedFile) {
145 validateJsonStructure(uploadedFile);
146 final int persistenceObjectId = cpService.storeJsonStructure(getJsonString(uploadedFile));
147 return new ResponseEntity<>(
148 "Object stored in CPS with identity: " + persistenceObjectId, HttpStatus.OK);
152 * Read a JSON Object using the object identifier.
154 * @param jsonObjectId the JSON object identifier.
155 * @return a ResponseEntity.
157 @GetMapping("/json-object/{id}")
158 public final ResponseEntity<String> getJsonObjectById(
159 @PathVariable("id") final int jsonObjectId) {
160 return new ResponseEntity<>(cpService.getJsonById(jsonObjectId), HttpStatus.OK);
164 * Delete a JSON Object using the object identifier.
166 * @param jsonObjectId the JSON object identifier.
167 * @return a ResponseEntity.
169 @DeleteMapping("json-object/{id}")
170 public final ResponseEntity<Object> deleteJsonObjectById(
171 @PathVariable("id") final int jsonObjectId) {
172 cpService.deleteJsonById(jsonObjectId);
173 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
176 private static void validateJsonStructure(final MultipartFile multipartFile) {
178 final Gson gson = new Gson();
179 gson.fromJson(getJsonString(multipartFile), Object.class);
180 } catch (final JsonSyntaxException e) {
181 throw new DataValidationException("Not a valid JSON file.", e.getMessage(), e);
185 private static File saveToFile(final MultipartFile multipartFile) {
187 final File file = File.createTempFile("tempFile", ".yang");
189 try (final OutputStream outputStream = new FileOutputStream(file)) {
190 outputStream.write(multipartFile.getBytes());
194 } catch (final IOException e) {
195 throw new CpsException(e);
199 private static String getJsonString(final MultipartFile multipartFile) {
201 return new String(multipartFile.getBytes());
202 } catch (final IOException e) {
203 throw new CpsException(e);