blob: a366073a4f0bb99e40f0e295d8beaad16f5f1815 [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;
Ray Milkey7a2dee52018-09-28 10:58:28 -070022import org.apache.karaf.shell.api.action.lifecycle.Service;
Thomas Vachuskaad37e372017-08-03 12:07:01 -070023import org.onosproject.app.ApplicationAdminService;
24import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.yang.YangLiveCompilerService;
27
28import java.io.IOException;
29import java.io.InputStream;
30import java.net.URL;
31
32/**
33 * Compiles the provided YANG source files and installs the resulting model extension.
34 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070035@Service
Thomas Vachuskaad37e372017-08-03 12:07:01 -070036@Command(scope = "onos", name = "compile-model",
37 description = "Compiles the provided YANG source files and installs the resulting model extension")
38public class YangCompileCommand extends AbstractShellCommand {
39
40 @Option(name = "-c", aliases = "--compile-only",
41 description = "Only compile, but do not install the model")
42 private boolean compileOnly = false;
43
44 @Option(name = "-f", aliases = "--force",
45 description = "Force reinstall if already installed")
46 private boolean forceReinstall = false;
47
48 @Argument(index = 0, name = "modelId",
49 description = "Model ID", required = true)
50 String modelId = null;
51
52 @Argument(index = 1, name = "url",
53 description = "URL to the YANG source file(s); .yang, .zip or .jar file",
54 required = true)
55 String url = null;
56
57 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070058 protected void doExecute() {
Thomas Vachuskaad37e372017-08-03 12:07:01 -070059 try {
60 InputStream yangJar = new URL(url).openStream();
61 YangLiveCompilerService compiler = get(YangLiveCompilerService.class);
62 if (compileOnly) {
63 ByteStreams.copy(compiler.compileYangFiles(modelId, yangJar), System.out);
64 } else {
65 ApplicationAdminService appService = get(ApplicationAdminService.class);
66
67 if (forceReinstall) {
68 ApplicationId appId = appService.getId(modelId);
69 if (appId != null && appService.getApplication(appId) != null) {
70 appService.uninstall(appId);
71 }
72 }
73
74 appService.install(compiler.compileYangFiles(modelId, yangJar));
75 appService.activate(appService.getId(modelId));
76 }
77 } catch (IOException e) {
78 e.printStackTrace();
79 }
80 }
81
82}