0e6b03291048f5426247a65fa631403d5c33d100
[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     private static final String MODELDATA = "elasticsearch/index/mwtn/modelDescription";
48
49     private final HtDataBaseReaderAndWriter<EsEventOdluxLog> eventRWOdluxLog;
50
51
52     private final HtDatabaseClientAbstract client;
53
54     // --- Construct and initialize
55
56     public IndexMwtnService(HtDatabaseNode database) throws Exception {
57         LOG.info("Create {} start", this.getClass().getSimpleName());
58
59
60         IndexClientBuilder clientBuilder = IndexClientBuilder.getBuilder(INDEX)
61                 .setMappingSettingJsonFileName(MAPPING)
62                 .setModelDataDirectory(MODELDATA);
63         client = clientBuilder.create(database);
64         clientBuilder.close();
65
66         eventRWOdluxLog = new HtDataBaseReaderAndWriter<>(client, EsEventOdluxLog.ESDATATYPENAME, EsEventOdluxLog.class);
67
68         LOG.info("Create {} finished. DB Service sucessfully started.", this.getClass().getSimpleName());
69     }
70
71     /**
72      * Get client to be used in other services
73      * @return client
74      */
75     public HtDatabaseClientAbstract getClient() {
76         return client;
77     }
78
79     /**
80      * Write into Odlux log, used by client
81      * @param logEntry as test data
82      */
83     public void writeOdluxEventForTestpurpose(EsEventOdluxLog logEntry) {
84         eventRWOdluxLog.doWrite(logEntry);
85     }
86
87
88     @Override
89     public void close() throws Exception {
90         if (client != null) {
91             client.close();
92         }
93     }
94
95     @Override
96     public int doIndexClean(Date olderAreOutdated) {
97
98         String netconfTimeStamp = NETCONFTIME_CONVERTER.getTimeStampAsNetconfString(olderAreOutdated);
99
100         QueryBuilder queryOdluxLog = EsEventOdluxLog.getQueryForTimeStamp(netconfTimeStamp);
101         int removed = eventRWOdluxLog.doRemoveByQuery(queryOdluxLog);
102         return removed;
103
104     }
105
106     @Override
107     public int getNumberOfOldObjects(Date olderAreOutdated) {
108
109         String netconfTimeStamp = NETCONFTIME_CONVERTER.getTimeStampAsNetconfString(olderAreOutdated);
110         int numberOfElements = 0;
111
112         QueryBuilder queryOdluxLog = EsEventOdluxLog.getQueryForTimeStamp(netconfTimeStamp);
113         numberOfElements += eventRWOdluxLog.doReadAll(queryOdluxLog).size();
114
115         return numberOfElements;
116     }
117 }