4d3f62d8996026f6b949f9708e2e923bd99e1684
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / extension / UnrecognizedEffectiveStatementImpl.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.extension;
9
10 import java.util.Objects;
11 import org.eclipse.jdt.annotation.NonNull;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
14 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.stmt.UnrecognizedEffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.UnrecognizedStatement;
17 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.UnknownEffectiveStatementBase;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
20 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 final class UnrecognizedEffectiveStatementImpl extends UnknownEffectiveStatementBase<String, UnrecognizedStatement>
25         implements UnrecognizedEffectiveStatement {
26     private static final Logger LOG = LoggerFactory.getLogger(UnrecognizedEffectiveStatementImpl.class);
27
28     private final QName maybeQNameArgument;
29     private final @NonNull SchemaPath path;
30
31     UnrecognizedEffectiveStatementImpl(final StmtContext<String, UnrecognizedStatement, ?> ctx) {
32         super(ctx);
33
34         // FIXME: Remove following section after fixing 4380
35         final UnknownSchemaNode original = (UnknownSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective)
36                 .orElse(null);
37         if (original != null) {
38             this.maybeQNameArgument = original.getQName();
39         } else {
40             QName maybeQNameArgumentInit = null;
41             try {
42                 maybeQNameArgumentInit = StmtContextUtils.qnameFromArgument(ctx, argument());
43             } catch (SourceException e) {
44                 LOG.debug("Not constructing QName from {}", argument(), e);
45                 maybeQNameArgumentInit = getNodeType();
46             }
47             this.maybeQNameArgument = maybeQNameArgumentInit;
48         }
49
50         SchemaPath maybePath;
51         try {
52             maybePath = ctx.coerceParentContext().getSchemaPath()
53                     .map(parentPath -> parentPath.createChild(maybeQNameArgument)).orElse(null);
54         } catch (IllegalArgumentException | SourceException e) {
55             LOG.debug("Cannot construct path for {}, attempting to recover", ctx, e);
56             maybePath = null;
57         }
58         path = maybePath;
59     }
60
61     @Override
62     public QName getQName() {
63         return maybeQNameArgument;
64     }
65
66     @Override
67     public SchemaPath getPath() {
68         return path;
69     }
70
71     @Override
72     public int hashCode() {
73         final int prime = 31;
74         int result = 1;
75         result = prime * result + Objects.hashCode(maybeQNameArgument);
76         result = prime * result + Objects.hashCode(path);
77         result = prime * result + Objects.hashCode(getNodeType());
78         result = prime * result + Objects.hashCode(getNodeParameter());
79         return result;
80     }
81
82     @Override
83     public boolean equals(final Object obj) {
84         if (this == obj) {
85             return true;
86         }
87         if (!(obj instanceof UnrecognizedEffectiveStatementImpl)) {
88             return false;
89         }
90         UnrecognizedEffectiveStatementImpl other = (UnrecognizedEffectiveStatementImpl) obj;
91         return Objects.equals(maybeQNameArgument, other.maybeQNameArgument) && Objects.equals(path, other.path)
92                 && Objects.equals(getNodeType(), other.getNodeType())
93                 && Objects.equals(getNodeParameter(), other.getNodeParameter());
94     }
95 }