summaryrefslogtreecommitdiffstats
path: root/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/PluginInfo.java
blob: 628f72bedcbf1ff2261c95a3ba4c236c70274c07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
Copyright 2023 Huawei Technologies

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package org.onap.modeling.yangkit.compiler;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import org.onap.modeling.yangkit.compiler.plugin.YangCompilerPlugin;


@SuppressWarnings("ALL")
public class PluginInfo {
    private final String pluginName;

    private  URLClassLoader classLoader;
    private final YangCompilerPlugin plugin;

    private String description;
    private final List<PluginParameterInfo> parameters = new ArrayList<>();

    /**
     * the constructor.
     *
     * @param pluginName plugin name
     * @param plugin     plugin
     */
    public PluginInfo(String pluginName, YangCompilerPlugin plugin) {
        this.pluginName = pluginName;
        this.plugin = plugin;
    }

    /**
     * get the name of plugin.
     *
     * @return name
     */
    public String getPluginName() {
        return pluginName;
    }

    public URLClassLoader getClassLoader() {
        return classLoader;
    }

    public void setClassLoader(URLClassLoader classLoader) {
        this.classLoader = classLoader;
    }

    /**
     * get the plugin.
     *
     * @return plugin
     */
    public YangCompilerPlugin getPlugin() {
        return plugin;
    }

    /**
     * get the description.
     *
     * @return description
     */
    public String getDescription() {
        return description;
    }

    /**
     * set the description.
     *
     * @param description description string
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * get the parameters of the plugin.
     *
     * @return the parameters
     */
    public List<PluginParameterInfo> getParameters() {
        return parameters;
    }

    /**
     * add a parameter to plugin.
     *
     * @param parameter parameter
     */
    public void addParameter(PluginParameterInfo parameter) {
        parameters.add(parameter);
    }

    /**
     * parse plugin information from json.
     *
     * @param pluginFile  plugins.json File handler
     * @param jsonElement json element
     * @return plugin information
     */
    public static PluginInfo parse(File pluginFile, JsonElement jsonElement) {
        JsonObject jsonObject = jsonElement.getAsJsonObject();
        String pluginName = jsonObject.get("name").getAsString();
        URLClassLoader classLoader = null;
        String classPath = null;
        JsonElement classPathElement = jsonObject.get("class-path");
        if (classPathElement != null) {
            classPath = classPathElement.getAsString();
        }
        String className = jsonObject.get("class").getAsString();
        try {
            Class<? extends YangCompilerPlugin> pluginClass = null;
            if (classPath != null && !classPath.trim().isEmpty()) {
                Path path = Paths.get(classPath);
                File file;
                if (!path.isAbsolute()) {
                    file = new File(pluginFile.getParentFile(), classPath);
                } else {
                    file = new File(classPath);
                }
                if (!file.exists()) {
                    System.out.println("[ERROR]the class-path:" + file.getAbsolutePath() + " is not found.");
                    return null;
                }
                URL[] cp = {file.toURI().toURL()};
                classLoader = new URLClassLoader(cp);

                pluginClass = (Class<? extends YangCompilerPlugin>) classLoader.loadClass(className);

            } else {
                pluginClass = (Class<? extends YangCompilerPlugin>) Class.forName(className);
            }

            Constructor<? extends YangCompilerPlugin> constructor = pluginClass.getConstructor();
            YangCompilerPlugin yangCompilerPlugin = constructor.newInstance();
            PluginInfo pluginInfo = new PluginInfo(pluginName, yangCompilerPlugin);
            pluginInfo.setClassLoader(classLoader);
            if (jsonObject.get("description") != null) {
                String description = jsonObject.get("description").getAsString();
                pluginInfo.setDescription(description);
            }
            JsonElement parasElement = jsonObject.get("parameter");
            if (parasElement != null) {
                JsonArray paraArray = parasElement.getAsJsonArray();
                for (int i = 0; i < paraArray.size(); i++) {
                    JsonElement paraElement = paraArray.get(i);
                    JsonObject para = paraElement.getAsJsonObject();
                    String name = para.get("name").getAsString();
                    PluginParameterInfo parameter = new PluginParameterInfo(name);
                    if (para.get("description") != null) {
                        String description = para.get("description").getAsString();
                        parameter.setDescription(description);
                    }
                    pluginInfo.addParameter(parameter);
                }
            }
            return pluginInfo;
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String toString() {
        return "PluginInfo{"
                + "pluginName='"
                + pluginName
                + '\''
                + ", plugin="
                + plugin
                + ", description='"
                + description
                + '\''
                + ", parameters="
                + parameters
                + '}';
    }
}