BMP controller to collect bmp packets from bmp agent router

Change-Id: I398baf5132ebde149e7d43f8c3a2542c6cd52101
diff --git a/apps/bgpmonitoring/app/src/main/java/org/onosproject/bgpmonitoring/impl/BmpControllerImpl.java b/apps/bgpmonitoring/app/src/main/java/org/onosproject/bgpmonitoring/impl/BmpControllerImpl.java
new file mode 100644
index 0000000..5331102
--- /dev/null
+++ b/apps/bgpmonitoring/app/src/main/java/org/onosproject/bgpmonitoring/impl/BmpControllerImpl.java
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2024-present Open Networking Foundation
+ *
+ * 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.
+ */
+
+package org.onosproject.bgpmonitoring.impl;
+
+import org.onosproject.bgpmonitoring.BmpController;
+import org.onosproject.bgpmonitoring.BmpPacket;
+import org.onosproject.bgpmonitoring.BmpParseException;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Deactivate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Component(immediate = true)
+public class BmpControllerImpl implements BmpController {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private Controller controller;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected CoreService coreService;
+
+    private ApplicationId appId;
+
+    @Activate
+    protected void activate() {
+
+        appId = coreService.registerApplication("org.onosproject.bgpmonitoring");
+        controller = new Controller(this);
+        controller.start();
+        log.info("BMP activated");
+    }
+
+    @Deactivate
+    protected void deactivate() {
+        log.info("BMP stopped");
+    }
+
+    /**
+     * Start listening bmp messages from bmp router.
+     */
+    @Override
+    public void startListener() {
+        log.info("BMP station start listening...");
+        controller.start();
+    }
+
+    /**
+     * Stop listening bmp messages from bmp router.
+     */
+    @Override
+    public void closeListener() {
+        log.info("BMP station stop listening...");
+        controller.stop();
+    }
+
+    /**
+     * Process bmp message and notify the appropriate listeners.
+     *
+     * @param packet the BMP packet to process.
+     * @throws BmpParseException on data processing error
+     */
+    @Override
+    public void processBmpPacket(BmpPacket packet) throws BmpParseException {
+        //TODO
+        log.info("BMP message received {}", packet);
+    }
+}
diff --git a/apps/bgpmonitoring/app/src/main/java/org/onosproject/bgpmonitoring/impl/Controller.java b/apps/bgpmonitoring/app/src/main/java/org/onosproject/bgpmonitoring/impl/Controller.java
new file mode 100644
index 0000000..df7b90b
--- /dev/null
+++ b/apps/bgpmonitoring/app/src/main/java/org/onosproject/bgpmonitoring/impl/Controller.java
@@ -0,0 +1,185 @@
+/*
+ * Copyright 2024-present Open Networking Foundation
+ *
+ * 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.
+ */
+
+package org.onosproject.bgpmonitoring.impl;
+
+
+import org.jboss.netty.bootstrap.ServerBootstrap;
+import org.jboss.netty.channel.AdaptiveReceiveBufferSizePredictor;
+import org.jboss.netty.channel.Channel;
+import org.jboss.netty.channel.ChannelPipelineFactory;
+import org.jboss.netty.channel.FixedReceiveBufferSizePredictorFactory;
+import org.jboss.netty.channel.group.ChannelGroup;
+import org.jboss.netty.channel.group.DefaultChannelGroup;
+import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
+import org.onosproject.bgpmonitoring.BmpController;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.lang.management.ManagementFactory;
+import java.lang.management.RuntimeMXBean;
+import java.net.InetSocketAddress;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.Executors;
+
+import static org.onlab.util.Tools.groupedThreads;
+
+/**
+ * The main controller class. Handles all setup and network listeners - Ownership of bmp message receiver
+ */
+public class Controller {
+
+    private static final Logger log = LoggerFactory.getLogger(Controller.class);
+
+    private ChannelGroup channelGroup;
+    private Channel serverChannel;
+
+    protected static final short BMP_PORT_NUM = 9000;
+    private final int workerThreads = 16;
+
+    private long systemStartTime;
+
+    private NioServerSocketChannelFactory serverExecFactory;
+    private BmpController bmpController;
+
+    private static final int SEND_BUFFER_SIZE = 8 * 1024 * 1024;
+
+    /**
+     * Constructor to initialize the values.
+     *
+     * @param bmpController bmp controller instance
+     */
+    public Controller(BmpController bmpController) {
+        this.bmpController = bmpController;
+    }
+
+    /**
+     * To get system start time.
+     *
+     * @return system start time in milliseconds
+     */
+    public long getSystemStartTime() {
+        return (this.systemStartTime);
+    }
+
+    public void init() {
+        this.systemStartTime = System.currentTimeMillis();
+    }
+
+    /**
+     * Gets run time memory.
+     *
+     * @return m run time memory
+     */
+    public Map<String, Long> getMemory() {
+        Map<String, Long> m = new HashMap<>();
+        Runtime runtime = Runtime.getRuntime();
+        m.put("total", runtime.totalMemory());
+        m.put("free", runtime.freeMemory());
+        return m;
+    }
+
+    /**
+     * Gets UP time.
+     *
+     * @return UP time
+     */
+    public Long getUptime() {
+        RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean();
+        return rb.getUptime();
+    }
+
+    /**
+     * Tell controller it will accept bmp router connections.
+     */
+    public void run() {
+
+        try {
+
+            final ServerBootstrap bootstrap = createServerBootStrap();
+
+            bootstrap.setOption("reuseAddress", true);
+            bootstrap.setOption("tcpNoDelay", true);
+            bootstrap.setOption("keepAlive", true);
+            bootstrap.setOption("receiveBufferSize", Controller.SEND_BUFFER_SIZE);
+            bootstrap.setOption("receiveBufferSizePredictorFactory",
+                    new FixedReceiveBufferSizePredictorFactory(
+                            Controller.SEND_BUFFER_SIZE));
+            bootstrap.setOption("receiveBufferSizePredictor",
+                    new AdaptiveReceiveBufferSizePredictor(64, 4096, 65536));
+            bootstrap.setOption("child.keepAlive", true);
+            bootstrap.setOption("child.tcpNoDelay", true);
+            bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE);
+            bootstrap.setOption("child.receiveBufferSize", Controller.SEND_BUFFER_SIZE);
+            bootstrap.setOption("child.receiveBufferSizePredictorFactory",
+                    new FixedReceiveBufferSizePredictorFactory(
+                            Controller.SEND_BUFFER_SIZE));
+            bootstrap.setOption("child.reuseAddress", true);
+
+            ChannelPipelineFactory pfact = null;
+
+            bootstrap.setPipelineFactory(pfact);
+            InetSocketAddress inetSocketAddress = new InetSocketAddress(BMP_PORT_NUM);
+            channelGroup = new DefaultChannelGroup();
+            serverChannel = bootstrap.bind(inetSocketAddress);
+            channelGroup.add(serverChannel);
+            log.info("Listening for bmp router connection on {}", inetSocketAddress);
+        } catch (Exception e) {
+            throw new IllegalStateException(e);
+        }
+    }
+
+    /**
+     * Creates server boot strap.
+     *
+     * @return ServerBootStrap
+     */
+    private ServerBootstrap createServerBootStrap() {
+
+        if (workerThreads == 0) {
+            serverExecFactory = new NioServerSocketChannelFactory(
+                    Executors.newCachedThreadPool(groupedThreads("onos/bmp", "boss-%d")),
+                    Executors.newCachedThreadPool(groupedThreads("onos/bmp", "worker-%d")));
+            return new ServerBootstrap(serverExecFactory);
+        } else {
+            serverExecFactory = new NioServerSocketChannelFactory(
+                    Executors.newCachedThreadPool(groupedThreads("onos/bmp", "boss-%d")),
+                    Executors.newCachedThreadPool(groupedThreads("onos/bmp", "worker-%d")),
+                    workerThreads);
+            return new ServerBootstrap(serverExecFactory);
+        }
+    }
+
+    /**
+     * Stops the BMP controller.
+     */
+    public void stop() {
+        log.info("Stopped");
+        serverExecFactory.shutdown();
+        channelGroup.close();
+    }
+
+    /**
+     * Starts the BMP controller.
+     */
+    public void start() {
+        log.info("Started");
+        this.run();
+    }
+
+
+}