blob: 9b82399748abbadb87a3d84f7090ea9fdd48dec6 [file] [log] [blame]
Thomas Vachuskaad37e372017-08-03 12:07:01 -07001/*
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 */
16package org.onosproject.yang.impl;
17
18import com.google.common.io.ByteStreams;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070019import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.Option;
Thomas Vachuskaad37e372017-08-03 12:07:01 -070022import org.onosproject.app.ApplicationAdminService;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.core.ApplicationId;
25import org.onosproject.yang.YangLiveCompilerService;
26
27import java.io.IOException;
28import java.io.InputStream;
29import 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")
36public 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 Milkey86ad7bb2018-09-27 12:32:28 -070056 protected void doExecute() {
Thomas Vachuskaad37e372017-08-03 12:07:01 -070057 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}