daef26dadd81869ddd0d053e84719e294a336239
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / devicemanager / impl / src / main / java / org / opendaylight / mwtn / performancemanager / impl / database / service / MicrowaveHistoricalPerformanceWriterService.java
1 package org.opendaylight.mwtn.performancemanager.impl.database.service;
2
3 import org.opendaylight.mwtn.base.database.HtDataBaseReaderAndWriter;
4 import org.opendaylight.mwtn.base.database.HtDatabaseClientAbstract;
5 import org.opendaylight.mwtn.base.database.HtDatabaseNode;
6 import org.opendaylight.mwtn.base.database.IndexClientBuilder;
7 import org.opendaylight.mwtn.base.netconf.AllPm;
8 import org.opendaylight.mwtn.base.netconf.NetconfTimeStamp;
9 import org.opendaylight.mwtn.performancemanager.impl.database.types.EsHistoricalPerformance15Minutes;
10 import org.opendaylight.mwtn.performancemanager.impl.database.types.EsHistoricalPerformance24Hours;
11 import org.opendaylight.mwtn.performancemanager.impl.database.types.EsHistoricalPerformanceLogEntry;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 public class MicrowaveHistoricalPerformanceWriterService {
16
17     private static final Logger LOG = LoggerFactory.getLogger(MicrowaveHistoricalPerformanceWriterService.class);
18     private static final String INDEX = "sdnperformance";
19     private static final String MAPPING = "/elasticsearch/index/sdnperformance/sdnperformanceMapping.json";
20
21
22     private HtDatabaseClientAbstract client;
23     private HtDataBaseReaderAndWriter<EsHistoricalPerformance15Minutes> historicalPerformance15mRW;
24     private HtDataBaseReaderAndWriter<EsHistoricalPerformance24Hours> historicalPerformance24hRW;
25     private HtDataBaseReaderAndWriter<EsHistoricalPerformanceLogEntry> historicalPerformanceLogRW;
26
27     public MicrowaveHistoricalPerformanceWriterService(HtDatabaseNode database) {
28
29         LOG.info("Create {} start", MicrowaveHistoricalPerformanceWriterService.class);
30
31         try {
32
33                 IndexClientBuilder clientBuilder = IndexClientBuilder.getBuilder(INDEX).setMappingSettingJsonFileName(MAPPING);
34                 client = clientBuilder.create(database);
35
36                 historicalPerformance15mRW = new HtDataBaseReaderAndWriter<>(client, EsHistoricalPerformance15Minutes.ESDATATYPENAME, EsHistoricalPerformance15Minutes.class);
37                 historicalPerformance24hRW = new HtDataBaseReaderAndWriter<>(client, EsHistoricalPerformance24Hours.ESDATATYPENAME, EsHistoricalPerformance24Hours.class);
38                 historicalPerformanceLogRW = new HtDataBaseReaderAndWriter<>(client, EsHistoricalPerformanceLogEntry.ESDATATYPENAME, EsHistoricalPerformanceLogEntry.class);
39
40         } catch (Exception e) {
41                 client = null;
42                 LOG.error("Can not start database client. Exception: {}", e.getMessage());
43         }
44
45         LOG.info("Create {} finished. DB Service {} started.", MicrowaveHistoricalPerformanceWriterService.class,  client != null ? "sucessfully" : "not" );
46     }
47
48
49     public void writePM(AllPm pm) {
50
51         LOG.debug("Write {} pm records", pm.size());
52
53         LOG.debug("Write 15m write to DB");
54         historicalPerformance15mRW.doWrite(pm.getPm15());
55         LOG.debug("Write 15m done, Write 24h write to DB");
56         historicalPerformance24hRW.doWrite(pm.getPm24());
57         LOG.debug("Write 24h done");
58
59     }
60
61     public void writePMLog(String mountpointName, String layerProtocolName, String msg) {
62
63         LOG.debug("Write PM Log: {}", msg);
64         EsHistoricalPerformanceLogEntry logEntry = new EsHistoricalPerformanceLogEntry(mountpointName,layerProtocolName,NetconfTimeStamp.getTimeStamp().getValue() , msg );
65         historicalPerformanceLogRW.doWrite(logEntry);
66         LOG.debug("Write PM Log done");
67
68     }
69
70
71     static public boolean isAvailable(MicrowaveHistoricalPerformanceWriterService s) {
72
73         if (s == null || s.client == null) {
74             return false;
75         }
76         return true;
77     }
78
79 }