Extend SDNC persistent service to store CM
[ccsdk/features.git] / sdnr / wt / data-provider / setup / src / main / java / org / onap / ccsdk / features / sdnr / wt / dataprovider / setup / ReleaseInformation.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  * 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.HashMap;
25 import java.util.Map;
26 import java.util.Set;
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.database.sqldb.SqlDBClient;
30 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.SdnrDbType;
31 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.ComponentName;
32 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.DatabaseInfo;
33 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.KeepDataSearchHitConverter;
34 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.MariaDBTableInfo;
35 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.Release;
36 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.SearchHitConverter;
37 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.elalto.ElAltoReleaseInformation;
38 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.frankfurt.FrankfurtReleaseInformation;
39 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.frankfurt.FrankfurtReleaseInformationR2;
40 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.guilin.GuilinReleaseInformation;
41 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.honolulu.HonoluluReleaseInformation;
42 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.istanbul.IstanbulReleaseInformation;
43 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.jakarta.JakartaReleaseInformation;
44
45 public abstract class ReleaseInformation {
46
47     // variables
48     private final Release release;
49     private final Map<ComponentName, DatabaseInfo> dbMap;
50     private Map<ComponentName, MariaDBTableInfo> mariadbMap;
51     // end of variables
52
53     // constructors
54     public ReleaseInformation(Release r, Map<ComponentName, DatabaseInfo> dbMap) {
55         this(r, dbMap, new HashMap<>());
56     }
57
58     public ReleaseInformation(Release r, Map<ComponentName, DatabaseInfo> dbMap,
59             Map<ComponentName, MariaDBTableInfo> mariadbMap) {
60         this.release = r;
61         this.dbMap = dbMap;
62         this.mariadbMap = mariadbMap;
63     }
64     // end of constructors
65
66     protected Release getReleas() {
67         return this.release;
68     }
69
70     /**
71      * get database alias for component
72      *
73      * @param name
74      * @return alias or null if not exists
75      */
76     public String getAlias(ComponentName name) {
77         return this.getAlias(name, "");
78     }
79
80     public String getAlias(ComponentName name, String prefix) {
81         return dbMap.get(name) == null ? null : prefix + dbMap.get(name).alias;
82     }
83
84     /**
85      * get index name for component
86      *
87      * @param comp
88      * @return null if component does not exists in this release, otherwise index name
89      */
90     public String getIndex(ComponentName comp) {
91         return this.getIndex(comp, "");
92     }
93
94     /**
95      * get index name for component with prefix
96      *
97      * @param comp
98      * @param prefix
99      * @return null if component does not exists in this release, otherwise index name
100      */
101     public String getIndex(ComponentName comp, String prefix) {
102         return dbMap.get(comp) == null ? null : (prefix + dbMap.get(comp).getIndex(this.release.getDBSuffix()));
103     }
104
105     /**
106      * get database datatype (doctype) for component
107      *
108      * @param name
109      * @return datatype or null if not exists
110      */
111     public String getDataType(ComponentName name) {
112         return dbMap.get(name) == null ? null : dbMap.get(name).doctype;
113     }
114
115     public String getDatabaseMapping(ComponentName name) {
116         return dbMap.get(name) == null ? null : dbMap.get(name).getMapping();
117     }
118
119     /**
120      * get database doctype definition for component
121      *
122      * @param name
123      * @return mappings or null if not exists
124      */
125     public String getDatabaseMapping(ComponentName name, boolean useStrict) {
126         return dbMap.get(name) == null ? null : dbMap.get(name).getMapping(useStrict);
127     }
128
129     /**
130      * get database settings definition for component
131      *
132      * @param name
133      * @return settings or null if not exists
134      */
135     public String getDatabaseSettings(ComponentName name, int shards, int replicas) {
136         return dbMap.get(name) == null ? null : dbMap.get(name).getSettings(shards, replicas);
137     }
138
139     public String getDatabaseMapping(ComponentName name, SdnrDbType dbType) {
140         switch (dbType) {
141             case ELASTICSEARCH:
142                 return this.getDatabaseMapping(name);
143             case MARIADB:
144                 return mariadbMap.get(name) == null ? null
145                         : mariadbMap.get(name).getMapping(this.release.getDBSuffix());
146             default:
147                 return null;
148         }
149     }
150
151     /**
152      * get converter for component data
153      *
154      * @param dst destination release
155      * @param comp component to convert
156      * @return
157      */
158     public SearchHitConverter getConverter(Release dst, ComponentName comp) {
159         if (dst == this.release && this.getComponents().contains(comp)) {
160             return new KeepDataSearchHitConverter(comp);
161         }
162         return null;
163     }
164
165     public static ReleaseInformation getInstance(Release r) {
166         switch (r) {
167             case EL_ALTO:
168                 return new ElAltoReleaseInformation();
169             case FRANKFURT_R1:
170                 return new FrankfurtReleaseInformation();
171             case FRANKFURT_R2:
172                 return new FrankfurtReleaseInformationR2();
173             case GUILIN_R1:
174                 return new GuilinReleaseInformation();
175             case HONOLULU_R1:
176                 return new HonoluluReleaseInformation();
177             case ISTANBUL_R1:
178                 return new IstanbulReleaseInformation();
179             case JAKARTA_R1:
180                 return new JakartaReleaseInformation();
181             default:
182                 return null;
183         }
184     }
185
186     /**
187      * @return
188      */
189     public Set<ComponentName> getComponents() {
190         return dbMap.keySet();
191     }
192
193     /**
194      * @param component
195      * @return
196      */
197     public boolean hasOwnDbIndex(ComponentName component) {
198         return this.getDatabaseMapping(component) != null;
199     }
200
201     /**
202      * @param indices
203      * @return true if components of this release are covered by the given indices
204      */
205     public boolean containsIndices(IndicesEntryList indices) {
206
207         if (this.dbMap.size() <= 0) {
208             return false;
209         }
210         for (DatabaseInfo entry : this.dbMap.values()) {
211             String dbIndexName = entry.getIndex(this.release.getDBSuffix());
212             if (indices.findByIndex(dbIndexName) == null) {
213                 return false;
214             }
215         }
216         return true;
217
218     }
219
220     /**
221      * @param dbClient
222      * @return if succeeded or not
223      */
224     public abstract boolean runPreInitCommands(HtDatabaseClient dbClient);
225
226     public abstract boolean runPreInitCommands(SqlDBClient dbClient);
227
228     /**
229      *
230      * @param dbClient
231      * @return if succeeded or not
232      */
233     public abstract boolean runPostInitCommands(HtDatabaseClient dbClient);
234
235     public abstract boolean runPostInitCommands(SqlDBClient dbClient);
236
237
238
239 }