Thomas Vachuska | ad37e37 | 2017-08-03 12:07:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | package org.onosproject.yang.impl; |
| 17 | |
| 18 | import com.google.common.io.ByteStreams; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 19 | import org.apache.karaf.shell.api.action.Argument; |
| 20 | import org.apache.karaf.shell.api.action.Command; |
| 21 | import org.apache.karaf.shell.api.action.Option; |
Thomas Vachuska | ad37e37 | 2017-08-03 12:07:01 -0700 | [diff] [blame] | 22 | import org.onosproject.app.ApplicationAdminService; |
| 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.yang.YangLiveCompilerService; |
| 26 | |
| 27 | import java.io.IOException; |
| 28 | import java.io.InputStream; |
| 29 | import java.net.URL; |
| 30 | |
| 31 | /** |
| 32 | * Compiles the provided YANG source files and installs the resulting model extension. |
| 33 | */ |
| 34 | @Command(scope = "onos", name = "compile-model", |
| 35 | description = "Compiles the provided YANG source files and installs the resulting model extension") |
| 36 | public class YangCompileCommand extends AbstractShellCommand { |
| 37 | |
| 38 | @Option(name = "-c", aliases = "--compile-only", |
| 39 | description = "Only compile, but do not install the model") |
| 40 | private boolean compileOnly = false; |
| 41 | |
| 42 | @Option(name = "-f", aliases = "--force", |
| 43 | description = "Force reinstall if already installed") |
| 44 | private boolean forceReinstall = false; |
| 45 | |
| 46 | @Argument(index = 0, name = "modelId", |
| 47 | description = "Model ID", required = true) |
| 48 | String modelId = null; |
| 49 | |
| 50 | @Argument(index = 1, name = "url", |
| 51 | description = "URL to the YANG source file(s); .yang, .zip or .jar file", |
| 52 | required = true) |
| 53 | String url = null; |
| 54 | |
| 55 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 56 | protected void doExecute() { |
Thomas Vachuska | ad37e37 | 2017-08-03 12:07:01 -0700 | [diff] [blame] | 57 | try { |
| 58 | InputStream yangJar = new URL(url).openStream(); |
| 59 | YangLiveCompilerService compiler = get(YangLiveCompilerService.class); |
| 60 | if (compileOnly) { |
| 61 | ByteStreams.copy(compiler.compileYangFiles(modelId, yangJar), System.out); |
| 62 | } else { |
| 63 | ApplicationAdminService appService = get(ApplicationAdminService.class); |
| 64 | |
| 65 | if (forceReinstall) { |
| 66 | ApplicationId appId = appService.getId(modelId); |
| 67 | if (appId != null && appService.getApplication(appId) != null) { |
| 68 | appService.uninstall(appId); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | appService.install(compiler.compileYangFiles(modelId, yangJar)); |
| 73 | appService.activate(appService.getId(modelId)); |
| 74 | } |
| 75 | } catch (IOException e) { |
| 76 | e.printStackTrace(); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | } |