Add Dhcp event listener 77/25877/2
authorDan Timoney <dtimoney@att.com>
Fri, 8 Dec 2017 15:32:26 +0000 (10:32 -0500)
committerDan Timoney <dtimoney@att.com>
Fri, 8 Dec 2017 20:01:58 +0000 (15:01 -0500)
Add missing DHCP event listener class

Change-Id: I882e40ffc25c560742b1d093b9a484596cde8ee8
Issue-ID: CCSDK-142
Signed-off-by: Dan Timoney <dtimoney@att.com>
dmaap-listener/pom.xml
dmaap-listener/src/main/java/org/onap/ccsdk/sli/northbound/dmaapclient/SdncDhcpEventConsumer.java [new file with mode: 0644]
dmaap-listener/src/main/java/org/onap/ccsdk/sli/northbound/dmaapclient/SdncDmaapConsumer.java
dmaap-listener/src/test/java/org/onap/ccsdk/sli/northbound/dmaapclient/TestSdncDhcpEventConsumer.java [new file with mode: 0644]
dmaap-listener/src/test/resources/dblib.properties [new file with mode: 0644]
dmaap-listener/src/test/resources/log4j.properties [new file with mode: 0644]

index e60f180..acc0948 100755 (executable)
@@ -1,7 +1,8 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
-        <groupId>org.onap.ccsdk.sli.northbound</groupId>
+       <groupId>org.onap.ccsdk.sli.northbound</groupId>
        <packaging>jar</packaging>
        <artifactId>dmaap-listener</artifactId>
        <version>0.1.1-SNAPSHOT</version>
                <dependency>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-api</artifactId>
-                       <version>1.7.10</version>
+                       <version>${slf4j.version}</version>
+               </dependency>
+               <dependency>
+                       <groupId>org.slf4j</groupId>
+                       <artifactId>jcl-over-slf4j</artifactId>
+                       <version>${slf4j.version}</version>
                </dependency>
                <dependency>
                        <groupId>org.slf4j</groupId>
                        <artifactId>jackson-databind</artifactId>
                        <version>${fasterxml.jackson.version}</version>
                </dependency>
-               <dependency>
-                        <groupId>com.fasterxml.jackson.core</groupId>
-                        <artifactId>jackson-annotations</artifactId>
-                        <version>${fasterxml.jackson.version}</version>
-                </dependency>
+               <dependency>
+                       <groupId>com.fasterxml.jackson.core</groupId>
+                       <artifactId>jackson-annotations</artifactId>
+                       <version>${fasterxml.jackson.version}</version>
+               </dependency>
                <dependency>
                        <groupId>junit</groupId>
                        <artifactId>junit</artifactId>
                        <version>${junit.version}</version>
                        <scope>test</scope>
                </dependency>
-               <dependency>
+               <dependency>
+                       <groupId>ch.vorburger.mariaDB4j</groupId>
+                       <artifactId>mariaDB4j</artifactId>
+                       <version>2.2.3</version>
+                       <scope>test</scope>
+               </dependency>
+
+               <dependency>
                        <groupId>org.onap.ccsdk.sli.core</groupId>
                        <artifactId>dblib-provider</artifactId>
-                       <version>${sdnctl.dblib.version}</version>
+                       <version>${sdnctl.dblib.version}</version>
                </dependency>
        </dependencies>
        <build>
                                        <target>1.7</target>
                                </configuration>
                        </plugin>
-                        <plugin>
-                                <groupId>org.apache.maven.plugins</groupId>
-                                <artifactId>maven-jar-plugin</artifactId>
-                                <version>2.6</version>
-                                <configuration>
-                                        <archive>
-                                                <manifest>
-                                                        <addClasspath>true</addClasspath>
-                                                        <mainClass>org.onap.ccsdk.sli.northbound.dmaapclient.DmaapListener</mainClass>
-                                                </manifest>
-                                        </archive>
-                                </configuration>
-                        </plugin>
+                       <plugin>
+                               <groupId>org.apache.maven.plugins</groupId>
+                               <artifactId>maven-jar-plugin</artifactId>
+                               <version>2.6</version>
+                               <configuration>
+                                       <archive>
+                                               <manifest>
+                                                       <addClasspath>true</addClasspath>
+                                                       <mainClass>org.onap.ccsdk.sli.northbound.dmaapclient.DmaapListener</mainClass>
+                                               </manifest>
+                                       </archive>
+                               </configuration>
+                       </plugin>
                        <plugin>
                                <artifactId>maven-assembly-plugin</artifactId>
                                <version>2.6</version>
diff --git a/dmaap-listener/src/main/java/org/onap/ccsdk/sli/northbound/dmaapclient/SdncDhcpEventConsumer.java b/dmaap-listener/src/main/java/org/onap/ccsdk/sli/northbound/dmaapclient/SdncDhcpEventConsumer.java
new file mode 100644 (file)
index 0000000..03560d3
--- /dev/null
@@ -0,0 +1,135 @@
+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.Iterator;
+import java.util.Map;
+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;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+public class SdncDhcpEventConsumer extends SdncDmaapConsumer {
+       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);
+               }
+
+       }
+
+}
index a0c5553..a51ea7c 100644 (file)
@@ -71,19 +71,25 @@ public abstract class SdncDmaapConsumer implements Runnable {
 
         try (FileInputStream in = new FileInputStream(new File(propertiesPath))) {
 
+           LOG.debug("propertiesPath: " + propertiesPath);
             this.properties = (Properties) properties.clone();
             this.properties.load(in);
 
-            String timeoutStr = properties.getProperty("timeout");
+
+            String timeoutStr = this.properties.getProperty("timeout");
+           LOG.debug("timeoutStr: " + timeoutStr);
 
             if ((timeoutStr != null) && (timeoutStr.length() > 0)) {
                 timeout = parseTimeOutValue(timeoutStr);
             }
 
-            String fetchPauseStr = properties.getProperty("fetchPause");
+            String fetchPauseStr = this.properties.getProperty("fetchPause");
+           LOG.debug("fetchPause(Str): " + fetchPauseStr);
             if ((fetchPauseStr != null) && (fetchPauseStr.length() > 0)) {
                 fetchPause = parseFetchPause(fetchPauseStr);
             }
+           LOG.debug("fetchPause: " + fetchPause);
+
 
             this.consumer = MRClientFactory.createConsumer(propertiesPath);
             ready = true;
diff --git a/dmaap-listener/src/test/java/org/onap/ccsdk/sli/northbound/dmaapclient/TestSdncDhcpEventConsumer.java b/dmaap-listener/src/test/java/org/onap/ccsdk/sli/northbound/dmaapclient/TestSdncDhcpEventConsumer.java
new file mode 100644 (file)
index 0000000..f160440
--- /dev/null
@@ -0,0 +1,139 @@
+/**
+ *
+ */
+package org.onap.ccsdk.sli.northbound.dmaapclient;
+
+import static org.junit.Assert.*;
+
+import java.io.InputStream;
+import java.net.URL;
+import java.sql.SQLException;
+import java.util.Properties;
+
+import javax.sql.rowset.CachedRowSet;
+
+import org.junit.After;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.onap.ccsdk.sli.core.dblib.DBResourceManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import ch.vorburger.mariadb4j.DB;
+import ch.vorburger.mariadb4j.DBConfigurationBuilder;
+
+/**
+ * @author dt5972
+ *
+ */
+public class TestSdncDhcpEventConsumer {
+
+       private static final Logger LOG = LoggerFactory.getLogger(TestSdncDhcpEventConsumer.class);
+
+
+       private static final String DHCP_MAP_TABLE = "CREATE TABLE `DHCP_MAP` (\n" +
+                       " mac_addr varchar(80) NOT NULL,\n" +
+                       " ip_addr varchar(80),\n" +
+                       " PRIMARY KEY(`mac_addr`)\n" +
+                       ")";
+
+
+
+       private static final String VALID_DHCP_EVENT =  "{\"msg_name\":\"DHCPACK\"," +
+                                                                                                    "\"macaddr\":\"fa:16:3e:8f:ea:68\"," +
+                                                                                                    "\"yiaddr\":\"10.3.0.2\"}";
+       private static final String SECOND_DHCP_EVENT =  "{\"msg_name\":\"DHCPACK\"," +
+                    "\"macaddr\":\"fa:16:3e:8f:ea:68\"," +
+                    "\"yiaddr\":\"10.3.0.3\"}";
+       private static final String MISSING_MSG_NAME_DHCP_EVENT =  "{\"macaddr\":\"fa:16:3e:8f:ea:68\"," +
+                                                                                                             "\"yiaddr\":\"10.3.0.2\"}";
+       private static final String MISSING_MAC_ADDR_DHCP_EVENT =   "{\"msg_name\":\"DHCPACK\"," +
+                                                                                                                       "\"yiaddr\":\"10.3.0.2\"}";
+       private static final String MISSING_IP_ADDR_DHCP_EVENT =  "{\"msg_name\":\"DHCPACK\"," +
+                                                                                                               "\"macaddr\":\"fa:16:3e:8f:ea:68\"}";
+
+       private static final String GET_DHCP_MAPPING = "SELECT * FROM DHCP_MAP WHERE mac_addr = 'fa:16:3e:8f:ea:68'";
+
+
+       private static DBResourceManager dblibSvc;
+       private static DB db;
+
+       private static SdncDhcpEventConsumer consumer;
+
+       /**
+        * @throws java.lang.Exception
+        */
+       @BeforeClass
+       public static void setUpBeforeClass() throws Exception {
+
+               LOG.info("Setting up DHCP event testing");
+
+               InputStream propStr = TestSdncDhcpEventConsumer.class.getResourceAsStream("/dblib.properties");
+
+               Properties props = new Properties();
+
+               props.load(propStr);
+
+
+               // Start MariaDB4j database
+
+               LOG.info("Starting MariaDB instance");
+               DBConfigurationBuilder config = DBConfigurationBuilder.newBuilder();
+               config.setPort(0); // 0 => autom. detect free port
+               db = DB.newEmbeddedDB(config.build());
+               db.start();
+
+
+               // Override jdbc URL and database name
+               props.setProperty("org.onap.ccsdk.sli.jdbc.database", "test");
+               props.setProperty("org.onap.ccsdk.sli.jdbc.url", config.getURL("test"));
+
+
+               // Create dblib connection
+
+               LOG.info("Getting DBResourceManager instance");
+               dblibSvc = new DBResourceManager(props);
+
+               // Create DHCP_MAP table
+               dblibSvc.writeData(DHCP_MAP_TABLE, null, null);
+
+               consumer = new SdncDhcpEventConsumer();
+               consumer.setJdbcDataSource(dblibSvc);
+               LOG.info("Setup complete");
+
+       }
+
+
+       @Test
+       public void testValid() throws InvalidMessageException, SQLException {
+               consumer.processMsg(VALID_DHCP_EVENT);
+               consumer.processMsg(SECOND_DHCP_EVENT);
+
+               CachedRowSet results = dblibSvc.getData(GET_DHCP_MAPPING, null, null);
+
+               if (!results.next()) {
+                       fail("Test query ["+GET_DHCP_MAPPING+"] returned no data");
+               } else {
+                       String ipAddr = results.getString("ip_addr");
+                       if (!"10.3.0.3".equals(ipAddr)) {
+                               fail("Expecting ipAddr to be 10.3.0.3, but was "+ipAddr);
+                       }
+               }
+
+       }
+
+       @Test (expected = InvalidMessageException.class)
+       public void testMissingMsgName() throws InvalidMessageException {
+               consumer.processMsg(MISSING_MSG_NAME_DHCP_EVENT);
+       }
+
+       @Test (expected = InvalidMessageException.class)
+       public void testMissingMacAddress() throws InvalidMessageException {
+               consumer.processMsg(MISSING_MAC_ADDR_DHCP_EVENT);
+       }
+
+       @Test (expected = InvalidMessageException.class)
+       public void testMissingIpAddress() throws InvalidMessageException {
+               consumer.processMsg(MISSING_IP_ADDR_DHCP_EVENT);
+       }
+}
diff --git a/dmaap-listener/src/test/resources/dblib.properties b/dmaap-listener/src/test/resources/dblib.properties
new file mode 100644 (file)
index 0000000..9506ac8
--- /dev/null
@@ -0,0 +1,38 @@
+###
+# ============LICENSE_START=======================================================
+# openECOMP : SDN-C
+# ================================================================================
+# Copyright (C) 2017 AT&T Intellectual Property. All rights
+#                                              reserved.
+# ================================================================================
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ============LICENSE_END=========================================================
+###
+
+# dblib.properrties
+org.onap.ccsdk.sli.dbtype=jdbc
+
+org.onap.ccsdk.sli.jdbc.hosts=sdnctldb01
+org.onap.ccsdk.sli.jdbc.url=jdbc:mysql://dbhost:3306/sdnctl
+org.onap.ccsdk.sli.jdbc.driver=org.mariadb.jdbc.Driver
+org.onap.ccsdk.sli.jdbc.database=sdnctl
+org.onap.ccsdk.sli.jdbc.user=sdnctl
+org.onap.ccsdk.sli.jdbc.password=gamma
+org.onap.ccsdk.sli.jdbc.connection.name=sdnctldb01
+org.onap.ccsdk.sli.jdbc.connection.timeout=50
+org.onap.ccsdk.sli.jdbc.request.timeout=100
+org.onap.ccsdk.sli.jdbc.limit.init=10
+org.onap.ccsdk.sli.jdbc.limit.min=10
+org.onap.ccsdk.sli.jdbc.limit.max=20
+org.onap.dblib.connection.recovery=false
+
diff --git a/dmaap-listener/src/test/resources/log4j.properties b/dmaap-listener/src/test/resources/log4j.properties
new file mode 100644 (file)
index 0000000..71849c3
--- /dev/null
@@ -0,0 +1,30 @@
+###
+# ============LICENSE_START=======================================================
+# openECOMP : SDN-C
+# ================================================================================
+# Copyright (C) 2017 AT&T Intellectual Property. All rights
+#                      reserved.
+# ================================================================================
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ============LICENSE_END=========================================================
+###
+
+log4j.rootLogger=DEBUG,CONSOLE
+
+# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
+log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
+log4j.appender.CONSOLE.Threshold=DEBUG
+log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
+log4j.appender.CONSOLE.layout.ConversionPattern=%p %d{yyyy-MM-dd HH:mm:ss.SSS Z} %c{1} - %m%n
+
+