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

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;

public class CompatibilityRules {
    private final List<CompatibilityRule> compatibilityRules = new ArrayList<>();
    private static final CompatibilityRules instance = new CompatibilityRules();

    private CompatibilityRules() {

    }

    /**
     * add a compatibility rule.
     *
     * @param rule rule
     * @return true or false
     */
    public boolean addCompatibilityRule(CompatibilityRule rule) {
        if (null != getCompatibilityRule(rule.getRuleId())) {
            return false;
        }
        return compatibilityRules.add(rule);
    }

    /**
     * get compatibility rules.
     *
     * @return rules
     */
    public List<CompatibilityRule> getCompatibilityRules() {
        return compatibilityRules;
    }

    /**
     * get compatibility rule by specified rule id.
     *
     * @param ruleId rule id
     * @return rule
     */
    public CompatibilityRule getCompatibilityRule(String ruleId) {
        for (CompatibilityRule rule : compatibilityRules) {
            if (rule.getRuleId().equals(ruleId)) {
                return rule;
            }
        }
        return null;
    }

    /**
     * get instance of compatibility rules.
     *
     * @return rules instance.
     */
    public static CompatibilityRules getInstance() {
        return instance;
    }

    /**
     * deserialize from xml document.
     *
     * @param document xml document
     */
    public void deserialize(Document document) {
        compatibilityRules.clear();
        Element root = document.getRootElement();
        List<Element> children = root.elements("rule");
        for (Element ruleElement : children) {
            CompatibilityRule rule = CompatibilityRule.deserialize(ruleElement);
            addCompatibilityRule(rule);
        }
    }

    /**
     *
     * @param left       left statement
     * @param right      right statement
     * @return
     */
    private boolean matchCondition(CompatibilityRule left, CompatibilityRule.ChangeInfo right) {
        if (left.getCondition() == CompatibilityRule.ChangeInfo.ANY) {
            return true;
        } else if (left.getCondition() == CompatibilityRule.ChangeInfo.IGNORE) {
            return false;
        }
        if (left.getCondition() == right) {
            return true;
        }
        if (left.getCondition() == CompatibilityRule.ChangeInfo.CHANGED
                && (right == CompatibilityRule.ChangeInfo.EXPAND
                || right == CompatibilityRule.ChangeInfo.REDUCE
                || right == CompatibilityRule.ChangeInfo.SEQUENCE_CHANGED
                || right == CompatibilityRule.ChangeInfo.INTEGER_TYPE_CHANGED)) {
            if (left.getExceptConditions() != null && left.getExceptConditions().contains(right)) {
                return false;
            }
            return true;
        }
        return false;
    }

    /**
     * search rule by some arguments.
     *
     * @param statement  statement
     * @param parentStmt parent statement
     * @param changeInfo change info
     * @return matched rule
     */
    public CompatibilityRule searchRule(String statement, String parentStmt,
                                        CompatibilityRule.ChangeInfo changeInfo) {
        for (CompatibilityRule rule : compatibilityRules) {
            if (rule.getStatements().contains(statement)) {
                if (rule.getParentStmt() != null) {
                    if (parentStmt == null) {
                        continue;
                    }
                    if (!rule.getParentStmt().equals(parentStmt)) {
                        continue;
                    }
                }
                if (matchCondition(rule, changeInfo)) {
                    return rule;
                }
            }
        }
        return null;
    }
}