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
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.cps.rest.controller;
22 import com.google.gson.Gson;
23 import com.google.gson.JsonSyntaxException;
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;
44 public class RestController {
47 private CpService cpService;
50 * Upload a yang model file.
52 * @param uploadedFile the yang model file.
53 * @return a http response code.
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 {
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();
75 * @param uploadedFile the JSON file.
76 * @return a http response code.
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) {
84 validateJsonStructure(uploadedFile);
85 final int persistenceObjectId = cpService.storeJsonStructure(uploadedFile);
86 return Response.status(Status.OK).entity("Object stored in CPS with identity: " + persistenceObjectId)
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();
96 * Read a JSON Object using the object identifier.
98 * @param jsonObjectId the JSON object identifier.
99 * @return a HTTP response.
102 @Path("json-object/{id}")
103 public final Response getJsonObjectById(@PathParam("id") int jsonObjectId) {
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();
113 private static final void validateJsonStructure(final String jsonFile) {
114 final Gson gson = new Gson();
115 gson.fromJson(jsonFile, Object.class);
118 private static final File renameFileIfNeeded(File originalFile) {
119 if (originalFile.getName().endsWith(".yang")) {
122 final File renamedFile = new File(originalFile.getName() + ".yang");
123 originalFile.renameTo(renamedFile);