2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.infra.distro.schema;
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;
24 import java.util.stream.Stream;
25 import javax.annotation.Nonnull;
28 * Index from guice module to yang module providers
30 class YangModuleMappingIndex implements ResourceLoader {
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 = ",";
38 * key - module class name
39 * value - yang module provider
41 private final Multimap<String, String> index;
43 YangModuleMappingIndex(final String indexPath) {
44 this.index = LinkedListMultimap.create();
45 loadResourceContentsOnPath(indexPath)
47 final String moduleName = parseModuleName(line);
48 parseYangModules(line).forEach(yModuleProvider -> index.put(moduleName, yModuleProvider));
52 Set<String> getByModuleName(@Nonnull final String moduleName) {
53 return ImmutableSet.copyOf(index.get(moduleName));
56 int applicationModulesCount() {
57 return index.keySet().size();
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));
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));