SDN-R data-provider with ES7
[ccsdk/features.git] / sdnr / wt / data-provider / setup / src / main / java / org / onap / ccsdk / features / sdnr / wt / dataprovider / setup / data / MavenDatabasePluginInitFile.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Update Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *     http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  *
23  */
24 package org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Set;
32
33 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.ReleaseInformation;
34
35
36 /**
37  * @author Michael Dürre
38  *
39  */
40 public class MavenDatabasePluginInitFile {
41     private static final int replicas = 1;
42     private static final int shards = 5;
43     private static final String mappings = "\"mappings\":%s";
44     private static final String settings =
45             "\"settings\":{\"index\":{\"number_of_shards\":%d,\"number_of_replicas\":%d},\"analysis\":{\"analyzer\":{\"content\":"
46                     + "{\"type\":\"custom\",\"tokenizer\":\"whitespace\"}}}}";
47
48     public static void create(Release release, String filename) throws IOException {
49
50         ReleaseInformation ri = ReleaseInformation.getInstance(release);
51         Set<ComponentName> comps = ri.getComponents();
52         List<String> lines = new ArrayList<>();
53         lines.add("PUT:_cluster/settings/:{\"persistent\":{\"action.auto_create_index\":\"true\"}}");
54         for (ComponentName c : comps) {
55             lines.add(String.format("PUT:%s/:{" + settings + "," + mappings + "}", ri.getIndex(c), shards, replicas,
56                     ri.getDatabaseMapping(c)));
57             lines.add(String.format("PUT:%s/_alias/%s/:{}", ri.getIndex(c), ri.getAlias(c)));
58         }
59
60         File filePath = new File(filename);
61         if (filePath.getParentFile() != null && !filePath.getParentFile().exists()) {
62             //Crate Directory if missing
63             filePath.getParentFile().mkdirs();
64         }
65         Files.write(filePath.toPath(), lines);
66     }
67 }