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