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