da7408df1b617642ddaa07b0b42431cff5c7d37e
[yangtools.git] / model / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / SchemaContextUtil.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.model.util;
9
10 import java.util.HashSet;
11 import java.util.Set;
12 import org.opendaylight.yangtools.yang.model.api.Module;
13 import org.opendaylight.yangtools.yang.model.api.ModuleLike;
14 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
15 import org.opendaylight.yangtools.yang.model.api.Submodule;
16 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
17 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
18
19 /**
20  * The Schema Context Util contains support methods for searching through Schema Context modules for specified schema
21  * nodes via Schema Path or Revision Aware XPath. The Schema Context Util is designed as mixin, so it is not
22  * instantiable.
23  */
24 public final class SchemaContextUtil {
25     private SchemaContextUtil() {
26         // Hidden on purpose
27     }
28
29     /**
30      * Extract the identifiers of all modules and submodules which were used to create a particular SchemaContext.
31      *
32      * @param context SchemaContext to be examined
33      * @return Set of ModuleIdentifiers.
34      */
35     // FIXME: rehost to yang-repo-spi (or -api?)
36     public static Set<SourceIdentifier> getConstituentModuleIdentifiers(final SchemaContext context) {
37         final Set<SourceIdentifier> ret = new HashSet<>();
38
39         for (Module module : context.getModules()) {
40             ret.add(moduleToIdentifier(module));
41
42             for (Submodule submodule : module.getSubmodules()) {
43                 ret.add(moduleToIdentifier(submodule));
44             }
45         }
46
47         return ret;
48     }
49
50     private static SourceIdentifier moduleToIdentifier(final ModuleLike module) {
51         return RevisionSourceIdentifier.create(module.getName(), module.getRevision());
52     }
53 }