Revert "Bug 4640: Change semantic-version to openconfig-version"
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / ImportEffectiveStatementImpl.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.stmt.rfc6020.effective;
9
10 import com.google.common.base.MoreObjects;
11 import java.util.Date;
12 import java.util.Objects;
13 import org.opendaylight.yangtools.concepts.SemVer;
14 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
15 import org.opendaylight.yangtools.yang.model.api.Module;
16 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
17 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
18 import org.opendaylight.yangtools.yang.model.api.stmt.ImportStatement;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.MissingSubstatementException;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
21 import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToSemVerModuleIdentifier;
22
23 public class ImportEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, ImportStatement> implements
24         ModuleImport {
25
26     private final String moduleName;
27     private final Date revision;
28     private final SemVer semVer;
29     private final String prefix;
30     private final String description;
31     private final String reference;
32
33     public ImportEffectiveStatementImpl(final StmtContext<String, ImportStatement, ?> ctx) {
34         super(ctx);
35
36         moduleName = ctx.getStatementArgument();
37         PrefixEffectiveStatementImpl prefixStmt = firstEffective(PrefixEffectiveStatementImpl.class);
38         if (prefixStmt != null) {
39             this.prefix = prefixStmt.argument();
40         } else {
41             throw new MissingSubstatementException("Prefix is mandatory substatement of import statement",
42                     ctx.getStatementSourceReference());
43         }
44
45         if (!ctx.isEnabledSemanticVersioning()) {
46             RevisionDateEffectiveStatementImpl revisionDateStmt = firstEffective(RevisionDateEffectiveStatementImpl.class);
47             this.revision = (revisionDateStmt == null) ? SimpleDateFormatUtil.DEFAULT_DATE_IMP : revisionDateStmt
48                     .argument();
49             this.semVer = Module.DEFAULT_SEMANTIC_VERSION;
50         } else {
51             ModuleIdentifier importedModuleIdentifier = ctx.getFromNamespace(ImpPrefixToSemVerModuleIdentifier.class, prefix);
52             revision = importedModuleIdentifier.getRevision();
53             semVer = importedModuleIdentifier.getSemanticVersion();
54         }
55
56         DescriptionEffectiveStatementImpl descriptionStmt = firstEffective(DescriptionEffectiveStatementImpl.class);
57         this.description = (descriptionStmt != null) ? descriptionStmt.argument() : null;
58
59         ReferenceEffectiveStatementImpl referenceStmt = firstEffective(ReferenceEffectiveStatementImpl.class);
60         this.reference = (referenceStmt != null) ? referenceStmt.argument() : null;
61     }
62
63     @Override
64     public String getModuleName() {
65         return moduleName;
66     }
67
68     @Override
69     public Date getRevision() {
70         return revision;
71     }
72
73     @Override
74     public SemVer getSemanticVersion() {
75         return semVer;
76     }
77
78     @Override
79     public String getPrefix() {
80         return prefix;
81     }
82
83     @Override
84     public String getDescription() {
85         return description;
86     }
87
88     @Override
89     public String getReference() {
90         return reference;
91     }
92
93     @Override
94     public int hashCode() {
95         return Objects.hash(moduleName, revision, prefix, semVer, description, reference);
96     }
97
98     @Override
99     public boolean equals(final Object obj) {
100         if (this == obj) {
101             return true;
102         }
103         if (obj == null) {
104             return false;
105         }
106         if (getClass() != obj.getClass()) {
107             return false;
108         }
109         ImportEffectiveStatementImpl other = (ImportEffectiveStatementImpl) obj;
110         return Objects.equals(moduleName, other.moduleName) && Objects.equals(revision, other.revision)
111                 && Objects.equals(semVer, other.semVer) && Objects.equals(prefix, other.prefix)
112                 && Objects.equals(description, other.description) && Objects.equals(reference, other.reference);
113     }
114
115     @Override
116     public String toString() {
117         return MoreObjects.toStringHelper(this).add("moduleName", getModuleName())
118                 .add("revision", getRevision()).add("semantic version", getSemanticVersion())
119                 .add("prefix", getPrefix()).add("description", getDescription())
120                 .add("reference", getReference()).toString();
121     }
122 }