2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation
4 * Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.rest.controller;
23 import com.google.gson.Gson;
24 import com.google.gson.JsonSyntaxException;
26 import javax.persistence.PersistenceException;
27 import javax.validation.Valid;
28 import javax.ws.rs.Consumes;
29 import javax.ws.rs.DELETE;
30 import javax.ws.rs.GET;
31 import javax.ws.rs.POST;
32 import javax.ws.rs.Path;
33 import javax.ws.rs.PathParam;
34 import javax.ws.rs.Produces;
35 import javax.ws.rs.core.MediaType;
36 import javax.ws.rs.core.Response;
37 import javax.ws.rs.core.Response.Status;
38 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
39 import org.glassfish.jersey.media.multipart.FormDataParam;
40 import org.hibernate.exception.ConstraintViolationException;
41 import org.onap.cps.api.CpService;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.dao.EmptyResultDataAccessException;
48 public class RestController implements CpsResourceApi {
51 private CpService cpService;
54 public Object createAnchor(Attachment fileDetail, Integer dataspaceName) {
59 public Object createModules(Attachment fileDetail, Integer dataspaceName) {
64 public Object createNode(Attachment fileDetail, Integer dataspaceName) {
69 public Object deleteAnchor(Integer dataspaceName, Integer anchorName) {
74 public Object deleteDataspace(Integer dataspaceName) {
79 public Object getAnchor(Integer dataspaceName, Integer anchorName) {
84 public Object getAnchors(Integer dataspaceName) {
89 public Object getModule(Integer dataspaceName, Integer namespaceName, Integer revision) {
94 public Object getNode(@Valid String body, Integer dataspaceName) {
99 public Object getNodeByDataspaceAndAnchor(@Valid String body, Integer dataspaceName, Integer anchorpoint) {
104 Old rest endpoints before contract first approach (Need to be removed).
108 * Upload a yang model file.
110 * @param uploadedFile the yang model file.
111 * @param dataspaceName the dataspace name linked to the model.
112 * @return a http response code.
115 @Path("/dataspaces/{dataspace_name}/modules")
116 @Produces(MediaType.APPLICATION_JSON)
117 @Consumes(MediaType.MULTIPART_FORM_DATA)
118 public final Response uploadYangModelFile(@FormDataParam("file") File uploadedFile,
119 @PathParam("dataspace_name") String dataspaceName) {
121 final File fileToParse = renameFileIfNeeded(uploadedFile);
122 final SchemaContext schemaContext = cpService.parseAndValidateModel(fileToParse);
123 cpService.storeSchemaContext(schemaContext, dataspaceName);
124 return Response.status(Status.CREATED).entity("Resource successfully created").build();
125 } catch (final YangParserException | ConstraintViolationException e) {
126 return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
127 } catch (final Exception e) {
128 return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
133 * Upload a JSON file.
135 * @param uploadedFile the JSON file.
136 * @return a http response code.
139 @Path("/upload-yang-json-data-file")
140 @Produces(MediaType.APPLICATION_JSON)
141 @Consumes(MediaType.MULTIPART_FORM_DATA)
142 public final Response uploadYangJsonDataFile(@FormDataParam("file") String uploadedFile) {
144 validateJsonStructure(uploadedFile);
145 final int persistenceObjectId = cpService.storeJsonStructure(uploadedFile);
146 return Response.status(Status.OK).entity("Object stored in CPS with identity: " + persistenceObjectId)
148 } catch (final JsonSyntaxException e) {
149 return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
150 } catch (final Exception e) {
151 return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
156 * Read a JSON Object using the object identifier.
158 * @param jsonObjectId the JSON object identifier.
159 * @return a HTTP response.
162 @Path("/json-object/{id}")
163 public final Response getJsonObjectById(@PathParam("id") int jsonObjectId) {
165 return Response.status(Status.OK).entity(cpService.getJsonById(jsonObjectId)).build();
166 } catch (final PersistenceException e) {
167 return Response.status(Status.NOT_FOUND).entity(e.getMessage()).build();
168 } catch (final Exception e) {
169 return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
174 * Delete a JSON Object using the object identifier.
176 * @param jsonObjectId the JSON object identifier.
177 * @return a HTTP response.
180 @Path("json-object/{id}")
181 public final Response deleteJsonObjectById(@PathParam("id") int jsonObjectId) {
183 cpService.deleteJsonById(jsonObjectId);
184 return Response.status(Status.OK).entity(Status.OK.toString()).build();
185 } catch (final EmptyResultDataAccessException e) {
186 return Response.status(Status.NOT_FOUND).entity(Status.NOT_FOUND.toString()).build();
187 } catch (final Exception e) {
188 return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
192 private static final void validateJsonStructure(final String jsonFile) {
193 final Gson gson = new Gson();
194 gson.fromJson(jsonFile, Object.class);
197 private static final File renameFileIfNeeded(File originalFile) {
198 if (originalFile.getName().endsWith(".yang")) {
201 final File renamedFile = new File(originalFile.getName() + ".yang");
202 originalFile.renameTo(renamedFile);