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