Bug 3874: Support of yang modeled AnyXML - XML deserialization
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / spi / SubstatementValidator.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.yangtools.yang.parser.spi;
10
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.MapDifference;
13 import com.google.common.collect.Maps;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.Map;
18 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.InvalidSubstatementException;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.MissingSubstatementException;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
22 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
23 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
24 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
25
26 public final class SubstatementValidator {
27     private final Map<StatementDefinition, Cardinality> cardinalityMap;
28     private final StatementDefinition currentStatement;
29     private final SpecialCase specialCase;
30     public final static int MAX = Integer.MAX_VALUE;
31
32     private SubstatementValidator(Builder builder, SpecialCase specialCase) {
33         this.cardinalityMap = builder.cardinalityMap.build();
34         this.currentStatement = builder.currentStatement;
35         this.specialCase = specialCase;
36     }
37
38     public static Builder builder(StatementDefinition currentStatement) {
39         return new Builder(currentStatement);
40     }
41
42     public static class Builder {
43         private final ImmutableMap.Builder<StatementDefinition, Cardinality> cardinalityMap = ImmutableMap.builder();
44         private final StatementDefinition currentStatement;
45
46         private Builder(StatementDefinition currentStatement) {
47             this.currentStatement = currentStatement;
48         }
49
50         public Builder add(StatementDefinition d, int min, int max) {
51             this.cardinalityMap.put(d, new Cardinality(min, max));
52             return this;
53         }
54
55         public SubstatementValidator build() {
56             return new SubstatementValidator(this, SpecialCase.NULL);
57         }
58
59         public SubstatementValidator build(SpecialCase specialCase) {
60             return new SubstatementValidator(this, specialCase);
61         }
62     }
63
64     public void validate(final StmtContext<?, ?, ?> ctx) throws InvalidSubstatementException,
65             MissingSubstatementException {
66         final Map<StatementDefinition, Integer> stmtDefMap = new HashMap<>();
67         final Map<StatementDefinition, Integer> validatedMap = new HashMap<>();
68         final Collection<StatementContextBase<?, ?, ?>> substatementsInit = new ArrayList<>();
69         substatementsInit.addAll(ctx.declaredSubstatements());
70         substatementsInit.addAll(ctx.effectiveSubstatements());
71
72         for (StatementContextBase<?, ?, ?> stmtCtx : substatementsInit) {
73             final StatementDefinition definition = stmtCtx.getPublicDefinition();
74             if (!stmtDefMap.containsKey(definition)) {
75                 stmtDefMap.put(definition, 1);
76             } else {
77                 stmtDefMap.put(definition, stmtDefMap.get(definition) + 1);
78             }
79         }
80
81         if (stmtDefMap.isEmpty() && specialCase == SpecialCase.NOTNULL) {
82             throw new InvalidSubstatementException(String.format("%s must contain atleast 1 element. Error in module "
83                     + "%s (%s)", currentStatement, ctx.getRoot().getStatementArgument(),
84                     ctx.getFromNamespace(ModuleCtxToModuleQName.class, ctx.getRoot())),
85                     ctx.getStatementSourceReference());
86         }
87
88         for (Map.Entry entry : stmtDefMap.entrySet()) {
89             final StatementDefinition key = (StatementDefinition) entry.getKey();
90             if (!cardinalityMap.containsKey(key)) {
91                 if (ctx.getFromNamespace(ExtensionNamespace.class, key.getStatementName()) != null) {
92                     continue;
93                 }
94                 throw new InvalidSubstatementException(String.format("%s is not valid for %s. Error in module %s (%s)",
95                         key, currentStatement, ctx.getRoot().getStatementArgument(),
96                         ctx.getFromNamespace(ModuleCtxToModuleQName.class, ctx.getRoot())),
97                         ctx.getStatementSourceReference());
98             }
99             if (cardinalityMap.get(key).getMin() > (Integer) entry.getValue()) {
100                 throw new InvalidSubstatementException(String.format("Minimal count of %s for %s is %s, detected %s. "
101                         + "Error in module %s (%s)", key, currentStatement, cardinalityMap.get(key).getMin(),
102                         entry.getValue(), ctx.getRoot().getStatementArgument(),
103                         ctx.getFromNamespace(ModuleCtxToModuleQName.class, ctx.getRoot())),
104                         ctx.getStatementSourceReference());
105             }
106             if (cardinalityMap.get(key).getMax() < (Integer) entry.getValue()) {
107                 throw new InvalidSubstatementException(String.format("Maximal count of %s for %s is %s, detected %s. "
108                         + "Error in module %s (%s)", key, currentStatement, cardinalityMap.get(key).getMax(),
109                         entry.getValue(), ctx.getRoot().getStatementArgument(),
110                         ctx.getFromNamespace(ModuleCtxToModuleQName.class, ctx.getRoot())),
111                         ctx.getStatementSourceReference());
112             }
113             validatedMap.put(key, 1);
114         }
115
116         final MapDifference<StatementDefinition, Object> diff = Maps.difference(validatedMap, cardinalityMap);
117
118         for (Map.Entry entry : diff.entriesOnlyOnRight().entrySet()) {
119             final int min = ((Cardinality) entry.getValue()).getMin();
120             if (min > 0) {
121                 throw new MissingSubstatementException(String.format("%s is missing %s. Minimal count is %s. Error in"
122                         + "  module %s (%s)", currentStatement, entry.getKey(), min, ctx.getRoot()
123                         .getStatementArgument(), ctx.getFromNamespace(ModuleCtxToModuleQName.class, ctx.getRoot())),
124                         ctx.getStatementSourceReference());
125             }
126         }
127     }
128
129     private static class Cardinality {
130         private final int min;
131         private final int max;
132
133         private Cardinality(int min, int max) throws YangParseException {
134             if (min > max) {
135                 throw new IllegalArgumentException("Min can not be greater than max!");
136             }
137             if (min < 0) {
138                 throw new IllegalArgumentException("Min can not be les than 0!");
139             }
140             this.min = min;
141             this.max = max;
142         }
143
144         private int getMax() {
145             return max;
146         }
147
148         private int getMin() {
149             return min;
150         }
151     }
152
153     public enum SpecialCase {
154         NOTNULL,
155         NULL
156     }
157 }