initial code for the mac-learning app

Change-Id: I04f3b17c453e502f20477e83cb7cdc796b4160ae
diff --git a/api/pom.xml b/api/pom.xml
new file mode 100644
index 0000000..042b5e4
--- /dev/null
+++ b/api/pom.xml
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Copyright 2017-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.
+  -->
+<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">
+    <parent>
+        <artifactId>maclearner</artifactId>
+        <groupId>org.opencord</groupId>
+        <version>1.0.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>maclearner-api</artifactId>
+    <packaging>bundle</packaging>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-source-plugin</artifactId>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>
diff --git a/api/src/main/java/org/opencord/maclearner/api/DefaultMacLearner.java b/api/src/main/java/org/opencord/maclearner/api/DefaultMacLearner.java
new file mode 100644
index 0000000..17109f3
--- /dev/null
+++ b/api/src/main/java/org/opencord/maclearner/api/DefaultMacLearner.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2017-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.opencord.maclearner.api;
+
+import org.apache.commons.lang.builder.ToStringBuilder;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+
+/**
+ * Default Mac Learner object model implementation.
+ */
+public class DefaultMacLearner implements MacLearner {
+
+    private MacLearnerKey macLearnerKey;
+    private MacAddress macAddress;
+
+    public static final String DEVICE_ID_FN = "deviceId";
+    public static final String PORT_NUMBER_FN = "portNumber";
+    public static final String VLAN_ID_FN = "vlanId";
+    public static final String MAC_ADDRESS_FN = "macAddress";
+
+    @Override
+    public DeviceId deviceId() {
+        return macLearnerKey.getDeviceId();
+    }
+
+    @Override
+    public PortNumber portNumber() {
+        return macLearnerKey.getPortNumber();
+    }
+
+    @Override
+    public VlanId vlanId() {
+        return macLearnerKey.getVlanId();
+    }
+
+    @Override
+    public MacAddress macAddress() {
+        return macAddress;
+    }
+
+    public DefaultMacLearner(DeviceId deviceId, PortNumber portNumber, VlanId vlanId, MacAddress macAddress) {
+        this.macLearnerKey = new MacLearnerKey(deviceId, portNumber, vlanId);
+        this.macAddress = macAddress;
+    }
+
+    @Override
+    public String toString() {
+        return new ToStringBuilder(this)
+                .append(DEVICE_ID_FN, deviceId())
+                .append(PORT_NUMBER_FN, portNumber())
+                .append(VLAN_ID_FN, vlanId())
+                .append(MAC_ADDRESS_FN, macAddress())
+                .toString();
+    }
+
+}
diff --git a/api/src/main/java/org/opencord/maclearner/api/MacDeleteResult.java b/api/src/main/java/org/opencord/maclearner/api/MacDeleteResult.java
new file mode 100644
index 0000000..3db8a62
--- /dev/null
+++ b/api/src/main/java/org/opencord/maclearner/api/MacDeleteResult.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2017-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.opencord.maclearner.api;
+
+/**
+ * Result of Delete Mac Mapping operation.
+ */
+public enum MacDeleteResult {
+    SUCCESSFUL,
+    UNSUCCESSFUL,
+    NOT_EXIST
+}
diff --git a/api/src/main/java/org/opencord/maclearner/api/MacLearner.java b/api/src/main/java/org/opencord/maclearner/api/MacLearner.java
new file mode 100644
index 0000000..1ec8d36
--- /dev/null
+++ b/api/src/main/java/org/opencord/maclearner/api/MacLearner.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2017-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.opencord.maclearner.api;
+
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+
+/**
+ * Representation of a mac learner object.
+ */
+public interface MacLearner {
+
+    /**
+     * Returns the device identifier.
+     *
+     * @return device id
+     */
+    DeviceId deviceId();
+
+    /**
+     * Returns from which port the mac address is learned.
+     * @return port number
+     */
+    PortNumber portNumber();
+
+    /**
+     * Returns which vlan id of the package the mac address is learned from.
+     * If packet is double tagged, vlan id is equal to QinQVID.
+     * @return vlan id
+     */
+    VlanId vlanId();
+
+    /**
+     * Returns Mac Address information.
+     * @return mac address
+     */
+    MacAddress macAddress();
+
+}
diff --git a/api/src/main/java/org/opencord/maclearner/api/MacLearnerEvent.java b/api/src/main/java/org/opencord/maclearner/api/MacLearnerEvent.java
new file mode 100644
index 0000000..d28e31d
--- /dev/null
+++ b/api/src/main/java/org/opencord/maclearner/api/MacLearnerEvent.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2017-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.opencord.maclearner.api;
+
+import org.apache.commons.lang.builder.ToStringBuilder;
+import org.onosproject.event.AbstractEvent;
+
+/**
+ * Entity that represents Mac Learner events.
+ */
+public class MacLearnerEvent extends AbstractEvent<MacLearnerEvent.Type, MacLearner> {
+
+    public enum Type {
+
+        /**
+         * New entry added to MacLearnerMap.
+         */
+        ADDED,
+        /**
+         * An entry removed from MacLearnerMap.
+         */
+        REMOVED,
+    }
+
+    /**
+     * Creates an event due to mac learner map update.
+     * @param type type of event
+     * @param macLearner mac learner object
+     */
+    public MacLearnerEvent(MacLearnerEvent.Type type, MacLearner macLearner) {
+        super(type, macLearner);
+    }
+
+    @Override
+    public String toString() {
+        return new ToStringBuilder(this)
+                .append(super.toString())
+                .toString();
+    }
+
+}
diff --git a/api/src/main/java/org/opencord/maclearner/api/MacLearnerKey.java b/api/src/main/java/org/opencord/maclearner/api/MacLearnerKey.java
new file mode 100644
index 0000000..d892f18
--- /dev/null
+++ b/api/src/main/java/org/opencord/maclearner/api/MacLearnerKey.java
@@ -0,0 +1,91 @@
+/*
+ * Copyright 2017-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.opencord.maclearner.api;
+
+import org.onlab.packet.VlanId;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+
+import java.util.Objects;
+
+/**
+ * Key of Mac Address Map.
+ */
+public class MacLearnerKey {
+
+    private DeviceId deviceId;
+    private PortNumber portNumber;
+    private VlanId vlanId;
+
+    public DeviceId getDeviceId() {
+        return deviceId;
+    }
+
+    public void setDeviceId(DeviceId deviceId) {
+        this.deviceId = deviceId;
+    }
+
+    public PortNumber getPortNumber() {
+        return portNumber;
+    }
+
+    public void setPortNumber(PortNumber portNumber) {
+        this.portNumber = portNumber;
+    }
+
+    public VlanId getVlanId() {
+        return vlanId;
+    }
+
+    public void setVlanId(VlanId vlanId) {
+        this.vlanId = vlanId;
+    }
+
+    public MacLearnerKey(DeviceId deviceId, PortNumber portNumber, VlanId vlanId) {
+        this.deviceId = deviceId;
+        this.portNumber = portNumber;
+        this.vlanId = vlanId;
+    }
+
+    @Override
+    public String toString() {
+        return "MacLearnerKey{" +
+                "deviceId=" + deviceId +
+                ", portNumber=" + portNumber +
+                ", vlanId=" + vlanId +
+                '}';
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+        MacLearnerKey that = (MacLearnerKey) o;
+        return Objects.equals(deviceId, that.deviceId) &&
+                Objects.equals(portNumber, that.portNumber) &&
+                Objects.equals(vlanId, that.vlanId);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(deviceId, portNumber, vlanId);
+    }
+
+}
diff --git a/api/src/main/java/org/opencord/maclearner/api/MacLearnerListener.java b/api/src/main/java/org/opencord/maclearner/api/MacLearnerListener.java
new file mode 100644
index 0000000..e8ea9cb
--- /dev/null
+++ b/api/src/main/java/org/opencord/maclearner/api/MacLearnerListener.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2017-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.opencord.maclearner.api;
+
+import org.onosproject.event.EventListener;
+
+/**
+ * Entity capable of receiving Mac Learner related events.
+ */
+public interface MacLearnerListener extends EventListener<MacLearnerEvent> {
+}
diff --git a/api/src/main/java/org/opencord/maclearner/api/MacLearnerProvider.java b/api/src/main/java/org/opencord/maclearner/api/MacLearnerProvider.java
new file mode 100644
index 0000000..fb4ace9
--- /dev/null
+++ b/api/src/main/java/org/opencord/maclearner/api/MacLearnerProvider.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2017-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.opencord.maclearner.api;
+
+import org.onosproject.net.provider.Provider;
+
+/**
+ * Abstraction of mac learner provider.
+ */
+public interface MacLearnerProvider extends Provider {
+}
diff --git a/api/src/main/java/org/opencord/maclearner/api/MacLearnerProviderService.java b/api/src/main/java/org/opencord/maclearner/api/MacLearnerProviderService.java
new file mode 100644
index 0000000..226aa98
--- /dev/null
+++ b/api/src/main/java/org/opencord/maclearner/api/MacLearnerProviderService.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2017-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.opencord.maclearner.api;
+
+import org.onosproject.net.provider.ProviderService;
+
+/**
+ * Abstraction of a flow rule provider service.
+ */
+public interface MacLearnerProviderService extends ProviderService<MacLearnerProvider> {
+}
diff --git a/api/src/main/java/org/opencord/maclearner/api/MacLearnerService.java b/api/src/main/java/org/opencord/maclearner/api/MacLearnerService.java
new file mode 100644
index 0000000..1fc3d97
--- /dev/null
+++ b/api/src/main/java/org/opencord/maclearner/api/MacLearnerService.java
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2017-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.opencord.maclearner.api;
+
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+import org.onosproject.event.ListenerService;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+
+/**
+ * Mac Learner Service.
+ */
+public interface MacLearnerService extends ListenerService<MacLearnerEvent, MacLearnerListener> {
+
+    /**
+     * Adding a port to ignore map to prevent mac mapping.
+     * @param deviceId openflow device id
+     * @param portNumber port number
+     */
+    void addPortToIgnore(DeviceId deviceId, PortNumber portNumber);
+
+    /**
+     * Removing a port from ignore port map.
+     * @param deviceId openflow device id
+     * @param portNumber port number
+     */
+    void removeFromIgnoredPorts(DeviceId deviceId, PortNumber portNumber);
+
+    /**
+     * Getting All MAC Mappings.
+     * @return Map of MAC Addresses by device id, port number and vlan id
+     */
+    Map<MacLearnerKey, MacAddress> getAllMappings();
+
+    /**
+     * Getting Requested MAC Address.
+     * @param deviceId openflow device id
+     * @param portNumber port number
+     * @param vlanId vlan id
+     * @return  MAC Address of requested device id, port number and vlan id
+     */
+    Optional<MacAddress> getMacMapping(DeviceId deviceId, PortNumber portNumber, VlanId vlanId);
+
+    /**
+     * Deleting a MAC Mapping.
+     * @param deviceId openflow device id
+     * @param portNumber port number
+     * @param vlanId vlan id
+     * @return MacDeleteResult situation after method call
+     */
+    MacDeleteResult deleteMacMapping(DeviceId deviceId, PortNumber portNumber, VlanId vlanId);
+
+    /**
+     * Deleting MAC Mappings of port.
+     * @param deviceId openflow device id
+     * @param portNumber port number
+     * @return true if the mappings deleted successfully; false otherwise
+     */
+    boolean deleteMacMappings(DeviceId deviceId, PortNumber portNumber);
+
+    /**
+     * Deleting MAC Mappings of device.
+     * @param deviceId openflow device id
+     * @return true if the mappings deleted successfully; false otherwise
+     */
+    boolean deleteMacMappings(DeviceId deviceId);
+
+    /**
+     * Getting Device List in MAC Mapping List.
+     * @return mapped ONOS devices
+     */
+    Set<DeviceId> getMappedDevices();
+
+    /**
+     * Getting Port Number List in MAC Mapping List.
+     * @return mapped port numbers
+     */
+    Set<PortNumber> getMappedPorts();
+
+    /**
+     * Getting Ignored Ports for MAC Mapping.
+     * @return device and its ignored ports map
+     */
+    Map<DeviceId, Set<PortNumber>> getIgnoredPorts();
+
+}
diff --git a/api/src/main/java/org/opencord/maclearner/api/MacLearnerValue.java b/api/src/main/java/org/opencord/maclearner/api/MacLearnerValue.java
new file mode 100644
index 0000000..bc1f244
--- /dev/null
+++ b/api/src/main/java/org/opencord/maclearner/api/MacLearnerValue.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2017-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.opencord.maclearner.api;
+
+import org.onlab.packet.MacAddress;
+
+import java.util.Objects;
+
+/**
+ * Value of Mac Address Map which keeps a timestamp via Mac Address.
+ */
+public class MacLearnerValue {
+
+    private MacAddress macAddress;
+    private long timestamp;
+
+    public MacAddress getMacAddress() {
+        return macAddress;
+    }
+
+    public void setMacAddress(MacAddress macAddress) {
+        this.macAddress = macAddress;
+    }
+
+    public long getTimestamp() {
+        return timestamp;
+    }
+
+    public void setTimestamp(long timestamp) {
+        this.timestamp = timestamp;
+    }
+
+    public MacLearnerValue(MacAddress macAddress, long timestamp) {
+        this.macAddress = macAddress;
+        this.timestamp = timestamp;
+    }
+
+    @Override
+    public String toString() {
+        return "MacLearnerValue{" +
+                "macAddress=" + macAddress +
+                ", timestamp=" + timestamp +
+                '}';
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+        MacLearnerValue that = (MacLearnerValue) o;
+        return timestamp == that.timestamp &&
+                Objects.equals(macAddress, that.macAddress);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(macAddress, timestamp);
+    }
+
+}
diff --git a/api/src/main/java/org/opencord/maclearner/api/package-info.java b/api/src/main/java/org/opencord/maclearner/api/package-info.java
new file mode 100755
index 0000000..11e53de
--- /dev/null
+++ b/api/src/main/java/org/opencord/maclearner/api/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2017-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.
+ */
+
+/**
+ * Mac Learner API.
+ */
+package org.opencord.maclearner.api;