Merge "Bug 1027: Decreased log level from INFO to DEBUG."
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / ModuleImpl.java
1 package org.opendaylight.yangtools.yang.parser.builder.impl;
2
3 import static com.google.common.base.Preconditions.checkNotNull;
4
5 import com.google.common.base.Optional;
6 import com.google.common.collect.ImmutableList;
7 import com.google.common.collect.ImmutableSet;
8 import java.net.URI;
9 import java.util.Collections;
10 import java.util.Date;
11 import java.util.List;
12 import java.util.Set;
13 import java.util.TreeSet;
14 import org.opendaylight.yangtools.concepts.Immutable;
15 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
16 import org.opendaylight.yangtools.yang.model.api.Deviation;
17 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
18 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
19 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.Module;
21 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
22 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
23 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
24 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
26 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainer;
27 import org.opendaylight.yangtools.yang.parser.builder.util.Comparators;
28
29 public final class ModuleImpl extends AbstractDocumentedDataNodeContainer implements Module, Immutable {
30
31     private final URI namespace;
32     private final String name;
33     private final String sourcePath;
34     private final Optional<Date> revision;
35     private final String prefix;
36     private final String yangVersion;
37     private final String organization;
38     private final String contact;
39     private final Set<ModuleImport> imports;
40     private final Set<FeatureDefinition> features;
41     private final Set<NotificationDefinition> notifications;
42     private final Set<AugmentationSchema> augmentations;
43     private final Set<RpcDefinition> rpcs;
44     private final Set<Deviation> deviations;
45     private final List<ExtensionDefinition> extensionNodes;
46     private final Set<IdentitySchemaNode> identities;
47     private final List<UnknownSchemaNode> unknownNodes;
48     private final String source;
49
50     /**
51      *
52      *
53      * <b>Note</b>Constructor has intentionality limited visibility to package, since
54      * this class should be only instantiated via builders.
55      *
56      * @param name
57      * @param sourcePath
58      * @param builder
59      */
60     ModuleImpl(final String name, final String sourcePath, final ModuleBuilder builder) {
61         super(builder);
62         this.name = checkNotNull(name, "Missing name");
63         this.sourcePath = sourcePath; //TODO: can this be nullable?
64         this.imports = ImmutableSet.<ModuleImport> copyOf(builder.imports);
65         this.namespace = builder.getNamespace();
66         this.prefix = builder.getPrefix();
67         this.revision = builder.getRevision() == null ? Optional.<Date>absent():
68                 Optional.of(new Date(builder.getRevision().getTime()));
69         this.yangVersion = builder.getYangVersion();
70         this.organization = builder.getOrganization();
71         this.contact = builder.getContact();
72         this.features = toImmutableSortedSet(builder.getFeatures());
73         this.notifications = toImmutableSortedSet(builder.getNotifications());
74         this.augmentations = ImmutableSet.copyOf(builder.getAugments());
75         this.rpcs = toImmutableSortedSet(builder.getRpcs());
76         this.deviations = ImmutableSet.copyOf(builder.getDeviations());
77         this.extensionNodes = ImmutableList.copyOf(builder.getExtensions());
78         this.identities = ImmutableSet.copyOf(builder.getIdentities());
79         this.unknownNodes = ImmutableList.copyOf(builder.getExtensionInstances());
80         this.source = checkNotNull(builder.getSource(), "Missing source");
81
82     }
83
84     @Override
85     public String getModuleSourcePath() {
86         return sourcePath;
87     }
88
89     @Override
90     public URI getNamespace() {
91         return namespace;
92     }
93
94     @Override
95     public String getName() {
96         return name;
97     }
98
99     @Override
100     public Date getRevision() {
101         if (revision.isPresent()) {
102             return new Date(revision.get().getTime());
103         } else {
104             return null;
105         }
106     }
107
108     @Override
109     public String getPrefix() {
110         return prefix;
111     }
112
113     @Override
114     public String getYangVersion() {
115         return yangVersion;
116     }
117
118     @Override
119     public String getOrganization() {
120         return organization;
121     }
122
123     @Override
124     public String getContact() {
125         return contact;
126     }
127
128     @Override
129     public Set<ModuleImport> getImports() {
130         return imports;
131     }
132
133     @Override
134     public Set<FeatureDefinition> getFeatures() {
135         return features;
136     }
137
138     @Override
139     public Set<NotificationDefinition> getNotifications() {
140         return notifications;
141     }
142
143     @Override
144     public Set<AugmentationSchema> getAugmentations() {
145         return augmentations;
146     }
147
148     @Override
149     public Set<RpcDefinition> getRpcs() {
150         return rpcs;
151     }
152
153     @Override
154     public Set<Deviation> getDeviations() {
155         return deviations;
156     }
157
158     @Override
159     public List<ExtensionDefinition> getExtensionSchemaNodes() {
160         return extensionNodes;
161     }
162
163     @Override
164     public Set<IdentitySchemaNode> getIdentities() {
165         return identities;
166     }
167
168     @Override
169     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
170         return unknownNodes;
171     }
172
173     public String getSource() {
174         return source;
175     }
176
177     @Override
178     public int hashCode() {
179         final int prime = 31;
180         int result = 1;
181         result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
182         result = prime * result + ((name == null) ? 0 : name.hashCode());
183         result = prime * result + ((revision == null) ? 0 : revision.hashCode());
184         result = prime * result + ((yangVersion == null) ? 0 : yangVersion.hashCode());
185         return result;
186     }
187
188     @Override
189     public boolean equals(final Object obj) {
190         if (this == obj) {
191             return true;
192         }
193         if (obj == null) {
194             return false;
195         }
196         if (getClass() != obj.getClass()) {
197             return false;
198         }
199         ModuleImpl other = (ModuleImpl) obj;
200         if (namespace == null) {
201             if (other.namespace != null) {
202                 return false;
203             }
204         } else if (!namespace.equals(other.namespace)) {
205             return false;
206         }
207         if (name == null) {
208             if (other.name != null) {
209                 return false;
210             }
211         } else if (!name.equals(other.name)) {
212             return false;
213         }
214         if (revision == null) {
215             if (other.revision != null) {
216                 return false;
217             }
218         } else if (!revision.equals(other.revision)) {
219             return false;
220         }
221         if (yangVersion == null) {
222             if (other.yangVersion != null) {
223                 return false;
224             }
225         } else if (!yangVersion.equals(other.yangVersion)) {
226             return false;
227         }
228         return true;
229     }
230
231     private static <T extends SchemaNode> Set<T> toImmutableSortedSet(final Set<T> original) {
232         TreeSet<T> sorted = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
233         sorted.addAll(original);
234         return Collections.unmodifiableSet(sorted);
235     }
236
237     @Override
238     public String toString() {
239         StringBuilder sb = new StringBuilder(ModuleImpl.class.getSimpleName());
240         sb.append("[");
241         sb.append("name=" + name);
242         sb.append(", namespace=" + namespace);
243         sb.append(", revision=" + revision);
244         sb.append(", prefix=" + prefix);
245         sb.append(", yangVersion=" + yangVersion);
246         sb.append("]");
247         return sb.toString();
248     }
249 }