Added a new application for packet statistics with passed checkstyle tests.

Change-Id: I29ca84dfe88953defa438aec56d66b2bf9b177a9
diff --git a/apps/packet-stats/BUCK b/apps/packet-stats/BUCK
new file mode 100644
index 0000000..560294b
--- /dev/null
+++ b/apps/packet-stats/BUCK
@@ -0,0 +1,19 @@
+COMPILE_DEPS = [
+    '//lib:CORE_DEPS',
+    '//core/store/serializers:onos-core-serializers',
+    '//core/store/primitives:onos-core-primitives',
+    '//core/api:onos-api',
+    '//lib:org.apache.karaf.shell.console',
+    '//cli:onos-cli',
+]
+
+osgi_jar_with_tests (
+    deps = COMPILE_DEPS,
+)
+
+onos_app (
+    title = 'Packet Statistics',
+    category = 'Traffic Steering',
+    url = 'http://onosproject.org',
+    description = 'Application to calculate the number of packets of different types',
+)
diff --git a/apps/packet-stats/pom.xml b/apps/packet-stats/pom.xml
new file mode 100644
index 0000000..c98d679
--- /dev/null
+++ b/apps/packet-stats/pom.xml
@@ -0,0 +1,75 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Copyright 2014-present Open Networking Laboratory
+  ~
+  ~ 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.
+  -->
+<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/maven-v4_0_0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.onosproject</groupId>
+        <artifactId>onos-apps</artifactId>
+        <version>1.11.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>onos-app-packet-stats</artifactId>
+    <packaging>bundle</packaging>
+
+    <description>Packet Statistics application using flow subsystem</description>
+
+    <properties>
+        <onos.app.name>org.onosproject.packet-stats</onos.app.name>
+        <onos.app.title>Packet Statistics App</onos.app.title>
+        <onos.app.category>Traffic Steering</onos.app.category>
+        <onos.app.url>http://onosproject.org</onos.app.url>
+        <onos.app.readme>Packet Statistics application using flow subsystem.</onos.app.readme>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-core-serializers</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-core-primitives</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-cli</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.karaf.shell</groupId>
+            <artifactId>org.apache.karaf.shell.console</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.scr.annotations</artifactId>
+        </dependency>
+    </dependencies>
+
+</project>
diff --git a/apps/packet-stats/src/main/java/org/onosproject/packetstats/PacketStatistics.java b/apps/packet-stats/src/main/java/org/onosproject/packetstats/PacketStatistics.java
new file mode 100644
index 0000000..3b09618
--- /dev/null
+++ b/apps/packet-stats/src/main/java/org/onosproject/packetstats/PacketStatistics.java
@@ -0,0 +1,227 @@
+/*
+ * Copyright 2014-present Open Networking Laboratory
+ *
+ * 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.packetstats;
+import com.codahale.metrics.Counter;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onlab.packet.Ethernet;
+import org.onlab.packet.IPv4;
+import org.onlab.packet.UDP;
+import org.onosproject.cfg.ComponentConfigService;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.net.flow.FlowRuleService;
+import org.onosproject.net.packet.InboundPacket;
+import org.onosproject.net.packet.PacketContext;
+import org.onosproject.net.packet.PacketProcessor;
+import org.onosproject.net.packet.PacketService;
+
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+import static org.slf4j.LoggerFactory.getLogger;
+import org.onlab.metrics.MetricsComponent;
+import org.onlab.metrics.MetricsFeature;
+import org.onlab.metrics.MetricsService;
+
+/**
+ * Application for Packet Statistics.
+ */
+@Component(immediate = true)
+public class PacketStatistics {
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected PacketService packetService;
+
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected FlowRuleService flowRuleService;
+
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected CoreService coreService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected ComponentConfigService cfgService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected MetricsService metricService;
+
+    private ReactivePacketProcessor processor = new ReactivePacketProcessor();
+    private final Logger log = getLogger(getClass());
+
+    private ApplicationId appId;
+
+
+    private MetricsComponent packetStatisticsComponent;
+    private MetricsFeature arpFeature;
+    private Counter arpCounter;
+    private MetricsFeature dhcpFeature;
+    private Counter dhcpCounter;
+    private MetricsFeature lldpFeature;
+    private Counter lldpCounter;
+    private MetricsFeature vlanFeature;
+    private Counter vlanCounter;
+    private MetricsFeature tcpFeature;
+    private Counter tcpCounter;
+    private MetricsFeature icmpFeature;
+    private Counter icmpCounter;
+    private MetricsFeature igmpFeature;
+    private Counter igmpCounter;
+    private MetricsFeature pimFeature;
+    private Counter pimCounter;
+    private MetricsFeature bsnFeature;
+    private Counter bsnCounter;
+    private MetricsFeature rarpFeature;
+    private Counter rarpCounter;
+    private MetricsFeature mplsFeature;
+    private Counter mplsCounter;
+    private MetricsFeature unknownFeature;
+    private Counter unknownCounter;
+
+
+
+    @Activate
+    public void activate(ComponentContext context) {
+        this.packetStatisticsComponent =
+                metricService.registerComponent("packetStatisticsComponent");
+        this.arpFeature =
+                packetStatisticsComponent.registerFeature("arpFeature");
+        this.dhcpFeature =
+                packetStatisticsComponent.registerFeature("dhcpFeature");
+        this.lldpFeature =
+                packetStatisticsComponent.registerFeature("lldpFeature");
+        this.vlanFeature =
+                packetStatisticsComponent.registerFeature("vlanFeature");
+        this.tcpFeature =
+                packetStatisticsComponent.registerFeature("tcpFeature");
+        this.icmpFeature =
+                packetStatisticsComponent.registerFeature("icmpFeature");
+        this.igmpFeature =
+                packetStatisticsComponent.registerFeature("igmpFeature");
+        this.pimFeature =
+                packetStatisticsComponent.registerFeature("pimFeature");
+        this.bsnFeature =
+                packetStatisticsComponent.registerFeature("bsnFeature");
+        this.rarpFeature =
+                packetStatisticsComponent.registerFeature("rarpFeature");
+        this.mplsFeature =
+                packetStatisticsComponent.registerFeature("mplsFeature");
+        this.unknownFeature =
+                packetStatisticsComponent.registerFeature("unknownFeature");
+        this.arpCounter =
+                metricService.createCounter(packetStatisticsComponent, arpFeature, "ARP Packet Counter");
+        this.dhcpCounter =
+                metricService.createCounter(packetStatisticsComponent, dhcpFeature, "DHCP Packet Counter");
+        this.lldpCounter =
+                metricService.createCounter(packetStatisticsComponent, lldpFeature, "LLDP Packet Counter");
+        this.vlanCounter =
+                metricService.createCounter(packetStatisticsComponent, vlanFeature, "VLAN Packet Counter");
+        this.icmpCounter =
+                metricService.createCounter(packetStatisticsComponent, icmpFeature, "ICMP Packet Counter");
+        this.igmpCounter =
+                metricService.createCounter(packetStatisticsComponent, igmpFeature, "IGMP Packet Counter");
+        this.pimCounter =
+                metricService.createCounter(packetStatisticsComponent, pimFeature, "PIM Packet Counter");
+        this.bsnCounter =
+                metricService.createCounter(packetStatisticsComponent, bsnFeature, "BSN Packet Counter");
+        this.mplsCounter =
+                metricService.createCounter(packetStatisticsComponent, bsnFeature, "MPLS Packet Counter");
+        this.unknownCounter =
+                metricService.createCounter(packetStatisticsComponent, bsnFeature, "Unknown Packet Counter");
+
+
+        appId = coreService.registerApplication("org.onosproject.packet-stats");
+
+        packetService.addProcessor(processor, PacketProcessor.director(2));
+        log.info("Started", appId.id());
+
+
+    }
+
+    @Deactivate
+    public void deactivate() {
+        cfgService.unregisterProperties(getClass(), false);
+        flowRuleService.removeFlowRulesById(appId);
+        packetService.removeProcessor(processor);
+        processor = null;
+        log.info("Stopped");
+    }
+
+
+    /**
+     * Packet processor responsible for forwarding packets along their paths.
+     */
+    private class ReactivePacketProcessor implements PacketProcessor {
+        @Override
+        public void process(PacketContext context) {
+            InboundPacket pkt = context.inPacket();
+            Ethernet ethPkt = pkt.parsed();
+
+            //Indicates whether this is an ARP Packet
+            if (ethPkt.getEtherType() == Ethernet.TYPE_ARP) {
+                arpCounter.inc();
+
+            } else if (ethPkt.getEtherType() == Ethernet.TYPE_LLDP) {
+                lldpCounter.inc();
+
+            } else if (ethPkt.getEtherType() == Ethernet.TYPE_VLAN) {
+                vlanCounter.inc();
+
+            } else if (ethPkt.getEtherType() == Ethernet.TYPE_BSN) {
+                bsnCounter.inc();
+
+            } else if (ethPkt.getEtherType() == Ethernet.TYPE_RARP) {
+                rarpCounter.inc();
+
+            } else if (ethPkt.getEtherType() == Ethernet.MPLS_UNICAST
+                    || ethPkt.getEtherType() == Ethernet.MPLS_MULTICAST) {
+                mplsCounter.inc();
+
+            } else if (ethPkt.getEtherType() == Ethernet.TYPE_IPV4) {
+                IPv4 ipv4Packet = (IPv4) ethPkt.getPayload();
+                //Indicates whether this is a TCP Packet
+                if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_TCP) {
+                    tcpCounter.inc();
+
+                } else if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
+                    icmpCounter.inc();
+
+                } else if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_IGMP) {
+                    igmpCounter.inc();
+
+                } else if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_PIM) {
+                    pimCounter.inc();
+
+                } else if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_UDP) {
+                    UDP udpPacket = (UDP) ipv4Packet.getPayload();
+                        //Indicates whether this packet is a DHCP Packet
+                        if (udpPacket.getSourcePort() == UDP.DHCP_CLIENT_PORT
+                                || udpPacket.getSourcePort() == UDP.DHCP_SERVER_PORT) {
+                            dhcpCounter.inc();
+                        }
+                    }
+
+                } else {
+                    log.info("Packet is unknown.");
+                    unknownCounter.inc();
+              }
+            }
+            }
+
+        }
\ No newline at end of file
diff --git a/apps/packet-stats/src/main/java/org/onosproject/packetstats/package-info.java b/apps/packet-stats/src/main/java/org/onosproject/packetstats/package-info.java
new file mode 100644
index 0000000..144cfa6
--- /dev/null
+++ b/apps/packet-stats/src/main/java/org/onosproject/packetstats/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2014-present Open Networking Laboratory
+ *
+ * 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.
+ */
+
+/**
+ * Sample application that provides simple packet statistics.
+ */
+package org.onosproject.packetstats;
diff --git a/apps/pom.xml b/apps/pom.xml
index 5f0052b..0c39c0f 100644
--- a/apps/pom.xml
+++ b/apps/pom.xml
@@ -34,6 +34,7 @@
         <module>roadm</module>
         <module>faultmanagement</module>
         <module>fwd</module>
+        <module>packet-stats</module>
         <module>mobility</module>
         <module>proxyarp</module>
         <module>sdnip</module>
diff --git a/modules.defs b/modules.defs
index 30fbc3e..8205c93 100644
--- a/modules.defs
+++ b/modules.defs
@@ -146,6 +146,7 @@
     '//apps/dhcp:onos-apps-dhcp-oar',
     '//apps/dhcprelay:onos-apps-dhcprelay-oar',
     '//apps/fwd:onos-apps-fwd-oar',
+    '//apps/packet-stats:onos-apps-packet-stats-oar',
     '//apps/acl:onos-apps-acl-oar',
     '//apps/bgprouter:onos-apps-bgprouter-oar',
     '//apps/cip:onos-apps-cip-oar',