Refactor notification statement implementation
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / notification / NotificationStatementRFC7950Support.java
1 /*
2  * Copyright (c) 2016 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.notification;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.collect.ImmutableSet;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
14 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
15 import org.opendaylight.yangtools.yang.model.api.stmt.NotificationEffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.NotificationStatement;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
20 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
21 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
22
23 /**
24  * Class providing necessary support for processing YANG 1.1 Notification
25  * statement.
26  */
27 @Beta
28 public final class NotificationStatementRFC7950Support extends AbstractNotificationStatementSupport {
29     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
30             .NOTIFICATION)
31             .addAny(YangStmtMapping.ANYDATA)
32             .addAny(YangStmtMapping.ANYXML)
33             .addAny(YangStmtMapping.CHOICE)
34             .addAny(YangStmtMapping.CONTAINER)
35             .addOptional(YangStmtMapping.DESCRIPTION)
36             .addAny(YangStmtMapping.GROUPING)
37             .addAny(YangStmtMapping.IF_FEATURE)
38             .addAny(YangStmtMapping.LEAF)
39             .addAny(YangStmtMapping.LEAF_LIST)
40             .addAny(YangStmtMapping.LIST)
41             .addAny(YangStmtMapping.MUST)
42             .addOptional(YangStmtMapping.REFERENCE)
43             .addOptional(YangStmtMapping.STATUS)
44             .addAny(YangStmtMapping.TYPEDEF)
45             .addAny(YangStmtMapping.USES)
46             .build();
47
48     private static final ImmutableSet<StatementDefinition> ILLEGAL_PARENTS = ImmutableSet.of(
49             YangStmtMapping.NOTIFICATION, YangStmtMapping.RPC, YangStmtMapping.ACTION);
50     private static final NotificationStatementRFC7950Support INSTANCE = new NotificationStatementRFC7950Support();
51
52     private NotificationStatementRFC7950Support() {
53         // Hidden
54     }
55
56     public static NotificationStatementRFC7950Support getInstance() {
57         return INSTANCE;
58     }
59
60     @Override
61     protected SubstatementValidator getSubstatementValidator() {
62         return SUBSTATEMENT_VALIDATOR;
63     }
64
65
66     @Override
67     void checkEffective(final StmtContext<QName, NotificationStatement, NotificationEffectiveStatement> ctx) {
68         final StatementSourceReference ref = ctx.getStatementSourceReference();
69         final QName argument = ctx.getStatementArgument();
70         SourceException.throwIf(StmtContextUtils.hasAncestorOfType(ctx, ILLEGAL_PARENTS), ref,
71             "Notification %s is defined within an rpc, action, or another notification", argument);
72         SourceException.throwIf(
73             !StmtContextUtils.hasAncestorOfTypeWithChildOfType(ctx, YangStmtMapping.LIST, YangStmtMapping.KEY), ref,
74             "Notification %s is defined within a list that has no key statement", argument);
75         SourceException.throwIf(StmtContextUtils.hasParentOfType(ctx, YangStmtMapping.CASE), ref,
76             "Notification %s is defined within a case statement", argument);
77     }
78 }