summaryrefslogtreecommitdiffstats
path: root/dmaap-listener/src/main/java/org/onap/ccsdk/sli/northbound/dmaapclient/SdncDhcpEventConsumer.java
blob: 7b68ceb638ade7397c3c5185eaa7d5fcb129f7d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package org.onap.ccsdk.sli.northbound.dmaapclient;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Properties;
import org.onap.ccsdk.sli.core.dblib.DBResourceManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class SdncDhcpEventConsumer extends SdncDmaapConsumerImpl {
	private static final Logger LOG = LoggerFactory.getLogger(SdncDhcpEventConsumer.class);

	private static final String MAC_ADDR_TAG = "macaddr";
	private static final String MSG_NAME_TAG = "msg_name";
	private static final String IP_ADDR_TAG = "yiaddr";

	private static DBResourceManager jdbcDataSource = null;
	private static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR";

	private class MissingDhcpAttributeException extends InvalidMessageException {

		public MissingDhcpAttributeException(String fieldName) {
			super("Invalid DHCP event - missing " + fieldName + " attribute");
		}
	}

	private static void setJdbcDataSource() throws IOException {

		String propPath;
		String propDir = System.getenv(SDNC_CONFIG_DIR);
		if (propDir == null) {
			propDir = "/opt/onap/sdnc/data/properties";
		}
		propPath = propDir + "/dblib.properties";
		File propFile = new File(propPath);

		if (!propFile.exists()) {

			throw new FileNotFoundException("Missing configuration properties file : " + propFile);
		}

		Properties props = new Properties();
		props.load(new FileInputStream(propFile));

		setJdbcDataSource(new DBResourceManager(props));

	}

	static void setJdbcDataSource(DBResourceManager dbMgr) {

		jdbcDataSource = dbMgr;

		if (jdbcDataSource.isActive()) {
			LOG.warn("DBLIB: JDBC DataSource has been initialized.");
		} else {
			LOG.warn("DBLIB: JDBC DataSource did not initialize successfully.");
		}
	}

	@Override
	public void processMsg(String msg) throws InvalidMessageException {
		if (msg == null) {
			throw new InvalidMessageException("Null message");
		}

		ObjectMapper oMapper = new ObjectMapper();

		JsonNode dhcpRootNode;
		String msgName;
		String macAddr;
		String ipAddr;

		try {
			dhcpRootNode = oMapper.readTree(msg);

		} catch (IOException e) {
			throw new InvalidMessageException("Cannot parse json object", e);
		}

		JsonNode msgNameNode = dhcpRootNode.get(MSG_NAME_TAG);
		if (msgNameNode != null) {
			msgName = msgNameNode.textValue();

		} else {
			throw new MissingDhcpAttributeException(MSG_NAME_TAG);
		}

		JsonNode macAddrNode = dhcpRootNode.get(MAC_ADDR_TAG);
		if (macAddrNode != null) {
			macAddr = macAddrNode.textValue();

		} else {
			throw new MissingDhcpAttributeException(MAC_ADDR_TAG);
		}

		JsonNode ipAddrNode = dhcpRootNode.get(IP_ADDR_TAG);
		if (ipAddrNode != null) {
			ipAddr = ipAddrNode.textValue();

		} else {
			throw new MissingDhcpAttributeException(IP_ADDR_TAG);
		}

		LOG.debug("Got DHCP event : msg name {}; mac addr {}; ip addr {}", msgName, macAddr, ipAddr);

		if (jdbcDataSource == null) {
			try {
				setJdbcDataSource();
			} catch (IOException e) {
				LOG.error("Could not create JDBC connection", e);
				return;
			}
		}

		try {

			jdbcDataSource.writeData("INSERT INTO DHCP_MAP(mac_addr, ip_addr) VALUES('" + macAddr + "','" + ipAddr + "') ON DUPLICATE KEY UPDATE ip_addr = '"+ipAddr+"'", null, null);

		} catch (SQLException e) {
			LOG.error("Could not insert DHCP event data into the database ", e);
		}

	}

}