4782f330531fe372c690371e25383ed229bd4967
[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.performancemanager.impl.database.service;
19
20 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database.HtDataBaseReaderAndWriter;
21 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database.HtDatabaseClientAbstract;
22 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database.HtDatabaseNode;
23 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database.IndexClientBuilder;
24 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.netconf.container.AllPm;
25 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.netconf.util.NetconfTimeStamp;
26 import org.onap.ccsdk.features.sdnr.wt.devicemanager.performancemanager.impl.database.types.EsHistoricalPerformance15Minutes;
27 import org.onap.ccsdk.features.sdnr.wt.devicemanager.performancemanager.impl.database.types.EsHistoricalPerformance24Hours;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.performancemanager.impl.database.types.EsHistoricalPerformanceLogEntry;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class MicrowaveHistoricalPerformanceWriterService {
33
34     private static final Logger LOG = LoggerFactory.getLogger(MicrowaveHistoricalPerformanceWriterService.class);
35     private static final String INDEX = "sdnperformance";
36     private static final String MAPPING = "/elasticsearch/index/sdnperformance/sdnperformanceMapping.json";
37     private static final NetconfTimeStamp NETCONFTIME_CONVERTER = NetconfTimeStamp.getConverter();
38
39
40     private HtDatabaseClientAbstract client;
41     private HtDataBaseReaderAndWriter<EsHistoricalPerformance15Minutes> historicalPerformance15mRW;
42     private HtDataBaseReaderAndWriter<EsHistoricalPerformance24Hours> historicalPerformance24hRW;
43     private HtDataBaseReaderAndWriter<EsHistoricalPerformanceLogEntry> historicalPerformanceLogRW;
44
45     public MicrowaveHistoricalPerformanceWriterService(HtDatabaseNode database) {
46
47         LOG.info("Create {} start", MicrowaveHistoricalPerformanceWriterService.class);
48
49         try {
50
51             IndexClientBuilder clientBuilder =
52                     IndexClientBuilder.getBuilder(INDEX).setMappingSettingJsonFileName(MAPPING);
53             client = clientBuilder.create(database);
54             clientBuilder.close();
55
56             historicalPerformance15mRW = new HtDataBaseReaderAndWriter<>(client,
57                     EsHistoricalPerformance15Minutes.ESDATATYPENAME, EsHistoricalPerformance15Minutes.class);
58             historicalPerformance24hRW = new HtDataBaseReaderAndWriter<>(client,
59                     EsHistoricalPerformance24Hours.ESDATATYPENAME, EsHistoricalPerformance24Hours.class);
60             historicalPerformanceLogRW = new HtDataBaseReaderAndWriter<>(client,
61                     EsHistoricalPerformanceLogEntry.ESDATATYPENAME, EsHistoricalPerformanceLogEntry.class);
62
63         } catch (Exception e) {
64             client = null;
65             LOG.error("Can not start database client. Exception: {}", e.getMessage());
66         }
67
68         LOG.info("Create {} finished. DB Service {} started.", MicrowaveHistoricalPerformanceWriterService.class,
69                 client != null ? "sucessfully" : "not");
70     }
71
72
73     public void writePM(AllPm pm) {
74
75         LOG.debug("Write {} pm records", pm.size());
76
77         LOG.debug("Write 15m write to DB");
78         historicalPerformance15mRW.doWrite(pm.getPm15());
79         LOG.debug("Write 15m done, Write 24h write to DB");
80         historicalPerformance24hRW.doWrite(pm.getPm24());
81         LOG.debug("Write 24h done");
82
83     }
84
85     public void writePMLog(String mountpointName, String layerProtocolName, String msg) {
86
87         LOG.debug("Write PM Log: {}", msg);
88         EsHistoricalPerformanceLogEntry logEntry = new EsHistoricalPerformanceLogEntry(mountpointName,
89                 layerProtocolName, NETCONFTIME_CONVERTER.getTimeStamp().getValue(), msg);
90         historicalPerformanceLogRW.doWrite(logEntry);
91         LOG.debug("Write PM Log done");
92
93     }
94
95
96     static public boolean isAvailable(MicrowaveHistoricalPerformanceWriterService s) {
97
98         if (s == null || s.client == null) {
99             return false;
100         }
101         return true;
102     }
103
104 }