4f346975945ec5b1fa70cd41cb1f92a3b56dbf38
[ccsdk/features.git] /
1 /*******************************************************************************
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.index.impl;
19
20 import java.util.Date;
21 import org.elasticsearch.index.query.QueryBuilder;
22 import org.onap.ccsdk.features.sdnr.wt.devicemanager.archiveservice.ArchiveCleanProvider;
23 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database.HtDataBaseReaderAndWriter;
24 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database.HtDatabaseClientAbstract;
25 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database.HtDatabaseNode;
26 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database.IndexClientBuilder;
27 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.netconf.util.NetconfTimeStamp;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.index.database.types.EsEventOdluxLog;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Setup index mwtn in the database
34  * @author herbert
35  *
36  */
37 public class IndexMwtnService implements AutoCloseable, ArchiveCleanProvider {
38
39     private static final Logger LOG = LoggerFactory.getLogger(IndexMwtnService.class);
40     private static final NetconfTimeStamp NETCONFTIME_CONVERTER = NetconfTimeStamp.getConverter();
41
42     /** Index name to be used */
43     public static final String INDEX = "mwtn";
44     /** Location of mapping data **/
45     private static final String MAPPING = "elasticsearch/index/mwtn/mwtnMapping.json";
46     /** Location of configuration data **/
47
48     private final HtDataBaseReaderAndWriter<EsEventOdluxLog> eventRWOdluxLog;
49
50
51     private final HtDatabaseClientAbstract client;
52
53     // --- Construct and initialize
54
55     public IndexMwtnService(HtDatabaseNode database) throws Exception {
56         LOG.info("Create {} start", this.getClass().getSimpleName());
57
58
59         IndexClientBuilder clientBuilder = IndexClientBuilder.getBuilder(INDEX)
60                 .setMappingSettingJsonFileName(MAPPING);
61         client = clientBuilder.create(database);
62         clientBuilder.close();
63
64         eventRWOdluxLog = new HtDataBaseReaderAndWriter<>(client, EsEventOdluxLog.ESDATATYPENAME, EsEventOdluxLog.class);
65
66         LOG.info("Create {} finished. DB Service sucessfully started.", this.getClass().getSimpleName());
67     }
68
69     /**
70      * Get client to be used in other services
71      * @return client
72      */
73     public HtDatabaseClientAbstract getClient() {
74         return client;
75     }
76
77     /**
78      * Write into Odlux log, used by client
79      * @param logEntry as test data
80      */
81     public void writeOdluxEventForTestpurpose(EsEventOdluxLog logEntry) {
82         eventRWOdluxLog.doWrite(logEntry);
83     }
84
85
86     @Override
87     public void close() throws Exception {
88         if (client != null) {
89             client.close();
90         }
91     }
92
93     @Override
94     public int doIndexClean(Date olderAreOutdated) {
95
96         String netconfTimeStamp = NETCONFTIME_CONVERTER.getTimeStampAsNetconfString(olderAreOutdated);
97
98         QueryBuilder queryOdluxLog = EsEventOdluxLog.getQueryForTimeStamp(netconfTimeStamp);
99         int removed = eventRWOdluxLog.doRemoveByQuery(queryOdluxLog);
100         return removed;
101
102     }
103
104     @Override
105     public int getNumberOfOldObjects(Date olderAreOutdated) {
106
107         String netconfTimeStamp = NETCONFTIME_CONVERTER.getTimeStampAsNetconfString(olderAreOutdated);
108         int numberOfElements = 0;
109
110         QueryBuilder queryOdluxLog = EsEventOdluxLog.getQueryForTimeStamp(netconfTimeStamp);
111         numberOfElements += eventRWOdluxLog.doReadAll(queryOdluxLog).size();
112
113         return numberOfElements;
114     }
115 }