1 package org.onap.ccsdk.sli.northbound.dmaapclient;
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;
15 public class SdncDhcpEventConsumer extends SdncDmaapConsumerImpl {
16 private static final Logger LOG = LoggerFactory.getLogger(SdncDhcpEventConsumer.class);
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";
22 private static DBResourceManager jdbcDataSource = null;
23 private static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR";
25 private class MissingDhcpAttributeException extends InvalidMessageException {
27 public MissingDhcpAttributeException(String fieldName) {
28 super("Invalid DHCP event - missing " + fieldName + " attribute");
32 private static void setJdbcDataSource() throws IOException {
35 String propDir = System.getenv(SDNC_CONFIG_DIR);
36 if (propDir == null) {
37 propDir = "/opt/onap/sdnc/data/properties";
39 propPath = propDir + "/dblib.properties";
40 File propFile = new File(propPath);
42 if (!propFile.exists()) {
44 throw new FileNotFoundException("Missing configuration properties file : " + propFile);
47 Properties props = new Properties();
48 props.load(new FileInputStream(propFile));
50 setJdbcDataSource(new DBResourceManager(props));
54 static void setJdbcDataSource(DBResourceManager dbMgr) {
56 jdbcDataSource = dbMgr;
58 if (jdbcDataSource.isActive()) {
59 LOG.warn("DBLIB: JDBC DataSource has been initialized.");
61 LOG.warn("DBLIB: JDBC DataSource did not initialize successfully.");
66 public void processMsg(String msg) throws InvalidMessageException {
68 throw new InvalidMessageException("Null message");
71 ObjectMapper oMapper = new ObjectMapper();
73 JsonNode dhcpRootNode;
79 dhcpRootNode = oMapper.readTree(msg);
81 } catch (IOException e) {
82 throw new InvalidMessageException("Cannot parse json object", e);
85 JsonNode msgNameNode = dhcpRootNode.get(MSG_NAME_TAG);
86 if (msgNameNode != null) {
87 msgName = msgNameNode.textValue();
90 throw new MissingDhcpAttributeException(MSG_NAME_TAG);
93 JsonNode macAddrNode = dhcpRootNode.get(MAC_ADDR_TAG);
94 if (macAddrNode != null) {
95 macAddr = macAddrNode.textValue();
98 throw new MissingDhcpAttributeException(MAC_ADDR_TAG);
101 JsonNode ipAddrNode = dhcpRootNode.get(IP_ADDR_TAG);
102 if (ipAddrNode != null) {
103 ipAddr = ipAddrNode.textValue();
106 throw new MissingDhcpAttributeException(IP_ADDR_TAG);
109 LOG.debug("Got DHCP event : msg name {}; mac addr {}; ip addr {}", msgName, macAddr, ipAddr);
111 if (jdbcDataSource == null) {
114 } catch (IOException e) {
115 LOG.error("Could not create JDBC connection", e);
122 jdbcDataSource.writeData("INSERT INTO DHCP_MAP(mac_addr, ip_addr) VALUES('" + macAddr + "','" + ipAddr + "') ON DUPLICATE KEY UPDATE ip_addr = '"+ipAddr+"'", null, null);
124 } catch (SQLException e) {
125 LOG.error("Could not insert DHCP event data into the database ", e);