e3afea7a26016e8212e0a182d54c72b0b5efe179
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / devicemanager / impl / src / main / java / org / opendaylight / mwtn / devicemanager / impl / database / service / HtDatabaseEventsService.java
1 /*********************************************************************************
2  *  Copyright © 2016, highstreet technologies GmbH
3  *  All rights reserved!
4  *
5  *  http://www.highstreet-technologies.com/
6  *
7  *  The reproduction, transmission or use of this document or its contents is not
8  *  permitted without express written authority. Offenders will be liable for
9  *  damages. All rights, including rights created by patent grant or registration
10  *  of a utility model or design, are reserved. Technical modifications possible.
11  *  Technical specifications and features are binding only insofar as they are
12  *  specifically and expressly agreed upon in a written contract.
13  *
14  *  @author: Herbert Eiselt [herbert.eiselt@highstreet-technologies.com]
15  *********************************************************************************/
16
17 package org.opendaylight.mwtn.devicemanager.impl.database.service;
18
19 import org.opendaylight.mwtn.base.database.HtDataBaseReaderAndWriter;
20 import org.opendaylight.mwtn.base.database.HtDatabaseClientAbstract;
21 import org.opendaylight.mwtn.base.database.HtDatabaseNode;
22 import org.opendaylight.mwtn.base.database.IndexClientBuilder;
23 import org.opendaylight.mwtn.devicemanager.impl.database.types.EsEventBase;
24 import org.opendaylight.mwtn.devicemanager.impl.database.types.EsFaultCurrent;
25 import org.opendaylight.mwtn.devicemanager.impl.database.types.EsFaultLog;
26 import org.opendaylight.mwtn.devicemanager.impl.xml.AttributeValueChangedNotificationXml;
27 import org.opendaylight.mwtn.devicemanager.impl.xml.MwtNotificationBase;
28 import org.opendaylight.mwtn.devicemanager.impl.xml.ObjectCreationNotificationXml;
29 import org.opendaylight.mwtn.devicemanager.impl.xml.ObjectDeletionNotificationXml;
30 import org.opendaylight.mwtn.devicemanager.impl.xml.ProblemNotificationXml;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * Event service, writing all events into the database into the appropriate index.
36  *
37  * @author herbert
38  */
39 public class HtDatabaseEventsService {
40     private static final Logger LOG = LoggerFactory.getLogger(HtDatabaseEventsService.class);
41
42     ///** Filename in the resources with maven initialized version information  */
43     //private static final String RESOURCENAME = "version.properties"; // could also be a constant
44     ///** Index name to be used */
45     private static final String INDEX = "sdnevents";
46     private static final String MAPPING = "/elasticsearch/index/sdnevents/sdneventsMapping.json";
47
48     private HtDatabaseClientAbstract client;
49     private HtDataBaseReaderAndWriter<EsEventBase> eventRWEventLog;
50     private HtDataBaseReaderAndWriter<EsFaultCurrent> eventRWFaultCurrent;
51     private HtDataBaseReaderAndWriter<EsFaultLog> eventRWFaultLog;
52
53
54     // --- Construct and initialize
55
56     public HtDatabaseEventsService(HtDatabaseNode database) {
57
58         LOG.info("Create {} start", HtDatabaseEventsService.class);
59
60         try {
61                 // Create control structure
62                 IndexClientBuilder clientBuilder = IndexClientBuilder.getBuilder(INDEX).setMappingSettingJsonFileName(MAPPING);
63                 client = clientBuilder.create(database);
64
65                 eventRWEventLog = new HtDataBaseReaderAndWriter<>(client, EsEventBase.ESDATATYPENAME, EsEventBase.class);
66                 eventRWFaultLog = new HtDataBaseReaderAndWriter<>(client, EsFaultLog.ESDATATYPENAME, EsFaultLog.class);
67                 eventRWFaultCurrent = new HtDataBaseReaderAndWriter<>(client, EsFaultCurrent.ESDATATYPENAME, EsFaultCurrent.class);
68
69
70         } catch (Exception e) {
71                 LOG.error("Can not start database client. Exception: {}", e.getMessage());
72         }
73         LOG.info("Create {} finished. DB Service {} started.", HtDatabaseEventsService.class,  client != null ? "sucessfully" : "not" );
74     }
75
76     // --- Function
77
78     public void writeEventLog(ObjectCreationNotificationXml event) {
79         writeEventGeneric(event);
80     }
81
82     public void writeEventLog(ObjectDeletionNotificationXml event) {
83         writeEventGeneric(event);
84     }
85
86     public void writeEventLog(AttributeValueChangedNotificationXml event) {
87         writeEventGeneric(event);
88     }
89
90     private void writeEventGeneric(MwtNotificationBase event) {
91         if (client == null) {
92             LOG.debug("No DB, can not write: {}",event.toString());
93             return;
94         }
95
96         LOG.debug("Write event: {}",event.toString());
97         EsEventBase eventBase = new EsEventBase();
98         eventBase.setProblem(event);
99         eventRWEventLog.doWrite(eventBase);
100     }
101
102     public void writeFaultLog(ProblemNotificationXml fault) {
103         if (client == null) {
104             LOG.debug("No DB, can not write: {}",fault.toString());
105             return;
106         }
107
108         LOG.debug("Write fault to faultlog: {}",fault.toString());
109         EsFaultLog eventProblem = new EsFaultLog();
110         eventProblem.setProblem(fault);
111         eventRWFaultLog.doWrite(eventProblem);
112     }
113
114     public void updateFaultCurrent(ProblemNotificationXml fault) {
115         if (client == null) {
116             LOG.debug("No DB, can not write: {}",fault.toString());
117             return;
118         }
119
120         EsFaultCurrent eventProblem = new EsFaultCurrent();
121         eventProblem.setProblem(fault);
122
123         if (eventProblem.isNoAlarmIndication()) {
124             LOG.debug("Remove fault from currentlog: {}",fault.toString());
125             eventRWFaultCurrent.doRemove(eventProblem);
126         } else {
127             LOG.debug("Write fault to currentlog: {}",fault.toString());
128             eventRWFaultCurrent.doWrite(eventProblem);
129         }
130     }
131
132     /**
133      * Remove all entries for one node
134      * @param nodeName contains the mountpointname
135      * @return number of deleted entries
136      */
137     public int clearFaultsCurrentOfNode(String nodeName) {
138         if (client == null) {
139             LOG.debug("No DB, can not delete for node: {}", nodeName);
140             return -1;
141         }
142         LOG.debug("Remove from currentlog all faults for node: {}", nodeName);
143         return eventRWFaultCurrent.doRemoveByQuery(EsFaultCurrent.getQueryForOneNode(nodeName));
144         /*
145         List<EsFaultCurrent> faults = eventRWFaultCurrent.doReadAll();
146         for (EsFaultCurrent fault : faults) {
147             if (fault.getProblem().getNodeName().equals(nodeName)) {
148                 eventRWFaultCurrent.doRemove(fault);
149             }
150         }
151         return faults.size();
152         */
153     }
154
155     /**
156      * Remove all entries for one node
157      * @param nodeName contains the mountpointname
158      * @param objectId of element to be deleted
159      * @return number of deleted entries
160      */
161     public int clearFaultsCurrentOfNodeWithObjectId(String nodeName, String objectId) {
162         if (client == null) {
163             LOG.debug("No DB, can not delete for node: {}", nodeName);
164             return -1;
165         }
166         LOG.debug("Remove from currentlog all faults for node/objectId: {}/{}", nodeName, objectId);
167         return eventRWFaultCurrent.doRemoveByQuery(EsFaultCurrent.getQueryForOneNodeAndObjectId(nodeName, objectId));
168
169     }
170
171
172     /*private void writeVersionInfo(HtDataBaseReaderAndWriter<EsVersionInfo> pEventRWVersionInfo,
173             String configurationId) {
174
175         ClassLoader loader = Thread.currentThread().getContextClassLoader();
176         Properties props = new Properties();
177
178         InputStream resourceStream = loader.getResourceAsStream(RESOURCENAME);
179         try {
180             props.load(resourceStream);
181
182         } catch (IOException e1) {
183         }
184         finally {
185                 try {
186                         if(resourceStream!=null)
187                                 resourceStream.close();
188                         } catch (IOException e) {
189                                 LOG.debug("failed to close stream");
190                         }
191                 }
192
193         EsVersionInfo version = new EsVersionInfo();
194         version.setEsId(configurationId);
195         version.setBundleVersion(FrameworkUtil.getBundle(getClass()).getVersion());
196         version.setVersion(props.getProperty("version", "unknown"));
197         version.setBuild(props.getProperty("build", "unknown"));
198
199         LOG.info("Versioninfo: {}",version );
200
201         pEventRWVersionInfo.doWrite(version);
202     }*/
203
204 }