Create Common Model objects in SPI
[cps.git] / cps-rest / src / main / java / org / onap / cps / rest / controller / CpsRestController.java
1 /*
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
9  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.rest.controller;
22
23 import com.google.gson.Gson;
24 import com.google.gson.JsonSyntaxException;
25 import java.io.File;
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;
49
50 @RestController
51 public class CpsRestController implements CpsRestApi {
52
53     @Autowired
54     private CpService cpService;
55
56     @Autowired
57     private CpsModuleService cpsModuleService;
58
59     @Autowired
60     private CpsAdminService cpsAdminService;
61
62     @Autowired
63     private ModelMapper modelMapper;
64
65     /**
66      * Create a new anchor.
67      *
68      * @param anchor        the anchor details object.
69      * @param dataspaceName the dataspace name.
70      * @return a ResponseEntity with the anchor name.
71      */
72     @Override
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);
79     }
80
81     @Override
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);
87     }
88
89     @Override
90     public ResponseEntity<Object> createNode(@Valid final MultipartFile multipartFile, final String dataspaceName) {
91         return null;
92     }
93
94     @Override
95     public ResponseEntity<Object> deleteAnchor(final String dataspaceName, final String anchorName) {
96         return null;
97     }
98
99     @Override
100     public ResponseEntity<Object> deleteDataspace(final String dataspaceName) {
101         return null;
102     }
103
104     @Override
105     public ResponseEntity<Object> getAnchor(final String dataspaceName, final String anchorName) {
106         return null;
107     }
108
109     @Override
110     public ResponseEntity<Object> getAnchors(final String dataspaceName) {
111         return null;
112     }
113
114     @Override
115     public ResponseEntity<Object> getModule(final String dataspaceName, @Valid final String namespaceName,
116         @Valid final String revision) {
117         return null;
118     }
119
120     @Override
121     public ResponseEntity<Object> getNode(final String dataspaceName) {
122         return null;
123     }
124
125     @Override
126     public ResponseEntity<Object> getNodeByDataspaceAndAnchor(final String dataspaceName, final String anchorName) {
127         return null;
128     }
129
130     /*
131     Old rest endpoints before contract first approach (Need to be removed).
132      */
133
134     /**
135      * Upload a JSON file.
136      *
137      * @param uploadedFile the JSON Multipart file.
138      * @return a ResponseEntity.
139      */
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);
146     }
147
148     /**
149      * Read a JSON Object using the object identifier.
150      *
151      * @param jsonObjectId the JSON object identifier.
152      * @return a ResponseEntity.
153      */
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);
158     }
159
160     /**
161      * Delete a JSON Object using the object identifier.
162      *
163      * @param jsonObjectId the JSON object identifier.
164      * @return a ResponseEntity.
165      */
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);
171     }
172
173     private static void validateJsonStructure(final MultipartFile multipartFile) {
174         try {
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);
179         }
180     }
181
182     private static File saveToFile(final MultipartFile multipartFile) {
183         try {
184             final File file = File.createTempFile("tempFile", ".yang");
185             file.deleteOnExit();
186             try (OutputStream outputStream = new FileOutputStream(file)) {
187                 outputStream.write(multipartFile.getBytes());
188             }
189             return file;
190
191         } catch (final IOException e) {
192             throw new CpsException(e);
193         }
194     }
195
196     private static String getJsonString(final MultipartFile multipartFile) {
197         try {
198             return new String(multipartFile.getBytes());
199         } catch (final IOException e) {
200             throw new CpsException(e);
201         }
202     }
203 }