Merge branch 'master' of ../controller
[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.EffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
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
22 /**
23  * Class providing necessary support for processing YANG 1.1 Notification
24  * statement.
25  */
26 @Beta
27 public final class NotificationStatementRFC7950Support extends AbstractNotificationStatementSupport {
28     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
29             .NOTIFICATION)
30             .addAny(YangStmtMapping.ANYDATA)
31             .addAny(YangStmtMapping.ANYXML)
32             .addAny(YangStmtMapping.CHOICE)
33             .addAny(YangStmtMapping.CONTAINER)
34             .addOptional(YangStmtMapping.DESCRIPTION)
35             .addAny(YangStmtMapping.GROUPING)
36             .addAny(YangStmtMapping.IF_FEATURE)
37             .addAny(YangStmtMapping.LEAF)
38             .addAny(YangStmtMapping.LEAF_LIST)
39             .addAny(YangStmtMapping.LIST)
40             .addAny(YangStmtMapping.MUST)
41             .addOptional(YangStmtMapping.REFERENCE)
42             .addOptional(YangStmtMapping.STATUS)
43             .addAny(YangStmtMapping.TYPEDEF)
44             .addAny(YangStmtMapping.USES)
45             .build();
46
47     private static final ImmutableSet<StatementDefinition> ILLEGAL_PARENTS = ImmutableSet.of(
48             YangStmtMapping.NOTIFICATION, YangStmtMapping.RPC, YangStmtMapping.ACTION);
49     private static final NotificationStatementRFC7950Support INSTANCE = new NotificationStatementRFC7950Support();
50
51     private NotificationStatementRFC7950Support() {
52         // Hidden
53     }
54
55     public static NotificationStatementRFC7950Support getInstance() {
56         return INSTANCE;
57     }
58
59     @Override
60     public EffectiveStatement<QName, NotificationStatement> createEffective(
61             final StmtContext<QName, NotificationStatement, EffectiveStatement<QName, NotificationStatement>> ctx) {
62         SourceException.throwIf(StmtContextUtils.hasAncestorOfType(ctx, ILLEGAL_PARENTS),
63             ctx.getStatementSourceReference(),
64             "Notification %s is defined within an rpc, action, or another notification",
65             ctx.getStatementArgument());
66         SourceException.throwIf(!StmtContextUtils.hasAncestorOfTypeWithChildOfType(ctx, YangStmtMapping.LIST,
67             YangStmtMapping.KEY), ctx.getStatementSourceReference(),
68             "Notification %s is defined within a list that has no key statement", ctx.getStatementArgument());
69         SourceException.throwIf(StmtContextUtils.hasParentOfType(ctx, YangStmtMapping.CASE),
70             ctx.getStatementSourceReference(), "Notification %s is defined within a case statement",
71             ctx.getStatementArgument());
72         return new NotificationEffectiveStatementImpl(ctx);
73     }
74
75     @Override
76     protected SubstatementValidator getSubstatementValidator() {
77         return SUBSTATEMENT_VALIDATOR;
78     }
79 }