[ONOS-5916] Implement DefaultMapping and DefaultMappingEntry

Change-Id: Icc4b8021e49e5cdfdcf7a0868df4dc5d041d5127
diff --git a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/DefaultMapping.java b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/DefaultMapping.java
index f51a8d1..5c98fee 100644
--- a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/DefaultMapping.java
+++ b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/DefaultMapping.java
@@ -18,40 +18,157 @@
 import org.onosproject.core.ApplicationId;
 import org.onosproject.net.DeviceId;
 
+import java.util.Objects;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
 /**
  * Default implementation class for mapping.
  */
 public class DefaultMapping implements Mapping {
+
+    private final DeviceId deviceId;
+    private final MappingKey key;
+    private final MappingValue value;
+
+    private final MappingId id;
+
+    private final Short appId;
+
+    /**
+     * Creates a mapping specified with mapping information.
+     *
+     * @param mapping mapping information
+     */
+    public DefaultMapping(Mapping mapping) {
+        this.deviceId = mapping.deviceId();
+        this.key = mapping.key();
+        this.value = mapping.value();
+        this.appId = mapping.appId();
+        this.id = mapping.id();
+    }
+
+    /**
+     * Creates a mapping specified with several parameters.
+     *
+     * @param deviceId device identifier
+     * @param key      mapping key
+     * @param value    mapping value
+     * @param id       mapping identifier
+     */
+    public DefaultMapping(DeviceId deviceId, MappingKey key, MappingValue value,
+                          MappingId id) {
+        this.deviceId = deviceId;
+        this.key = key;
+        this.value = value;
+        this.appId = (short) (id.value() >>> 48);
+        this.id = id;
+    }
+
     @Override
     public MappingId id() {
-        return null;
+        return id;
     }
 
     @Override
     public short appId() {
-        return 0;
+        return appId;
     }
 
     @Override
     public DeviceId deviceId() {
-        return null;
+        return deviceId;
+    }
+
+    @Override
+    public MappingKey key() {
+        return key;
+    }
+
+    @Override
+    public MappingValue value() {
+        return value;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(deviceId, key, value, id);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj instanceof DefaultMapping) {
+            DefaultMapping that = (DefaultMapping) obj;
+            return Objects.equals(deviceId, that.deviceId) &&
+                    Objects.equals(key, that.key) &&
+                    Objects.equals(value, that.value) &&
+                    Objects.equals(id, that.id);
+        }
+        return false;
+    }
+
+    @Override
+    public String toString() {
+        return toStringHelper(this)
+                .add("id", Long.toHexString(id.value()))
+                .add("deviceId", deviceId)
+                .add("key", key)
+                .add("value", value)
+                .toString();
     }
 
     public static final class Builder implements Mapping.Builder {
 
+        private MappingId id;
+        private ApplicationId appId;
+        private DeviceId deviceId;
+        private MappingKey key = new DefaultMappingKey();
+        private MappingValue value = new DefaultMappingValue();
+
+        @Override
+        public Mapping.Builder withId(long id) {
+            this.id = MappingId.valueOf(id);
+            return this;
+        }
+
         @Override
         public Mapping.Builder fromApp(ApplicationId appId) {
-            return null;
+            this.appId = appId;
+            return this;
         }
 
         @Override
         public Mapping.Builder forDevice(DeviceId deviceId) {
-            return null;
+            this.deviceId = deviceId;
+            return this;
+        }
+
+        @Override
+        public Mapping.Builder withKey(MappingKey key) {
+            this.key = key;
+            return this;
+        }
+
+        @Override
+        public Mapping.Builder withValue(MappingValue value) {
+            this.value = value;
+            return this;
         }
 
         @Override
         public Mapping build() {
-            return null;
+
+            checkArgument((id != null) ^ (appId != null), "Either an application" +
+                    " id or a mapping id must be supplied");
+            checkNotNull(key, "Mapping key cannot be null");
+            checkNotNull(deviceId, "Must refer to a device");
+
+            return new DefaultMapping(deviceId, key, value, id);
         }
     }
 }
diff --git a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/DefaultMappingEntry.java b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/DefaultMappingEntry.java
index 4c7ae70..cbdce56 100644
--- a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/DefaultMappingEntry.java
+++ b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/DefaultMappingEntry.java
@@ -15,12 +15,41 @@
  */
 package org.onosproject.mapping;
 
+import org.slf4j.Logger;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
+import static org.slf4j.LoggerFactory.getLogger;
+
 /**
  * Default implementation of MappingEntry.
  */
 public class DefaultMappingEntry extends DefaultMapping implements MappingEntry {
+
+    private static final Logger log = getLogger(DefaultMappingEntry.class);
+
+    private MappingEntryState state;
+
+    /**
+     * Creates a mapping entry specified with the mapping, state information.
+     *
+     * @param mapping mapping
+     * @param state   mapping state
+     */
+    public DefaultMappingEntry(Mapping mapping, MappingEntryState state) {
+        super(mapping);
+        this.state = state;
+    }
+
     @Override
     public MappingEntryState state() {
-        return null;
+        return state;
+    }
+
+    @Override
+    public String toString() {
+        return toStringHelper(this)
+                .add("mapping", super.toString())
+                .add("state", state)
+                .toString();
     }
 }
diff --git a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/DefaultMappingKey.java b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/DefaultMappingKey.java
new file mode 100644
index 0000000..96feb4a
--- /dev/null
+++ b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/DefaultMappingKey.java
@@ -0,0 +1,22 @@
+/*
+ * 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;
+
+/**
+ * Default mapping key implementation.
+ */
+public class DefaultMappingKey implements MappingKey {
+}
diff --git a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/DefaultMappingValue.java b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/DefaultMappingValue.java
new file mode 100644
index 0000000..3987218
--- /dev/null
+++ b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/DefaultMappingValue.java
@@ -0,0 +1,22 @@
+/*
+ * 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;
+
+/**
+ * Default mapping value implementation.
+ */
+public class DefaultMappingValue implements MappingValue {
+}
diff --git a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/Mapping.java b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/Mapping.java
index 1233020..8688894 100644
--- a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/Mapping.java
+++ b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/Mapping.java
@@ -45,14 +45,28 @@
     DeviceId deviceId();
 
     /**
-     * {@inheritDoc}
+     * Obtains the mapping key that is used for query the mapping entry.
      *
+     * @return mapping key
+     */
+    MappingKey key();
+
+    /**
+     * Obtains the mapping value that is queried using the mapping key.
+     *
+     * @return mapping value
+     */
+    MappingValue value();
+
+    /**
+     * {@inheritDoc}
+     * <p>
      * Equality for mappings only considers 'match equality'. This means that
      * two mappings with the same match conditions will be equal.
      *
-     * @param   obj   the reference object with which to compare.
-     * @return  {@code true} if this object is the same as the obj
-     *          argument; {@code false} otherwise.
+     * @param obj the reference object with which to compare.
+     * @return {@code true} if this object is the same as the obj
+     * argument; {@code false} otherwise.
      */
     boolean equals(Object obj);
 
@@ -62,6 +76,14 @@
     interface Builder {
 
         /**
+         * Assigns an id value to this mapping.
+         *
+         * @param id a long value
+         * @return this builder object
+         */
+        Builder withId(long id);
+
+        /**
          * Assigns the application that built this mapping to this object.
          * The short value of the appId will be used as a basis for the
          * cookie value computation. It is expected that application use this
@@ -81,6 +103,22 @@
         Builder forDevice(DeviceId deviceId);
 
         /**
+         * Sets the mapping key for this mapping.
+         *
+         * @param key mapping key
+         * @return this builder object
+         */
+        Builder withKey(MappingKey key);
+
+        /**
+         * Sets the mapping value for this mapping.
+         *
+         * @param value mapping value
+         * @return this builder object
+         */
+        Builder withValue(MappingValue value);
+
+        /**
          * Builds a mapping object.
          *
          * @return a mapping object
diff --git a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/MappingKey.java b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/MappingKey.java
new file mode 100644
index 0000000..37c0a1a
--- /dev/null
+++ b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/MappingKey.java
@@ -0,0 +1,22 @@
+/*
+ * 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;
+
+/**
+ * Abstraction of key of mapping information.
+ */
+public interface MappingKey {
+}
diff --git a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/MappingValue.java b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/MappingValue.java
new file mode 100644
index 0000000..5c68eef
--- /dev/null
+++ b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/MappingValue.java
@@ -0,0 +1,22 @@
+/*
+ * 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;
+
+/**
+ * Abstraction of value of mapping information.
+ */
+public interface MappingValue {
+}