Release version 1.1.0 of sli/northbound
[ccsdk/sli/northbound.git] / dmaap-listener / src / main / java / org / onap / ccsdk / sli / northbound / dmaapclient / SdncDhcpEventConsumer.java
1 package org.onap.ccsdk.sli.northbound.dmaapclient;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.sql.SQLException;
8 import java.util.Properties;
9 import org.onap.ccsdk.sli.core.dblib.DBResourceManager;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12 import com.fasterxml.jackson.databind.JsonNode;
13 import com.fasterxml.jackson.databind.ObjectMapper;
14
15 public class SdncDhcpEventConsumer extends SdncDmaapConsumerImpl {
16         private static final Logger LOG = LoggerFactory.getLogger(SdncDhcpEventConsumer.class);
17
18         private static final String MAC_ADDR_TAG = "macaddr";
19         private static final String MSG_NAME_TAG = "msg_name";
20         private static final String IP_ADDR_TAG = "yiaddr";
21
22         private static DBResourceManager jdbcDataSource = null;
23         private static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR";
24
25         private class MissingDhcpAttributeException extends InvalidMessageException {
26
27                 public MissingDhcpAttributeException(String fieldName) {
28                         super("Invalid DHCP event - missing " + fieldName + " attribute");
29                 }
30         }
31
32         private static void setJdbcDataSource() throws IOException {
33
34                 String propPath;
35                 String propDir = System.getenv(SDNC_CONFIG_DIR);
36                 if (propDir == null) {
37                         propDir = "/opt/onap/sdnc/data/properties";
38                 }
39                 propPath = propDir + "/dblib.properties";
40                 File propFile = new File(propPath);
41
42                 if (!propFile.exists()) {
43
44                         throw new FileNotFoundException("Missing configuration properties file : " + propFile);
45                 }
46
47                 Properties props = new Properties();
48                 props.load(new FileInputStream(propFile));
49
50                 setJdbcDataSource(new DBResourceManager(props));
51
52         }
53
54         static void setJdbcDataSource(DBResourceManager dbMgr) {
55
56                 jdbcDataSource = dbMgr;
57
58                 if (jdbcDataSource.isActive()) {
59                         LOG.warn("DBLIB: JDBC DataSource has been initialized.");
60                 } else {
61                         LOG.warn("DBLIB: JDBC DataSource did not initialize successfully.");
62                 }
63         }
64
65         @Override
66         public void processMsg(String msg) throws InvalidMessageException {
67                 if (msg == null) {
68                         throw new InvalidMessageException("Null message");
69                 }
70
71                 ObjectMapper oMapper = new ObjectMapper();
72
73                 JsonNode dhcpRootNode;
74                 String msgName;
75                 String macAddr;
76                 String ipAddr;
77
78                 try {
79                         dhcpRootNode = oMapper.readTree(msg);
80
81                 } catch (IOException e) {
82                         throw new InvalidMessageException("Cannot parse json object", e);
83                 }
84
85                 JsonNode msgNameNode = dhcpRootNode.get(MSG_NAME_TAG);
86                 if (msgNameNode != null) {
87                         msgName = msgNameNode.textValue();
88
89                 } else {
90                         throw new MissingDhcpAttributeException(MSG_NAME_TAG);
91                 }
92
93                 JsonNode macAddrNode = dhcpRootNode.get(MAC_ADDR_TAG);
94                 if (macAddrNode != null) {
95                         macAddr = macAddrNode.textValue();
96
97                 } else {
98                         throw new MissingDhcpAttributeException(MAC_ADDR_TAG);
99                 }
100
101                 JsonNode ipAddrNode = dhcpRootNode.get(IP_ADDR_TAG);
102                 if (ipAddrNode != null) {
103                         ipAddr = ipAddrNode.textValue();
104
105                 } else {
106                         throw new MissingDhcpAttributeException(IP_ADDR_TAG);
107                 }
108
109                 LOG.debug("Got DHCP event : msg name {}; mac addr {}; ip addr {}", msgName, macAddr, ipAddr);
110
111                 if (jdbcDataSource == null) {
112                         try {
113                                 setJdbcDataSource();
114                         } catch (IOException e) {
115                                 LOG.error("Could not create JDBC connection", e);
116                                 return;
117                         }
118                 }
119
120                 try {
121
122                         jdbcDataSource.writeData("INSERT INTO DHCP_MAP(mac_addr, ip_addr) VALUES('" + macAddr + "','" + ipAddr + "') ON DUPLICATE KEY UPDATE ip_addr = '"+ipAddr+"'", null, null);
123
124                 } catch (SQLException e) {
125                         LOG.error("Could not insert DHCP event data into the database ", e);
126                 }
127
128         }
129
130 }