Vidyashree Rama | 3c50361 | 2017-05-10 13:55:34 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Vidyashree Rama | 3c50361 | 2017-05-10 13:55:34 +0530 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.yang.web; |
| 18 | |
Thomas Vachuska | ad37e37 | 2017-08-03 12:07:01 -0700 | [diff] [blame] | 19 | import org.glassfish.jersey.media.multipart.FormDataParam; |
| 20 | import org.onosproject.app.ApplicationAdminService; |
Vidyashree Rama | 3c50361 | 2017-05-10 13:55:34 +0530 | [diff] [blame] | 21 | import org.onosproject.rest.AbstractWebResource; |
Thomas Vachuska | ad37e37 | 2017-08-03 12:07:01 -0700 | [diff] [blame] | 22 | import org.onosproject.yang.YangLiveCompilerService; |
Vidyashree Rama | 3c50361 | 2017-05-10 13:55:34 +0530 | [diff] [blame] | 23 | |
| 24 | import javax.ws.rs.Consumes; |
Thomas Vachuska | ad37e37 | 2017-08-03 12:07:01 -0700 | [diff] [blame] | 25 | import javax.ws.rs.DefaultValue; |
Vidyashree Rama | 3c50361 | 2017-05-10 13:55:34 +0530 | [diff] [blame] | 26 | import javax.ws.rs.POST; |
| 27 | import javax.ws.rs.Path; |
Thomas Vachuska | ad37e37 | 2017-08-03 12:07:01 -0700 | [diff] [blame] | 28 | import javax.ws.rs.QueryParam; |
Vidyashree Rama | 3c50361 | 2017-05-10 13:55:34 +0530 | [diff] [blame] | 29 | import javax.ws.rs.core.MediaType; |
| 30 | import javax.ws.rs.core.Response; |
Vidyashree Rama | 3c50361 | 2017-05-10 13:55:34 +0530 | [diff] [blame] | 31 | import java.io.IOException; |
| 32 | import java.io.InputStream; |
Vidyashree Rama | 3c50361 | 2017-05-10 13:55:34 +0530 | [diff] [blame] | 33 | |
Vidyashree Rama | 3c50361 | 2017-05-10 13:55:34 +0530 | [diff] [blame] | 34 | /** |
| 35 | * Yang files upload resource. |
| 36 | */ |
Vidyashree Rama | 0044923 | 2017-06-21 09:41:07 +0530 | [diff] [blame] | 37 | @Path("models") |
Vidyashree Rama | 3c50361 | 2017-05-10 13:55:34 +0530 | [diff] [blame] | 38 | public class YangWebResource extends AbstractWebResource { |
| 39 | private static final String YANG_FILE_EXTENSION = ".yang"; |
| 40 | private static final String SER_FILE_EXTENSION = ".ser"; |
Vidyashree Rama | 0044923 | 2017-06-21 09:41:07 +0530 | [diff] [blame] | 41 | private static final String JAR_FILE_EXTENSION = ".jar"; |
| 42 | private static final String ZIP_FILE_EXTENSION = ".zip"; |
Vidyashree Rama | 3c50361 | 2017-05-10 13:55:34 +0530 | [diff] [blame] | 43 | 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 Rama | 0044923 | 2017-06-21 09:41:07 +0530 | [diff] [blame] | 46 | private static final String YANG_RESOURCES = "target/yang/resources/"; |
Vidyashree Rama | 3c50361 | 2017-05-10 13:55:34 +0530 | [diff] [blame] | 47 | 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 Rama | 0044923 | 2017-06-21 09:41:07 +0530 | [diff] [blame] | 50 | private static final String SLASH = "/"; |
Vidyashree Rama | 3c50361 | 2017-05-10 13:55:34 +0530 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Compiles and registers the given yang files. |
| 54 | * |
Thomas Vachuska | ad37e37 | 2017-08-03 12:07:01 -0700 | [diff] [blame] | 55 | * @param modelId model identifier |
sonugupta-huawei | 87c8733 | 2017-10-11 12:09:59 +0530 | [diff] [blame^] | 56 | * @param stream YANG, ZIP or JAR file |
Thomas Vachuska | ad37e37 | 2017-08-03 12:07:01 -0700 | [diff] [blame] | 57 | * @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 Rama | 3c50361 | 2017-05-10 13:55:34 +0530 | [diff] [blame] | 70 | } |