Adjust test suite parser update to conform with API changes
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / type / StringTypeEffectiveStatementImpl.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 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
9
10 import javax.annotation.Nonnull;
11 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
12 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
13 import org.opendaylight.yangtools.yang.model.api.stmt.PatternEffectiveStatement;
14 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
16 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
17 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
18 import org.opendaylight.yangtools.yang.model.util.type.InvalidLengthConstraintException;
19 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
20 import org.opendaylight.yangtools.yang.model.util.type.StringTypeBuilder;
21 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.DeclaredEffectiveStatementBase;
22 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.length.LengthEffectiveStatementImpl;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 final class StringTypeEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, TypeStatement>
29         implements TypeEffectiveStatement<TypeStatement> {
30     private static final Logger LOG = LoggerFactory.getLogger(StringTypeEffectiveStatementImpl.class);
31     private final StringTypeDefinition typeDefinition;
32
33     StringTypeEffectiveStatementImpl(
34             final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
35             final StringTypeDefinition baseType) {
36         super(ctx);
37
38         final StringTypeBuilder builder = RestrictedTypes.newStringBuilder(baseType,
39             AbstractTypeStatementSupport.typeEffectiveSchemaPath(ctx));
40
41         for (EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
42             if (stmt instanceof LengthEffectiveStatementImpl) {
43                 final LengthEffectiveStatementImpl length = (LengthEffectiveStatementImpl)stmt;
44
45                 try {
46                     builder.setLengthConstraint(length, length.argument());
47                 } catch (IllegalStateException e) {
48                     throw new SourceException(ctx.getStatementSourceReference(), e,
49                             "Multiple length constraints encountered");
50                 } catch (InvalidLengthConstraintException e) {
51                     throw new SourceException(ctx.getStatementSourceReference(), e, "Invalid length constraint %s",
52                         length.argument());
53                 }
54             }
55             if (stmt instanceof PatternEffectiveStatement) {
56                 final PatternConstraint pattern = ((PatternEffectiveStatement)stmt).argument();
57                 if (pattern != null) {
58                     builder.addPatternConstraint(pattern);
59                 } else {
60                     LOG.debug("Ignoring empty pattern statement {}", stmt);
61                 }
62             }
63             if (stmt instanceof UnknownSchemaNode) {
64                 builder.addUnknownSchemaNode((UnknownSchemaNode)stmt);
65             }
66         }
67
68         typeDefinition = builder.build();
69     }
70
71     @Nonnull
72     @Override
73     public StringTypeDefinition getTypeDefinition() {
74         return typeDefinition;
75     }
76 }