Adding swagger codegen
[cps.git] / cps / cps-rest / src / main / java / org / onap / cps / rest / controller / RestController.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.cps.rest.controller;
21
22 import com.google.gson.Gson;
23 import com.google.gson.JsonSyntaxException;
24 import java.io.File;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import javax.persistence.PersistenceException;
28 import javax.validation.Valid;
29 import javax.ws.rs.Consumes;
30 import javax.ws.rs.DELETE;
31 import javax.ws.rs.GET;
32 import javax.ws.rs.POST;
33 import javax.ws.rs.Path;
34 import javax.ws.rs.PathParam;
35 import javax.ws.rs.Produces;
36 import javax.ws.rs.core.MediaType;
37 import javax.ws.rs.core.Response;
38 import javax.ws.rs.core.Response.Status;
39 import javax.ws.rs.core.SecurityContext;
40 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
41 import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
42 import org.glassfish.jersey.media.multipart.FormDataParam;
43 import org.onap.cps.api.CpService;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
45 import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.dao.EmptyResultDataAccessException;
48
49 @Path("cps")
50 public class RestController implements CpsResourceApi {
51
52     @Autowired
53     private CpService cpService;
54
55     @Override
56     public Object createAnchor(Attachment fileDetail, Integer dataspaceName) {
57         return null;
58     }
59
60     @Override
61     public Object createModules(Attachment fileDetail, Integer dataspaceName) {
62         return null;
63     }
64
65     @Override
66     public Object createNode(Attachment fileDetail, Integer dataspaceName) {
67         return null;
68     }
69
70     @Override
71     public Object deleteAnchor(Integer dataspaceName, Integer anchorName) {
72         return null;
73     }
74
75     @Override
76     public Object deleteDataspace(Integer dataspaceName) {
77         return null;
78     }
79
80     @Override
81     public Object getAnchor(Integer dataspaceName, Integer anchorName) {
82         return null;
83     }
84
85     @Override
86     public Object getAnchors(Integer dataspaceName) {
87         return null;
88     }
89
90     @Override
91     public Object getModule(Integer dataspaceName, Integer namespaceName, Integer revision) {
92         return null;
93     }
94
95     @Override
96     public Object getNode(@Valid String body, Integer dataspaceName) {
97         return null;
98     }
99
100     @Override
101     public Object getNodeByDataspaceAndAnchor(@Valid String body, Integer dataspaceName, Integer anchorpoint) {
102         return null;
103     }
104
105     /*
106     Old rest endpoints before contract first approach (Need to be removed).
107      */
108
109     /**
110      * Upload a yang model file.
111      *
112      * @param uploadedFile the yang model file.
113      * @return a http response code.
114      */
115     @POST
116     @Path("/upload-yang-model-file")
117     @Produces(MediaType.APPLICATION_JSON)
118     @Consumes(MediaType.MULTIPART_FORM_DATA)
119     public final Response uploadYangModelFile(@FormDataParam("file") File uploadedFile) throws IOException {
120         try {
121             final File fileToParse = renameFileIfNeeded(uploadedFile);
122             final SchemaContext schemaContext = cpService.parseAndValidateModel(fileToParse);
123             cpService.storeSchemaContext(schemaContext);
124             return Response.status(Status.OK).entity("Yang File Parsed").build();
125         } catch (final YangParserException e) {
126             return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
127         } catch (final Exception e) {
128             return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
129         }
130     }
131
132     /**
133      * Upload a JSON file.
134      *
135      * @param uploadedFile the JSON file.
136      * @return a http response code.
137      */
138     @POST
139     @Path("/upload-yang-json-data-file")
140     @Produces(MediaType.APPLICATION_JSON)
141     @Consumes(MediaType.MULTIPART_FORM_DATA)
142     public final Response uploadYangJsonDataFile(@FormDataParam("file") String uploadedFile) {
143         try {
144             validateJsonStructure(uploadedFile);
145             final int persistenceObjectId = cpService.storeJsonStructure(uploadedFile);
146             return Response.status(Status.OK).entity("Object stored in CPS with identity: " + persistenceObjectId)
147                 .build();
148         } catch (final JsonSyntaxException e) {
149             return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
150         } catch (final Exception e) {
151             return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
152         }
153     }
154
155     /**
156      * Read a JSON Object using the object identifier.
157      *
158      * @param jsonObjectId the JSON object identifier.
159      * @return a HTTP response.
160      */
161     @GET
162     @Path("/json-object/{id}")
163     public final Response getJsonObjectById(@PathParam("id") int jsonObjectId) {
164         try {
165             return Response.status(Status.OK).entity(cpService.getJsonById(jsonObjectId)).build();
166         } catch (final PersistenceException e) {
167             return Response.status(Status.NOT_FOUND).entity(e.getMessage()).build();
168         } catch (final Exception e) {
169             return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
170         }
171     }
172
173     /**
174      * Delete a JSON Object using the object identifier.
175      *
176      * @param jsonObjectId the JSON object identifier.
177      * @return a HTTP response.
178      */
179     @DELETE
180     @Path("json-object/{id}")
181     public final Response deleteJsonObjectById(@PathParam("id") int jsonObjectId) {
182         try {
183             cpService.deleteJsonById(jsonObjectId);
184             return Response.status(Status.OK).entity(Status.OK.toString()).build();
185         } catch (final EmptyResultDataAccessException e) {
186             return Response.status(Status.NOT_FOUND).entity(Status.NOT_FOUND.toString()).build();
187         } catch (final Exception e) {
188             return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
189         }
190     }
191
192     private static final void validateJsonStructure(final String jsonFile) {
193         final Gson gson = new Gson();
194         gson.fromJson(jsonFile, Object.class);
195     }
196
197     private static final File renameFileIfNeeded(File originalFile) {
198         if (originalFile.getName().endsWith(".yang")) {
199             return originalFile;
200         }
201         final File renamedFile = new File(originalFile.getName() + ".yang");
202         originalFile.renameTo(renamedFile);
203         return renamedFile;
204     }
205 }