e90d20cdec6fe5899952568a37cedc8a1fe39ae4
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / notification / AbstractNotificationStatementSupport.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, 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.stmt.notification;
9
10 import com.google.common.collect.ImmutableList;
11 import org.opendaylight.yangtools.yang.common.QName;
12 import org.opendaylight.yangtools.yang.model.api.Status;
13 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
14 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.NotificationEffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.NotificationStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
19 import org.opendaylight.yangtools.yang.parser.rfc7950.namespace.ChildSchemaNodeNamespace;
20 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseQNameStatementSupport;
21 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStatementMixins.EffectiveStatementWithFlags.FlagsBuilder;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
25
26 abstract class AbstractNotificationStatementSupport
27         extends BaseQNameStatementSupport<NotificationStatement, NotificationEffectiveStatement> {
28     AbstractNotificationStatementSupport() {
29         super(YangStmtMapping.NOTIFICATION);
30     }
31
32     @Override
33     public final QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
34         return StmtContextUtils.parseIdentifier(ctx, value);
35     }
36
37     @Override
38     public final void onStatementAdded(
39             final Mutable<QName, NotificationStatement, NotificationEffectiveStatement> stmt) {
40         stmt.coerceParentContext().addToNs(ChildSchemaNodeNamespace.class, stmt.coerceStatementArgument(), stmt);
41     }
42
43     @Override
44     protected final NotificationStatement createDeclared(final StmtContext<QName, NotificationStatement, ?> ctx,
45             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
46         return new RegularNotificationStatement(ctx.coerceStatementArgument(), substatements);
47     }
48
49     @Override
50     protected final NotificationStatement createEmptyDeclared(final StmtContext<QName, NotificationStatement, ?> ctx) {
51         return new EmptyNotificationStatement(ctx.coerceStatementArgument());
52     }
53
54     @Override
55     protected final NotificationEffectiveStatement createEffective(
56             final StmtContext<QName, NotificationStatement, NotificationEffectiveStatement> ctx,
57             final NotificationStatement declared,
58             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
59         checkEffective(ctx);
60
61         final int flags = new FlagsBuilder()
62                 .setHistory(ctx.getCopyHistory())
63                 .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
64                 .toFlags();
65
66         return new NotificationEffectiveStatementImpl(declared, flags, ctx, substatements);
67     }
68
69     @Override
70     protected final NotificationEffectiveStatement createEmptyEffective(
71             final StmtContext<QName, NotificationStatement, NotificationEffectiveStatement> ctx,
72             final NotificationStatement declared) {
73         return createEffective(ctx, declared, ImmutableList.of());
74     }
75
76     abstract void checkEffective(StmtContext<QName, NotificationStatement, NotificationEffectiveStatement> ctx);
77 }