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.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;
47 public class RestController {
50 private CpService cpService;
53 * Upload a yang model file.
55 * @param uploadedFile the yang model file.
56 * @return a http response code.
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 {
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();
78 * @param uploadedFile the JSON file.
79 * @return a http response code.
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) {
87 validateJsonStructure(uploadedFile);
88 final int persistenceObjectId = cpService.storeJsonStructure(uploadedFile);
89 return Response.status(Status.OK).entity("Object stored in CPS with identity: " + persistenceObjectId)
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();
99 * Read a JSON Object using the object identifier.
101 * @param jsonObjectId the JSON object identifier.
102 * @return a HTTP response.
105 @Path("/json-object/{id}")
106 public final Response getJsonObjectById(@PathParam("id") int jsonObjectId) {
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();
117 * Delete a JSON Object using the object identifier.
119 * @param jsonObjectId the JSON object identifier.
120 * @return a HTTP response.
123 @Path("json-object/{id}")
124 public final Response deleteJsonObjectById(@PathParam("id") int jsonObjectId) {
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();
135 private static final void validateJsonStructure(final String jsonFile) {
136 final Gson gson = new Gson();
137 gson.fromJson(jsonFile, Object.class);
140 private static final File renameFileIfNeeded(File originalFile) {
141 if (originalFile.getName().endsWith(".yang")) {
144 final File renamedFile = new File(originalFile.getName() + ".yang");
145 originalFile.renameTo(renamedFile);