summaryrefslogtreecommitdiffstats
path: root/yang-comparator/src/main/java/org/onap/modeling/yangkit/comparator/app/YangComparatorPlugin.java
blob: a46b97de3077ce3c96a65dce880f4ca108818db9 (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
/*
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.comparator.app;

import com.google.gson.JsonElement;

import java.io.IOException;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.onap.modeling.yangkit.comparator.CompareType;
import org.onap.modeling.yangkit.comparator.YangComparator;
import org.onap.modeling.yangkit.comparator.YangCompareResult;
import org.onap.modeling.yangkit.compiler.BuildOption;
import org.onap.modeling.yangkit.compiler.Settings;
import org.onap.modeling.yangkit.compiler.Source;
import org.onap.modeling.yangkit.compiler.YangCompiler;
import org.onap.modeling.yangkit.compiler.YangCompilerException;
import org.onap.modeling.yangkit.compiler.plugin.YangCompilerPlugin;
import org.onap.modeling.yangkit.compiler.plugin.YangCompilerPluginParameter;
import org.onap.modeling.yangkit.compiler.util.YangCompilerUtil;
import org.yangcentral.yangkit.common.api.validate.ValidatorResult;


import org.yangcentral.yangkit.model.api.schema.YangSchemaContext;
import org.yangcentral.yangkit.utils.file.FileUtil;
import org.yangcentral.yangkit.utils.xml.XmlWriter;


/**
 * yang comparator plugin.
 */
public class YangComparatorPlugin implements YangCompilerPlugin {
    @Override
    public YangCompilerPluginParameter getParameter(String name, JsonElement value)
            throws YangCompilerException {
        if (!name.equals("old-yang") && !name.equals("settings")
                && !name.equals("compare-type") && !name.equals("rule")
                && !name.equals("result")) {
            throw new YangCompilerException("unrecognized parameter:" + name);
        }
        if (name.equals("old-yang") || name.equals("compare-type")) {
            YangCompilerPluginParameter yangCompilerPluginParameter = new YangCompilerPluginParameter() {
                @Override
                public String getName() {
                    return name;
                }

                @Override
                public Object getValue() throws YangCompilerException {
                    if (name.equals("old-yang")) {
                        return BuildOption.parseSources(value);
                    }

                    if (name.equals("compare-type")) {
                        if (value.equals("stmt")) {
                            return CompareType.STMT;
                        } else if (value.equals("tree")) {
                            return CompareType.TREE;
                        } else if (value.equals("compatible-check")) {
                            return CompareType.COMPATIBLE_CHECK;
                        }
                        throw new YangCompilerException("unrecognized value:" + value);
                    }
                    return null;
                }

            };
            return yangCompilerPluginParameter;
        }
        return YangCompilerPlugin.super.getParameter(name, value);

    }

    @Override
    public void run(YangSchemaContext yangSchemaContext, YangCompiler yangCompiler,
                    List<YangCompilerPluginParameter> list) throws YangCompilerException {
        CompareType compareType = null;
        List<Source> sources = null;
        Settings settings = yangCompiler.getSettings();
        String rulePath = null;
        String resultPath = null;
        for (YangCompilerPluginParameter parameter : list) {
            //System.out.println("para name="+parameter.getName() + " para value="+parameter.getValue());
            if (parameter.getName().equals("old-yang")) {
                sources = (List<Source>) parameter.getValue();
            } else if (parameter.getName().equals("settings")) {
                String settingsPath = (String) parameter.getValue();
                try {
                    settings = Settings.parse(FileUtil.readFile2String(settingsPath));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            } else if (parameter.getName().equals("compare-type")) {
                compareType = (CompareType) parameter.getValue();
            } else if (parameter.getName().equals("rule")) {
                rulePath = (String) parameter.getValue();
            } else if (parameter.getName().equals("result")) {
                resultPath = (String) parameter.getValue();
            }

        }
        if (sources == null) {
            throw new YangCompilerException("missing mandatory parameter:old-yang");
        }
        if (compareType == null) {
            throw new YangCompilerException("missing mandatory parameter:compare-type");
        }
        if (resultPath == null) {
            throw new YangCompilerException("missing mandatory parameter:result");
        }
        YangSchemaContext oldSchemaContext = YangCompilerUtil.buildSchemaContext(sources, settings);
        ValidatorResult oldResult = oldSchemaContext.validate();
        if (!oldResult.isOk()) {
            throw new YangCompilerException("fail to validate the schema context"
                    + ".\n"
                    + oldResult);
        }
        //System.out.println(oldSchemaContext.getValidateResult());
        YangComparator yangComparator = new YangComparator(oldSchemaContext, yangSchemaContext);
        try {
            List<YangCompareResult> results = yangComparator.compare(compareType, rulePath);
            boolean needCompatible = false;
            if (compareType == CompareType.COMPATIBLE_CHECK) {
                needCompatible = true;
            }
            Document document = yangComparator.outputXmlCompareResult(results, needCompatible, compareType);
            //System.out.println(XmlWriter.transDom4jDoc2String(document));
            XmlWriter.writeDom4jDoc(document, resultPath);
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        } catch (RuntimeException e) {
            for (StackTraceElement traceElement : e.getStackTrace()) {
                System.out.println(traceElement);
            }

        }

    }
}