b8b07a3e9a8ad16ae916e7139b4de28b2c9dcce1
[yangtools.git] / yang / yang-parser-rfc7950 / 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 org.eclipse.jdt.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.LengthEffectiveStatement;
14 import org.opendaylight.yangtools.yang.model.api.stmt.PatternEffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
17 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
18 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
19 import org.opendaylight.yangtools.yang.model.util.type.InvalidLengthConstraintException;
20 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
21 import org.opendaylight.yangtools.yang.model.util.type.StringTypeBuilder;
22 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.DeclaredEffectiveStatementBase;
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
32     private final @NonNull StringTypeDefinition typeDefinition;
33
34     StringTypeEffectiveStatementImpl(
35             final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
36             final StringTypeDefinition baseType) {
37         super(ctx);
38
39         final StringTypeBuilder builder = RestrictedTypes.newStringBuilder(baseType,
40             AbstractTypeStatementSupport.typeEffectiveSchemaPath(ctx));
41
42         for (EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
43             if (stmt instanceof LengthEffectiveStatement) {
44                 final LengthEffectiveStatement length = (LengthEffectiveStatement)stmt;
45
46                 try {
47                     builder.setLengthConstraint(length, length.argument());
48                 } catch (IllegalStateException e) {
49                     throw new SourceException(ctx.getStatementSourceReference(), e,
50                             "Multiple length constraints encountered");
51                 } catch (InvalidLengthConstraintException e) {
52                     throw new SourceException(ctx.getStatementSourceReference(), e, "Invalid length constraint %s",
53                         length.argument());
54                 }
55             }
56             if (stmt instanceof PatternEffectiveStatement) {
57                 final PatternConstraint pattern = ((PatternEffectiveStatement)stmt).argument();
58                 if (pattern != null) {
59                     builder.addPatternConstraint(pattern);
60                 } else {
61                     LOG.debug("Ignoring empty pattern statement {}", stmt);
62                 }
63             }
64             if (stmt instanceof UnknownSchemaNode) {
65                 builder.addUnknownSchemaNode((UnknownSchemaNode)stmt);
66             }
67         }
68
69         typeDefinition = builder.build();
70     }
71
72     @Override
73     public StringTypeDefinition getTypeDefinition() {
74         return typeDefinition;
75     }
76 }