Initial work on using ExtensionMappingAddress to abstract LCAF addr

Change-Id: I0db0682a1b15c8c7ba49ff82c82f06770b011dc6
diff --git a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/addresses/ExtensionMappingAddress.java b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/addresses/ExtensionMappingAddress.java
index 853fd97..79e76b4 100644
--- a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/addresses/ExtensionMappingAddress.java
+++ b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/addresses/ExtensionMappingAddress.java
@@ -15,73 +15,17 @@
  */
 package org.onosproject.mapping.addresses;
 
-import org.onosproject.net.DeviceId;
-import org.onosproject.net.flow.criteria.ExtensionSelector;
-
-import java.util.Objects;
+import org.onosproject.net.flow.Extension;
 
 /**
- * Mapping address for implementing extensions.
+ * An extension for the mapping address API.
  */
-public final class ExtensionMappingAddress implements MappingAddress {
-
-    private final ExtensionSelector extensionSelector;
-    private final DeviceId deviceId;
+public interface ExtensionMappingAddress extends Extension {
 
     /**
-     * Default constructor of ExtensionMappingAddress.
+     * Obtains the type of the extension mapping address.
      *
-     * @param extensionSelector extension selector
-     * @param deviceId          device identifier
+     * @return type
      */
-    public ExtensionMappingAddress(ExtensionSelector extensionSelector, DeviceId deviceId) {
-        this.extensionSelector = extensionSelector;
-        this.deviceId = deviceId;
-    }
-
-    @Override
-    public Type type() {
-        return Type.EXTENSION;
-    }
-
-    /**
-     * Returns the extension selector.
-     *
-     * @return extension selector
-     */
-    public ExtensionSelector extensionSelector() {
-        return extensionSelector;
-    }
-
-    /**
-     * Returns the device identifier.
-     *
-     * @return the device identifier
-     */
-    public DeviceId deviceId() {
-        return deviceId;
-    }
-
-    @Override
-    public String toString() {
-        return type().toString() + TYPE_SEPARATOR + deviceId + "/" + extensionSelector;
-    }
-
-    @Override
-    public int hashCode() {
-        return Objects.hash(type().ordinal(), extensionSelector, deviceId);
-    }
-
-    @Override
-    public boolean equals(Object obj) {
-        if (this == obj) {
-            return true;
-        }
-        if (obj instanceof ExtensionMappingAddress) {
-            ExtensionMappingAddress that = (ExtensionMappingAddress) obj;
-            return Objects.equals(extensionSelector, that.extensionSelector) &&
-                    Objects.equals(deviceId, that.deviceId);
-        }
-        return false;
-    }
+    ExtensionMappingAddressType type();
 }
diff --git a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/addresses/ExtensionMappingAddressResolver.java b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/addresses/ExtensionMappingAddressResolver.java
new file mode 100644
index 0000000..82427fb
--- /dev/null
+++ b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/addresses/ExtensionMappingAddressResolver.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2017-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.mapping.addresses;
+
+import com.google.common.annotations.Beta;
+import org.onosproject.net.driver.HandlerBehaviour;
+
+/**
+ * Provides access to the extension mapping addresses implemented by this driver.
+ */
+@Beta
+public interface ExtensionMappingAddressResolver extends HandlerBehaviour {
+
+    /**
+     * Obtains an extension mapping address instance of the specified type,
+     * if supported by the driver.
+     *
+     * @param type type of extension to get
+     * @return extension mapping address
+     * @throws UnsupportedOperationException if the extension type is not
+     * supported by this driver
+     */
+    ExtensionMappingAddress getExtensionMappingAddress(ExtensionMappingAddressType type);
+}
diff --git a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/addresses/ExtensionMappingAddressType.java b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/addresses/ExtensionMappingAddressType.java
new file mode 100644
index 0000000..824de13
--- /dev/null
+++ b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/addresses/ExtensionMappingAddressType.java
@@ -0,0 +1,110 @@
+/*
+ * Copyright 2017-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.mapping.addresses;
+
+import com.google.common.base.MoreObjects;
+
+import java.util.Objects;
+
+/**
+ * Type of mapping address extensions.
+ */
+public class ExtensionMappingAddressType {
+
+    /**
+     * A list of well-known named extension mapping address type codes.
+     * These numbers have no impact on the actual LISP type id.
+     */
+    public enum ExtensionMappingAddressTypes {
+        LIST_ADDRESS(1),
+        SEGMENT_ADDRESS(2),
+        AS_ADDRESS(3),
+        APPLICATION_DATA_ADDRESS(4),
+        GEO_COORDINATE_ADDRESS(5),
+        NAT_ADDRESS(7),
+        NONCE_ADDRESS(8),
+        MULTICAST_ADDRESS(9),
+        TRAFFIC_ENGINEERING_ADDRESS(10),
+        SECURITY_ADDRESS(11),
+        SOURCE_DEST_ADDRESS(12),
+
+        UNRESOLVED_TYPE(200);
+
+        private ExtensionMappingAddressType type;
+
+        /**
+         * Creates a new named extension mapping address type.
+         *
+         * @param type type code
+         */
+        ExtensionMappingAddressTypes(int type) {
+            this.type = new ExtensionMappingAddressType(type);
+        }
+
+        /**
+         * Obtains the extension type object for this named type code.
+         *
+         * @return extension type object
+         */
+        public ExtensionMappingAddressType type() {
+            return type;
+        }
+    }
+
+    private final int type;
+
+    /**
+     * Creates an extension type with the given int type code.
+     *
+     * @param type type code
+     */
+    public ExtensionMappingAddressType(int type) {
+        this.type = type;
+    }
+
+    /**
+     * Obtains the integer value associated with this type.
+     *
+     * @return an integer value
+     */
+    public int toInt() {
+        return this.type;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(type);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj instanceof ExtensionMappingAddressType) {
+            final ExtensionMappingAddressType that = (ExtensionMappingAddressType) obj;
+            return this.type == that.type;
+        }
+        return false;
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(ExtensionMappingAddressType.class)
+                    .add("type", type)
+                    .toString();
+    }
+}
diff --git a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/addresses/ExtensionMappingAddressWrapper.java b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/addresses/ExtensionMappingAddressWrapper.java
new file mode 100644
index 0000000..a3a539a
--- /dev/null
+++ b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/addresses/ExtensionMappingAddressWrapper.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2017-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.mapping.addresses;
+
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.flow.criteria.ExtensionSelector;
+
+import java.util.Objects;
+
+/**
+ * Mapping address for implementing extensions.
+ */
+public final class ExtensionMappingAddressWrapper implements MappingAddress {
+
+    private final ExtensionSelector extensionSelector;
+    private final DeviceId deviceId;
+
+    /**
+     * Default constructor of ExtensionMappingAddressWrapper.
+     *
+     * @param extensionSelector extension selector
+     * @param deviceId          device identifier
+     */
+    public ExtensionMappingAddressWrapper(ExtensionSelector extensionSelector, DeviceId deviceId) {
+        this.extensionSelector = extensionSelector;
+        this.deviceId = deviceId;
+    }
+
+    @Override
+    public Type type() {
+        return Type.EXTENSION;
+    }
+
+    /**
+     * Returns the extension selector.
+     *
+     * @return extension selector
+     */
+    public ExtensionSelector extensionSelector() {
+        return extensionSelector;
+    }
+
+    /**
+     * Returns the device identifier.
+     *
+     * @return the device identifier
+     */
+    public DeviceId deviceId() {
+        return deviceId;
+    }
+
+    @Override
+    public String toString() {
+        return type().toString() + TYPE_SEPARATOR + deviceId + "/" + extensionSelector;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(type().ordinal(), extensionSelector, deviceId);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj instanceof ExtensionMappingAddressWrapper) {
+            ExtensionMappingAddressWrapper that = (ExtensionMappingAddressWrapper) obj;
+            return Objects.equals(extensionSelector, that.extensionSelector) &&
+                    Objects.equals(deviceId, that.deviceId);
+        }
+        return false;
+    }
+}
diff --git a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/addresses/MappingAddresses.java b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/addresses/MappingAddresses.java
index 6683190..a47dae0 100644
--- a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/addresses/MappingAddresses.java
+++ b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/addresses/MappingAddresses.java
@@ -17,8 +17,6 @@
 
 import org.onlab.packet.IpPrefix;
 import org.onlab.packet.MacAddress;
-import org.onosproject.net.DeviceId;
-import org.onosproject.net.flow.criteria.ExtensionSelector;
 
 /**
  * Factory class for creating various mapping addresses.
@@ -79,16 +77,4 @@
     public static IPMappingAddress ipv6MappingAddress(IpPrefix ip) {
         return new IPMappingAddress(ip, MappingAddress.Type.IPV6);
     }
-
-    /**
-     * Creates an ExtensionMappingAddress using the specified value.
-     *
-     * @param selector extension selector
-     * @param deviceId device identifier
-     * @return mapping address
-     */
-    public static ExtensionMappingAddress extension(ExtensionSelector selector,
-                                                    DeviceId deviceId) {
-        return new ExtensionMappingAddress(selector, deviceId);
-    }
 }
diff --git a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/addresses/UnresolvedExtensionMappingAddress.java b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/addresses/UnresolvedExtensionMappingAddress.java
new file mode 100644
index 0000000..236b77c
--- /dev/null
+++ b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/addresses/UnresolvedExtensionMappingAddress.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright 2016-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.mapping.addresses;
+
+import org.onosproject.net.flow.AbstractExtension;
+
+import java.util.Arrays;
+import java.util.Objects;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
+import static org.onosproject.mapping.addresses.ExtensionMappingAddressType
+                             .ExtensionMappingAddressTypes.UNRESOLVED_TYPE;
+
+/**
+ * Unresolved extension mapping address.
+ */
+public class UnresolvedExtensionMappingAddress extends AbstractExtension
+                                            implements ExtensionMappingAddress {
+
+    private byte[] bytes;
+    private ExtensionMappingAddressType unresolvedAddressType;
+
+    /**
+     * Creates a new unresolved extension mapping address with given data
+     * in byte form.
+     *
+     * @param arrayByte byte data for the extension mapping address
+     * @param type      unresolved extension data type
+     */
+    public UnresolvedExtensionMappingAddress(byte[] arrayByte,
+                                             ExtensionMappingAddressType type) {
+        this.bytes = arrayByte;
+        this.unresolvedAddressType = type;
+    }
+
+    @Override
+    public ExtensionMappingAddressType type() {
+        return UNRESOLVED_TYPE.type();
+    }
+
+    @Override
+    public byte[] serialize() {
+        return bytes;
+    }
+
+    @Override
+    public void deserialize(byte[] data) {
+        bytes = data;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(bytes);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj instanceof UnresolvedExtensionMappingAddress) {
+            UnresolvedExtensionMappingAddress that =
+                                        (UnresolvedExtensionMappingAddress) obj;
+            return Arrays.equals(bytes, that.bytes);
+        }
+        return false;
+    }
+
+    @Override
+    public String toString() {
+        return toStringHelper(type().toString())
+                .add("bytes", bytes)
+                .add("unresolvedAddressType", unresolvedAddressType)
+                .toString();
+    }
+}
diff --git a/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/addresses/MappingAddressesTest.java b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/addresses/MappingAddressesTest.java
index d55bba7..498b4e9 100644
--- a/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/addresses/MappingAddressesTest.java
+++ b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/addresses/MappingAddressesTest.java
@@ -119,7 +119,7 @@
         assertThatClassIsImmutable(ASMappingAddress.class);
         assertThatClassIsImmutable(DNMappingAddress.class);
         assertThatClassIsImmutable(EthMappingAddress.class);
-        assertThatClassIsImmutable(ExtensionMappingAddress.class);
+        assertThatClassIsImmutable(ExtensionMappingAddressWrapper.class);
         assertThatClassIsImmutable(IPMappingAddress.class);
     }