blob: d293b324395bc28fb08b04c89db5e32413d86867 [file] [log] [blame]
Vidyashree Rama3c503612017-05-10 13:55:34 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Vidyashree Rama3c503612017-05-10 13:55:34 +05303 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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
17package org.onosproject.yang.web;
18
Thomas Vachuskaad37e372017-08-03 12:07:01 -070019import org.glassfish.jersey.media.multipart.FormDataParam;
20import org.onosproject.app.ApplicationAdminService;
Vidyashree Rama3c503612017-05-10 13:55:34 +053021import org.onosproject.rest.AbstractWebResource;
Thomas Vachuskaad37e372017-08-03 12:07:01 -070022import org.onosproject.yang.YangLiveCompilerService;
Vidyashree Rama3c503612017-05-10 13:55:34 +053023
24import javax.ws.rs.Consumes;
Thomas Vachuskaad37e372017-08-03 12:07:01 -070025import javax.ws.rs.DefaultValue;
Vidyashree Rama3c503612017-05-10 13:55:34 +053026import javax.ws.rs.POST;
27import javax.ws.rs.Path;
Thomas Vachuskaad37e372017-08-03 12:07:01 -070028import javax.ws.rs.QueryParam;
Vidyashree Rama3c503612017-05-10 13:55:34 +053029import javax.ws.rs.core.MediaType;
30import javax.ws.rs.core.Response;
Vidyashree Rama3c503612017-05-10 13:55:34 +053031import java.io.IOException;
32import java.io.InputStream;
Vidyashree Rama3c503612017-05-10 13:55:34 +053033
Vidyashree Rama3c503612017-05-10 13:55:34 +053034/**
35 * Yang files upload resource.
36 */
Vidyashree Rama00449232017-06-21 09:41:07 +053037@Path("models")
Vidyashree Rama3c503612017-05-10 13:55:34 +053038public class YangWebResource extends AbstractWebResource {
39 private static final String YANG_FILE_EXTENSION = ".yang";
40 private static final String SER_FILE_EXTENSION = ".ser";
Vidyashree Rama00449232017-06-21 09:41:07 +053041 private static final String JAR_FILE_EXTENSION = ".jar";
42 private static final String ZIP_FILE_EXTENSION = ".zip";
Vidyashree Rama3c503612017-05-10 13:55:34 +053043 private static final String REGISTER = "register";
44 private static final String UNREGISTER = "unregister";
45 private static final String CODE_GEN_DIR = "target/generated-sources/";
Vidyashree Rama00449232017-06-21 09:41:07 +053046 private static final String YANG_RESOURCES = "target/yang/resources/";
Vidyashree Rama3c503612017-05-10 13:55:34 +053047 private static final String SERIALIZED_FILE_NAME = "YangMetaData.ser";
48 private static final String UNKNOWN_KEY = "Key must be either register " +
49 "or unregister.";
Vidyashree Rama00449232017-06-21 09:41:07 +053050 private static final String SLASH = "/";
Vidyashree Rama3c503612017-05-10 13:55:34 +053051
52 /**
53 * Compiles and registers the given yang files.
54 *
Thomas Vachuskaad37e372017-08-03 12:07:01 -070055 * @param modelId model identifier
sonugupta-huawei87c87332017-10-11 12:09:59 +053056 * @param stream YANG, ZIP or JAR file
Thomas Vachuskaad37e372017-08-03 12:07:01 -070057 * @return 200 OK
58 * @throws IOException when fails to generate a file
59 */
60 @POST
61 @Consumes(MediaType.MULTIPART_FORM_DATA)
62 public Response upload(@QueryParam("modelId") @DefaultValue("org.onosproject.model.unknown") String modelId,
63 @FormDataParam("file") InputStream stream) throws IOException {
64 YangLiveCompilerService compiler = get(YangLiveCompilerService.class);
65 ApplicationAdminService appService = get(ApplicationAdminService.class);
66 appService.install(compiler.compileYangFiles(modelId, stream));
67 appService.activate(appService.getId(modelId));
68 return Response.ok().build();
69 }
Vidyashree Rama3c503612017-05-10 13:55:34 +053070}