3d595ed524aa29d32922a62f1ff57d225b2c9395
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / SchemaContextImpl.java
1 /*
2  * Copyright (c) 2013 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.impl;
9
10 import java.net.URI;
11 import java.util.Date;
12 import java.util.HashSet;
13 import java.util.LinkedHashSet;
14 import java.util.List;
15 import java.util.Set;
16
17 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
21 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort;
24
25 final class SchemaContextImpl implements SchemaContext {
26     private final Set<Module> modules;
27
28     SchemaContextImpl(final Set<Module> modules) {
29         this.modules = modules;
30     }
31
32     @Override
33     public Set<DataSchemaNode> getDataDefinitions() {
34         final Set<DataSchemaNode> dataDefs = new HashSet<DataSchemaNode>();
35         for (Module m : modules) {
36             dataDefs.addAll(m.getChildNodes());
37         }
38         return dataDefs;
39     }
40
41     @Override
42     public Set<Module> getModules() {
43         List<Module> sorted = ModuleDependencySort.sort(modules.toArray(new Module[modules.size()]));
44         return new LinkedHashSet<Module>(sorted);
45     }
46
47     @Override
48     public Set<NotificationDefinition> getNotifications() {
49         final Set<NotificationDefinition> notifications = new HashSet<NotificationDefinition>();
50         for (Module m : modules) {
51             notifications.addAll(m.getNotifications());
52         }
53         return notifications;
54     }
55
56     @Override
57     public Set<RpcDefinition> getOperations() {
58         final Set<RpcDefinition> rpcs = new HashSet<RpcDefinition>();
59         for (Module m : modules) {
60             rpcs.addAll(m.getRpcs());
61         }
62         return rpcs;
63     }
64
65     @Override
66     public Set<ExtensionDefinition> getExtensions() {
67         final Set<ExtensionDefinition> extensions = new HashSet<ExtensionDefinition>();
68         for (Module m : modules) {
69             extensions.addAll(m.getExtensionSchemaNodes());
70         }
71         return extensions;
72     }
73
74     @Override
75     public Module findModuleByName(final String name, final Date revision) {
76         if (name != null) {
77             for (final Module module : modules) {
78                 if (revision == null) {
79                     if (module.getName().equals(name)) {
80                         return module;
81                     }
82                 } else if (module.getName().equals(name) && module.getRevision().equals(revision)) {
83                     return module;
84                 }
85             }
86         }
87         return null;
88     }
89
90     @Override
91     public Set<Module> findModuleByNamespace(final URI namespace) {
92         final Set<Module> ret = new HashSet<Module>();
93         if (namespace != null) {
94             for (final Module module : modules) {
95                 if (module.getNamespace().equals(namespace)) {
96                     ret.add(module);
97                 }
98             }
99         }
100         return ret;
101     }
102
103     @Override
104     public Module findModuleByNamespaceAndRevision(URI namespace, Date revision) {
105         if (namespace != null) {
106             for (final Module module : modules) {
107                 if (module.getNamespace().equals(namespace) && module.getRevision().equals(revision)) {
108                     return(module);
109                 }
110             }
111         }
112         return null;
113     }
114
115 }