e86d329baad97f007e38eee845dba95f5a6543b2
[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 javax.persistence.PersistenceException;
27 import javax.ws.rs.Consumes;
28 import javax.ws.rs.DELETE;
29 import javax.ws.rs.GET;
30 import javax.ws.rs.POST;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.PathParam;
33 import javax.ws.rs.Produces;
34 import javax.ws.rs.core.MediaType;
35 import javax.ws.rs.core.Response;
36 import javax.ws.rs.core.Response.Status;
37 import org.glassfish.jersey.media.multipart.FormDataParam;
38 import org.onap.cps.api.CpService;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.dao.EmptyResultDataAccessException;
43
44
45
46 @Path("v1")
47 public class RestController {
48
49     @Autowired
50     private CpService cpService;
51
52     /**
53      * Upload a yang model file.
54      *
55      * @param uploadedFile the yang model file.
56      * @return a http response code.
57      */
58     @POST
59     @Path("/upload-yang-model-file")
60     @Produces(MediaType.APPLICATION_JSON)
61     @Consumes(MediaType.MULTIPART_FORM_DATA)
62     public final Response uploadYangModelFile(@FormDataParam("file") File uploadedFile) throws IOException {
63         try {
64             final File fileToParse = renameFileIfNeeded(uploadedFile);
65             final SchemaContext schemaContext = cpService.parseAndValidateModel(fileToParse);
66             cpService.storeSchemaContext(schemaContext);
67             return Response.status(Status.OK).entity("Yang File Parsed").build();
68         } catch (final YangParserException e) {
69             return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
70         } catch (final Exception e) {
71             return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
72         }
73     }
74
75     /**
76      * Upload a JSON file.
77      *
78      * @param uploadedFile the JSON file.
79      * @return a http response code.
80      */
81     @POST
82     @Path("/upload-yang-json-data-file")
83     @Produces(MediaType.APPLICATION_JSON)
84     @Consumes(MediaType.MULTIPART_FORM_DATA)
85     public final Response uploadYangJsonDataFile(@FormDataParam("file") String uploadedFile) {
86         try {
87             validateJsonStructure(uploadedFile);
88             final int persistenceObjectId = cpService.storeJsonStructure(uploadedFile);
89             return Response.status(Status.OK).entity("Object stored in CPS with identity: " + persistenceObjectId)
90                 .build();
91         } catch (final JsonSyntaxException e) {
92             return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
93         } catch (final Exception e) {
94             return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
95         }
96     }
97
98     /**
99      * Read a JSON Object using the object identifier.
100      *
101      * @param jsonObjectId the JSON object identifier.
102      * @return a HTTP response.
103      */
104     @GET
105     @Path("/json-object/{id}")
106     public final Response getJsonObjectById(@PathParam("id") int jsonObjectId) {
107         try {
108             return Response.status(Status.OK).entity(cpService.getJsonById(jsonObjectId)).build();
109         } catch (final PersistenceException e) {
110             return Response.status(Status.NOT_FOUND).entity(e.getMessage()).build();
111         } catch (final Exception e) {
112             return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
113         }
114     }
115
116     /**
117      * Delete a JSON Object using the object identifier.
118      *
119      * @param jsonObjectId the JSON object identifier.
120      * @return a HTTP response.
121      */
122     @DELETE
123     @Path("json-object/{id}")
124     public final Response deleteJsonObjectById(@PathParam("id") int jsonObjectId) {
125         try {
126             cpService.deleteJsonById(jsonObjectId);
127             return Response.status(Status.OK).entity(Status.OK.toString()).build();
128         } catch (final EmptyResultDataAccessException e) {
129             return Response.status(Status.NOT_FOUND).entity(Status.NOT_FOUND.toString()).build();
130         } catch (final Exception e) {
131             return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
132         }
133     }
134
135     private static final void validateJsonStructure(final String jsonFile) {
136         final Gson gson = new Gson();
137         gson.fromJson(jsonFile, Object.class);
138     }
139
140     private static final File renameFileIfNeeded(File originalFile) {
141         if (originalFile.getName().endsWith(".yang")) {
142             return originalFile;
143         }
144         final File renamedFile = new File(originalFile.getName() + ".yang");
145         originalFile.renameTo(renamedFile);
146         return renamedFile;
147     }
148 }