Do not build unrecognized extension statements
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / namespace / YangNamespaceContextNamespace.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.namespace;
9
10 import static com.google.common.base.Verify.verify;
11
12 import com.google.common.annotations.Beta;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yangtools.yang.common.YangNamespaceContext;
15 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.ParserNamespace;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
19
20 @Beta
21 public interface YangNamespaceContextNamespace extends ParserNamespace<StmtContext<?, ?, ?>, YangNamespaceContext> {
22     NamespaceBehaviour<StmtContext<?, ?, ?>, YangNamespaceContext, @NonNull YangNamespaceContextNamespace> BEHAVIOUR =
23             NamespaceBehaviour.global(YangNamespaceContextNamespace.class);
24
25     static @NonNull YangNamespaceContext computeIfAbsent(final StmtContext<?, ?, ?> ctx) {
26         final StmtContext<?, ?, ?> root = ctx.getRoot();
27         YangNamespaceContext ret = ctx.getFromNamespace(YangNamespaceContextNamespace.class, root);
28         if (ret == null) {
29             verify(ctx instanceof Mutable, "Cannot populate namespace context to %s", ctx);
30             ret = new StmtNamespaceContext(root);
31             ((Mutable<?, ?, ?>)ctx).addToNs(YangNamespaceContextNamespace.class, root, ret);
32         }
33         return ret;
34     }
35 }