aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/data-provider/setup/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/setup/database/MariaDbDataMigrationProvider.java
blob: 55ba66ba6a6aaf2367ac97514d7d513fe0d99a16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
 * ============LICENSE_START=======================================================
 * ONAP : ccsdk features
 * ================================================================================
 * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
 * All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 *
 */
package org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.database;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import org.onap.ccsdk.features.sdnr.wt.common.database.SearchHit;
import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult;
import org.onap.ccsdk.features.sdnr.wt.common.database.data.AliasesEntry;
import org.onap.ccsdk.features.sdnr.wt.common.database.data.AliasesEntryList;
import org.onap.ccsdk.features.sdnr.wt.common.database.data.DatabaseVersion;
import org.onap.ccsdk.features.sdnr.wt.common.database.data.IndicesEntry;
import org.onap.ccsdk.features.sdnr.wt.common.database.data.IndicesEntryList;
import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBClient;
import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.SdnrDbType;
import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.DataMigrationProviderService;
import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.ReleaseInformation;
import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.ComponentData;
import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.ComponentName;
import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.DataContainer;
import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.DataMigrationReport;
import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.Release;
import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.ReleaseGroup;
import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.SearchHitConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MariaDbDataMigrationProvider implements DataMigrationProviderService {


    private static final Logger LOG = LoggerFactory.getLogger(MariaDbDataMigrationProvider.class);
    private static final SdnrDbType DBTYPE = SdnrDbType.MARIADB;
    private static final String LOG_DELETING_INDEX = "deleting index {}";
    private final SqlDBClient dbClient;

    public MariaDbDataMigrationProvider(String url, String username, String password, boolean trustAll,
            long timeoutms) throws Exception {
        dbClient = new SqlDBClient(url, username, password);
    }

    @Override
    public DataMigrationReport importData(String filename, boolean dryrun) throws Exception {
        return this.importData(filename, dryrun, Release.CURRENT_RELEASE);
    }

    @Override
    public DataMigrationReport importData(String filename, boolean dryrun, Release forRelease) throws Exception {
        DataMigrationReport report = new DataMigrationReport();
        File file = new File(filename);
        if (!file.exists()) {
            if (dryrun) {
                report.error("file %s not found", filename);
                return report;
            }
            throw new FileNotFoundException(filename);
        }
        DataContainer container = null;
        try {
            container = DataContainer.load(file);
        } catch (Exception e) {
            if (dryrun) {
                report.error("problem loading file %s: %s", filename, e.getMessage());
                return report;
            }
            throw new Exception("problem loading file " + filename, e);
        }
        ReleaseInformation ri = ReleaseInformation.getInstance(forRelease);
        SearchHitConverter converter;
        Set<ComponentName> components = ri.getComponents();
        //for all db components of dest architecture
        for (ComponentName component : components) {
            //convert to ComponentData for current release with existing ComponentData of the container
            converter = SearchHitConverter.Factory.getInstance(container.getRelease(), forRelease, component);
            if (converter == null) {
                continue;
            }
            ComponentData data = converter.convert(container);
            if (data != null) {
                String indexName = ri.getAlias(component);
                String dataTypeName = ri.getDataType(component);
                if (dryrun) {
                    report.log("write %d entries into %s/%s", data.size(), indexName, dataTypeName);
                } else {
                    LOG.debug("write {} entries into {}/{}", data.size(), indexName, dataTypeName);
                }
                for (SearchHit item : data) {
                    if (!dryrun) {
                        String id = null;//this.dbClient.doWriteRaw(indexName, dataTypeName, item.getId(), item.getSourceAsString(), true);
                        if (!item.getId().equals(id)) {
                            LOG.warn("entry for {} with original id {} was written with another id {}",
                                    component.getValue(), item.getId(), id);
                        }
                    }
                }
            } else {
                if (dryrun) {
                    report.error("unable to convert data for " + component.getValue() + " from version "
                            + container.getRelease().getValue() + " to " + forRelease.getValue() + "\n");
                } else {
                    LOG.warn("unable to convert data for {} from version {} to {}", component.getValue(),
                            container.getRelease().getValue(), forRelease.getValue());
                }
            }
        }
        LOG.info("import of {} completed", filename);
        if (dryrun) {
            report.log("import of %s completed", filename);
        }
        report.setCompleted(true);
        return report;
    }


    /**
     * export data if file exists .1 (.n) will be created
     *
     */
    @Override
    public DataMigrationReport exportData(String filename) {
        DataMigrationReport report = new DataMigrationReport();

        DataContainer container = new DataContainer();

        filename = this.checkFilenameForWrite(filename);
        LOG.info("output will be written to {}", filename);
        //autodetect version
        Release dbRelease = this.autoDetectRelease();
        if (dbRelease == null) {
            report.error("unbable to detect db release. is database initialized?");
            return report;
        }
        ReleaseInformation ri = ReleaseInformation.getInstance(dbRelease);
        boolean componentsSucceeded = true;
        for (ComponentName c : ri.getComponents()) {
            ComponentData data = new ComponentData(c);
            SearchResult<SearchHit> result = null;//this.dbClient.doReadAllJsonData(ri.getAlias(c), ri.getDataType(c), false);
            data.addAll(result.getHits());
            container.addComponent(c, data);
        }
        try {
            Files.write(new File(filename).toPath(), Arrays.asList(container.toJSON()), StandardCharsets.UTF_8);
            report.setCompleted(componentsSucceeded);
        } catch (IOException e) {
            LOG.warn("problem writing data to {}: {}", filename, e);
        }
        return report;
    }

    private String checkFilenameForWrite(String filename) {
        File f = new File(filename);
        if (!f.exists()) {
            return filename;
        }
        return this.checkFilenameForWrite(filename, 0);
    }

    private String checkFilenameForWrite(String filename, int apdx) {
        File f = new File(String.format("$s.$d", filename, apdx));
        if (!f.exists()) {
            return filename;
        }
        return this.checkFilenameForWrite(filename, apdx + 1);
    }

    @Override
    public Release getCurrentVersion() {
        return Release.CURRENT_RELEASE;
    }


    @Override
    public Release autoDetectRelease() {
        DatabaseVersion dbVersion;
        try {
            dbVersion = this.dbClient.readActualVersion();
        } catch (SQLException | ParseException e) {
            LOG.error("unable to detect db version", e);
            return null;
        }
        AliasesEntryList aliases = this.dbClient.readViews();
        IndicesEntryList indices = this.dbClient.readTables();
        if (indices == null) {
            return null;
        }
        List<Release> foundReleases = new ArrayList<>();
        //if there are active aliases reduce indices to the active ones
        if (aliases != null && !aliases.isEmpty()) {
            indices = indices.subList(aliases.getLinkedIndices());
        }
        for (Release r : Release.values()) {
            if (r.isDbInRange(dbVersion, SdnrDbType.MARIADB)) {
                ReleaseInformation ri = ReleaseInformation.getInstance(r);
                if (ri != null && ri.containsIndices(indices)) {
                    foundReleases.add(r);
                }
            }
        }
        if (foundReleases.size() == 1) {
            return foundReleases.get(0);
        }
        LOG.error("detect {} releases: {}. unable to detect for which one to do sth.", foundReleases.size(),
                foundReleases);
        return null;
    }

    @Override
    public boolean initDatabase(Release release, int numShards, int numReplicas, String dbPrefix, boolean forceRecreate,
            long timeoutms) {
        if (timeoutms > 0) {
            this.dbClient.waitForYellowStatus(timeoutms);
        }
        DatabaseVersion dbVersion;
        try {
            dbVersion = this.dbClient.readActualVersion();
        } catch (SQLException | ParseException e1) {
            LOG.error("unable to detect db version", e1);
            return false;
        }
        if (dbVersion == null) {
            return false;
        }
        LOG.info("detected database version {}", dbVersion);
        if (release == null) {
            release = ReleaseGroup.CURRENT_RELEASE.getLatestCompatibleRelease(dbVersion, SdnrDbType.MARIADB);
            if (release == null) {
                LOG.warn("unable to autodetect release for this database version for release {}",
                        ReleaseGroup.CURRENT_RELEASE.name());
                return false;
            }
            LOG.info("autodetect release {}", release);
        }
        if (!release.isDbInRange(dbVersion, SdnrDbType.MARIADB)) {
            LOG.warn("db version {} maybe not compatible with release {}", dbVersion, release);
            return false;
        }
        if (forceRecreate) {
            this.clearDatabase(release, dbPrefix, 0);
        }
        ReleaseInformation ri = ReleaseInformation.getInstance(release);
        AliasesEntryList views = this.dbClient.readViews();
        IndicesEntryList indices = this.dbClient.readTables();
        if (views == null || indices == null) {
            return false;
        }
        boolean response = false;
        if (!ri.runPreInitCommands(this.dbClient)) {
            return false;
        }
        for (ComponentName component : ri.getComponents()) {
            try {
                if (ri.hasOwnDbIndex(component)) {
                    //check if index already exists
                    String tableName = ri.getIndex(component, dbPrefix);
                    String viewName = ri.getAlias(component, dbPrefix);
                    if (indices.findByIndex(tableName) == null) {
                        LOG.info("creating index for {}", component);
                        response = this.dbClient.createTable(ri.getIndex(component, dbPrefix), ri.getDatabaseMapping(component, DBTYPE));
                        LOG.info(response ? "succeeded" : "failed");
                    } else {
                        LOG.info("index {} for {} already exists", tableName, component);
                    }
                    //check if alias already exists
                    if (views.findByAlias(viewName) == null) {
                        LOG.info("creating alias for {}", component);
                        response = this.dbClient.createView(tableName, viewName);
                        LOG.info(response ? "succeeded" : "failed");
                    } else {
                        LOG.info("view {} for table {} for {} already exists", viewName, tableName, component);
                    }
                }
            } catch (SQLException e) {
                LOG.error(e.getMessage());
                return false;
            }
        }
        if (!ri.runPostInitCommands(this.dbClient)) {
            return false;
        }
        return true;
    }

    @Override
    public boolean clearDatabase(Release release, String dbPrefix, long timeoutms) {

        if (timeoutms > 0) {
            this.dbClient.waitForYellowStatus(timeoutms);
        }
        //check aliases
        AliasesEntryList entries = this.dbClient.readViews();
        IndicesEntryList entries2 = this.dbClient.readTables();
        if (entries == null) {
            return false;
        }
        if (release == null) {
            DatabaseVersion dbVersion;
            try {
                dbVersion = this.dbClient.readActualVersion();
            } catch (SQLException | ParseException e) {
                LOG.error("unable to detect db version", e);
                return false;
            }
            LOG.info("detected database version {}", dbVersion);
            release = ReleaseGroup.CURRENT_RELEASE.getLatestCompatibleRelease(dbVersion, SdnrDbType.MARIADB);
            if (release == null) {
                LOG.warn("unable to autodetect release for this database version for release {}",
                        ReleaseGroup.CURRENT_RELEASE.name());
                return false;
            }
            LOG.info("autodetect release {}", release);
        }
        ReleaseInformation ri = ReleaseInformation.getInstance(release);
        boolean response;
        if (entries.isEmpty()) {
            LOG.info("no aliases to clear");
        } else {
            //check for every component of release if alias exists
            for (ComponentName component : ri.getComponents()) {
                String aliasToDelete = ri.getAlias(component, dbPrefix);
                AliasesEntry entryToDelete = entries.findByAlias(aliasToDelete);
                if (entryToDelete != null) {
                    try {
                        LOG.info("deleting alias {} for index {}", entryToDelete.getAlias(), entryToDelete.getIndex());
                        response = this.dbClient.deleteView(entryToDelete.getAlias());
                        LOG.info(response ? "succeeded" : "failed");
                    } catch (SQLException e) {
                        LOG.error(e.getMessage());
                        return false;
                    }
                } else {
                    //try to find malformed typed index with alias name
                    IndicesEntry entry2ToDelete = entries2.findByIndex(aliasToDelete);
                    if (entry2ToDelete != null) {
                        try {
                            LOG.info(LOG_DELETING_INDEX, entry2ToDelete.getName());
                            response = this.dbClient.deleteTable(entry2ToDelete.getName());
                            LOG.info(response ? "succeeded" : "failed");
                        } catch (SQLException e) {
                            LOG.error(e.getMessage());
                            return false;
                        }
                    }
                }
            }
        }
        if (entries2 == null) {
            return false;
        }
        if (entries2.isEmpty()) {
            LOG.info("no indices to clear");
        } else {
            //check for every component of release if index exists
            for (ComponentName component : ri.getComponents()) {
                String indexToDelete = ri.getIndex(component, dbPrefix);
                IndicesEntry entryToDelete = entries2.findByIndex(indexToDelete);
                if (entryToDelete != null) {
                    try {
                        LOG.info(LOG_DELETING_INDEX, entryToDelete.getName());
                        response = this.dbClient.deleteTable(entryToDelete.getName());
                        LOG.info(response ? "succeeded" : "failed");
                    } catch (SQLException e) {
                        LOG.error(e.getMessage());
                        return false;
                    }
                }
            }
        }

        return true;
    }

    /**
     * @param timeoutms
     * @return
     */
    public boolean clearCompleteDatabase(long timeoutms) {
        if (timeoutms > 0) {
            this.dbClient.waitForYellowStatus(timeoutms);
        }
        //check aliases and indices
        AliasesEntryList aliases = this.dbClient.readViews();
        IndicesEntryList indices = this.dbClient.readTables();
        if (aliases == null || indices == null) {
            return false;
        }
        for (AliasesEntry alias : aliases) {
            try {
                LOG.info("deleting alias {} for index {}", alias.getAlias(), alias.getIndex());
                this.dbClient.deleteView(alias.getAlias());
            } catch (SQLException e) {
                LOG.error("problem deleting alias {}: {}", alias.getAlias(), e);
                return false;
            }
        }
        for (IndicesEntry index : indices) {
            try {
                LOG.info(LOG_DELETING_INDEX, index.getName());
                this.dbClient.deleteTable(index.getName());
            } catch (SQLException e) {
                LOG.error("problem deleting index {}: {}", index.getName(), e);
                return false;
            }
        }
        return true;
    }

}