cb20025ffebc5250698df7f8c810fc2a7f4b4c64
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.setup;
23
24 import java.util.Map;
25 import java.util.Set;
26
27 import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient;
28 import org.onap.ccsdk.features.sdnr.wt.common.database.data.IndicesEntryList;
29 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.ComponentName;
30 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.DatabaseInfo;
31 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.KeepDataSearchHitConverter;
32 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.Release;
33 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.SearchHitConverter;
34 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.elalto.ElAltoReleaseInformation;
35 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.frankfurt.FrankfurtReleaseInformation;
36 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.guilin.GuilinReleaseInformation;
37
38 public abstract class ReleaseInformation {
39
40     // variables
41     private final Release release;
42     private final Map<ComponentName, DatabaseInfo> dbMap;
43     // end of variables
44
45     // constructors
46     public ReleaseInformation(Release r, Map<ComponentName, DatabaseInfo> dbMap) {
47         this.release = r;
48         this.dbMap = dbMap;
49     }
50     // end of constructors
51
52     /**
53      * get database alias for component
54      * 
55      * @param name
56      * @return alias or null if not exists
57      */
58     public String getAlias(ComponentName name) {
59         return this.getAlias(name, "");
60     }
61
62     public String getAlias(ComponentName name, String prefix) {
63         return dbMap.get(name) == null ? null : prefix + dbMap.get(name).alias;
64     }
65
66     /**
67      * get index name for component
68      * 
69      * @param comp
70      * @return null if component does not exists in this release, otherwise index name
71      */
72     public String getIndex(ComponentName comp) {
73         return this.getIndex(comp, "");
74     }
75
76     /**
77      * get index name for component with prefix
78      * 
79      * @param comp
80      * @param prefix
81      * @return null if component does not exists in this release, otherwise index name
82      */
83     public String getIndex(ComponentName comp, String prefix) {
84         return dbMap.get(comp) == null ? null : (prefix + dbMap.get(comp).getIndex(this.release.getDBSuffix()));
85     }
86
87     /**
88      * get database datatype (doctype) for component
89      * 
90      * @param name
91      * @return datatype or null if not exists
92      */
93     public String getDataType(ComponentName name) {
94         return dbMap.get(name) == null ? null : dbMap.get(name).doctype;
95     }
96
97     public String getDatabaseMapping(ComponentName name) {
98         return dbMap.get(name) == null ? null : dbMap.get(name).getMapping();
99     }
100
101     /**
102      * get database doctype definition for component
103      * 
104      * @param name
105      * @return mappings or null if not exists
106      */
107     public String getDatabaseMapping(ComponentName name, boolean useStrict) {
108         return dbMap.get(name) == null ? null : dbMap.get(name).getMapping(useStrict);
109     }
110
111     /**
112      * get database settings definition for component
113      * 
114      * @param name
115      * @return settings or null if not exists
116      */
117     public String getDatabaseSettings(ComponentName name, int shards, int replicas) {
118         return dbMap.get(name) == null ? null : dbMap.get(name).getSettings(shards, replicas);
119     }
120
121     /**
122      * get converter for component data
123      * 
124      * @param dst destination release
125      * @param comp component to convert
126      * @return
127      */
128     public SearchHitConverter getConverter(Release dst, ComponentName comp) {
129         if (dst == this.release && this.getComponents().contains(comp)) {
130             return new KeepDataSearchHitConverter(comp);
131         }
132         return null;
133     }
134
135     public static ReleaseInformation getInstance(Release r) {
136         switch (r) {
137             case EL_ALTO:
138                 return new ElAltoReleaseInformation();
139             case FRANKFURT_R1:
140                 return new FrankfurtReleaseInformation();
141             case GUILIN:
142                 return new GuilinReleaseInformation();
143             default:
144                 return null;
145         }
146     }
147
148     /**
149      * @return
150      */
151     public Set<ComponentName> getComponents() {
152         return dbMap.keySet();
153     }
154
155     /**
156      * @param component
157      * @return
158      */
159     public boolean hasOwnDbIndex(ComponentName component) {
160         return this.getDatabaseMapping(component) != null;
161     }
162
163     /**
164      * @param indices
165      * @return true if components of this release are covered by the given indices
166      */
167     protected boolean containsIndices(IndicesEntryList indices) {
168
169         if (this.dbMap.size() <= 0) {
170             return false;
171         }
172         for (DatabaseInfo entry : this.dbMap.values()) {
173             String dbIndexName = entry.getIndex(this.release.getDBSuffix());
174             if (indices.findByIndex(dbIndexName) == null) {
175                 return false;
176             }
177         }
178         return true;
179
180     }
181
182     /**
183      * @param dbClient
184      * @return if succeeded or not
185      */
186     protected abstract boolean runPreInitCommands(HtDatabaseClient dbClient);
187
188     /**
189      * 
190      * @param dbClient
191      * @return if succeeded or not
192      */
193     protected abstract boolean runPostInitCommands(HtDatabaseClient dbClient);
194
195 }