Honeynode test tool
[transportpce.git] / tests / honeynode / minimal-distribution-core / src / main / java / io / fd / honeycomb / infra / distro / schema / YangModuleMappingIndex.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.infra.distro.schema;
18
19 import com.google.common.collect.ImmutableSet;
20 import com.google.common.collect.LinkedListMultimap;
21 import com.google.common.collect.Multimap;
22 import java.util.Arrays;
23 import java.util.Set;
24 import java.util.stream.Stream;
25 import javax.annotation.Nonnull;
26
27 /**
28  * Index from guice module to yang module providers
29  */
30 class YangModuleMappingIndex implements ResourceLoader {
31
32     private static final String G_MODULE_TOKEN = "GUICE_MODULE:";
33     private static final String KEY_VALUE_SEPARATOR = "|";
34     private static final String Y_MODULE_TOKEN = "YANG_MODULES:";
35     private static final String Y_MODULE_SEPARATOR = ",";
36
37     /**
38      * key - module class name
39      * value  - yang module provider
40      */
41     private final Multimap<String, String> index;
42
43     YangModuleMappingIndex(final String indexPath) {
44         this.index = LinkedListMultimap.create();
45         loadResourceContentsOnPath(indexPath)
46                 .forEach(line -> {
47                     final String moduleName = parseModuleName(line);
48                     parseYangModules(line).forEach(yModuleProvider -> index.put(moduleName, yModuleProvider));
49                 });
50     }
51
52     Set<String> getByModuleName(@Nonnull final String moduleName) {
53         return ImmutableSet.copyOf(index.get(moduleName));
54     }
55
56     int applicationModulesCount() {
57         return index.keySet().size();
58     }
59
60     private static String parseModuleName(final String rawLine) {
61         return rawLine.substring(rawLine.indexOf(G_MODULE_TOKEN) + G_MODULE_TOKEN.length(),
62                 rawLine.indexOf(KEY_VALUE_SEPARATOR));
63     }
64
65     private static Stream<String> parseYangModules(final String rawLine) {
66         return Arrays.stream(rawLine.substring(rawLine.indexOf(Y_MODULE_TOKEN) + Y_MODULE_TOKEN.length())
67                 .split(Y_MODULE_SEPARATOR));
68     }
69 }