[ONOS-7321] Add gRPC FlowEntry protobuf model with translator

Change-Id: I220058603d27e6d2048c328500e4f9f6721a89a1
diff --git a/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/flow/FlowEntryEnumsProtoTranslator.java b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/flow/FlowEntryEnumsProtoTranslator.java
new file mode 100644
index 0000000..1c61a68
--- /dev/null
+++ b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/flow/FlowEntryEnumsProtoTranslator.java
@@ -0,0 +1,140 @@
+/*
+ * 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.onosproject.incubator.protobuf.models.net.flow;
+
+import org.onosproject.grpc.net.flow.models.FlowEntryEnumsProto;
+import org.onosproject.net.flow.FlowEntry.FlowEntryState;
+import org.onosproject.net.flow.FlowEntry.FlowLiveType;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Optional;
+
+
+/**
+ * gRPC FlowEntryEnumsProto message to equivalant ONOS FlowRule enums conversion related utilities.
+ */
+public final class FlowEntryEnumsProtoTranslator {
+
+    private static final Logger log = LoggerFactory.getLogger(FlowEntryEnumsProtoTranslator.class);
+
+    /**
+     * Translates {@link FlowEntryState} to gRPC FlowEntryState.
+     *
+     * @param flowEntryState {@link FlowEntryState}
+     * @return gRPC message
+     */
+    public static FlowEntryEnumsProto.FlowEntryStateProto translate(FlowEntryState flowEntryState) {
+
+        switch (flowEntryState) {
+            case PENDING_ADD:
+                return FlowEntryEnumsProto.FlowEntryStateProto.PENDING_ADD;
+            case ADDED:
+                return FlowEntryEnumsProto.FlowEntryStateProto.ADDED;
+            case PENDING_REMOVE:
+                return FlowEntryEnumsProto.FlowEntryStateProto.PENDING_REMOVE;
+            case REMOVED:
+                return FlowEntryEnumsProto.FlowEntryStateProto.REMOVED;
+            case FAILED:
+                return FlowEntryEnumsProto.FlowEntryStateProto.FAILED;
+
+            default:
+                log.warn("Unexpected flow entry state: {}", flowEntryState);
+                return FlowEntryEnumsProto.FlowEntryStateProto.UNRECOGNIZED;
+        }
+    }
+
+    /**
+     * Translates gRPC FlowEntryState to {@link FlowEntryState}.
+     *
+     * @param flowEntryState gRPC message
+     * @return {@link FlowEntryState}
+     */
+    public static Optional<Object> translate(FlowEntryEnumsProto.FlowEntryStateProto flowEntryState) {
+
+        switch (flowEntryState) {
+            case PENDING_ADD:
+                return Optional.of(FlowEntryState.PENDING_ADD);
+            case ADDED:
+                return Optional.of(FlowEntryState.ADDED);
+            case PENDING_REMOVE:
+                return Optional.of(FlowEntryState.PENDING_REMOVE);
+            case REMOVED:
+                return Optional.of(FlowEntryState.REMOVED);
+            case FAILED:
+                return Optional.of(FlowEntryState.FAILED);
+
+            default:
+                log.warn("Unexpected flow entry state: {}", flowEntryState);
+                return Optional.empty();
+        }
+    }
+
+    /**
+     * Translates {@link FlowLiveType} to gRPC FlowLiveType.
+     *
+     * @param flowLiveType {@link FlowLiveType}
+     * @return gRPC message
+     */
+    public static FlowEntryEnumsProto.FlowLiveTypeProto translate(FlowLiveType flowLiveType) {
+
+        switch (flowLiveType) {
+            case IMMEDIATE:
+                return FlowEntryEnumsProto.FlowLiveTypeProto.IMMEDIATE;
+            case SHORT:
+                return FlowEntryEnumsProto.FlowLiveTypeProto.SHORT;
+            case MID:
+                return FlowEntryEnumsProto.FlowLiveTypeProto.MID;
+            case LONG:
+                return FlowEntryEnumsProto.FlowLiveTypeProto.LONG;
+            case UNKNOWN:
+                return FlowEntryEnumsProto.FlowLiveTypeProto.UNKNOWN;
+
+            default:
+                log.warn("Unexpected flow live type : {}", flowLiveType);
+                return FlowEntryEnumsProto.FlowLiveTypeProto.UNRECOGNIZED;
+        }
+    }
+
+    /**
+     * Translates gRPC FlowLiveType to {@link FlowLiveType}.
+     *
+     * @param flowLiveType gRPC message
+     * @return {@link FlowLiveType}
+     */
+    public static Optional<Object> translate(FlowEntryEnumsProto.FlowLiveTypeProto flowLiveType) {
+
+        switch (flowLiveType) {
+            case IMMEDIATE:
+                return Optional.of(FlowLiveType.IMMEDIATE);
+            case SHORT:
+                return Optional.of(FlowLiveType.SHORT);
+            case MID:
+                return Optional.of(FlowLiveType.MID);
+            case LONG:
+                return Optional.of(FlowLiveType.LONG);
+            case UNKNOWN:
+                return Optional.of(FlowLiveType.UNKNOWN);
+
+            default:
+                log.warn("Unexpected flow live type : {}", flowLiveType);
+                return Optional.empty();
+        }
+    }
+
+    // Utility class not intended for instantiation.
+    private FlowEntryEnumsProtoTranslator() {}
+}
diff --git a/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/flow/FlowEntryProtoTranslator.java b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/flow/FlowEntryProtoTranslator.java
new file mode 100644
index 0000000..9082f27
--- /dev/null
+++ b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/flow/FlowEntryProtoTranslator.java
@@ -0,0 +1,79 @@
+/*
+ * 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.onosproject.incubator.protobuf.models.net.flow;
+
+import org.onosproject.grpc.net.flow.models.FlowEntryProtoOuterClass.FlowEntryProto;
+import org.onosproject.net.flow.DefaultFlowEntry;
+import org.onosproject.net.flow.FlowEntry;
+import org.onosproject.net.flow.FlowRule;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * gRPC FlowEntryProto message to equivalent ONOS FlowEntry conversion related utilities.
+ */
+public final class FlowEntryProtoTranslator {
+
+    private static final Logger log = LoggerFactory.getLogger(FlowEntryProtoTranslator.class);
+
+    /**
+     * Translates {@link FlowRule} to gRPC FlowRuleProto.
+     *
+     * @param flowEntry {@link FlowRule}
+     * @return gRPC message
+     */
+    public static FlowEntryProto translate(FlowEntry flowEntry) {
+
+        if (flowEntry != null) {
+            FlowEntryProto.Builder builder = FlowEntryProto.newBuilder();
+            builder.setLife(flowEntry.life())
+                    .setPackets(flowEntry.packets())
+                    .setBytes(flowEntry.bytes())
+                    .setLastSeen(flowEntry.lastSeen())
+                    .setErrType(flowEntry.errType())
+                    .setErrCode(flowEntry.errCode())
+                    .setState(FlowEntryEnumsProtoTranslator.translate(flowEntry.state()))
+                    .setLiveType(FlowEntryEnumsProtoTranslator.translate(flowEntry.liveType()));
+            return builder.build();
+        }
+
+        return FlowEntryProto.getDefaultInstance();
+    }
+
+    /**
+     * Translates gRPC FlowRule to {@link FlowRule}.
+     *
+     * @param flowEntry gRPC message
+     * @return {@link FlowRule}
+     */
+    public static FlowEntry translate(FlowEntryProto flowEntry) {
+        if (flowEntry.equals(FlowEntryProto.getDefaultInstance())) {
+            return null;
+        }
+
+        FlowEntry.FlowEntryState state = (FlowEntry.FlowEntryState)
+                FlowEntryEnumsProtoTranslator.translate(flowEntry.getState()).get();
+        FlowEntry.FlowLiveType liveType = (FlowEntry.FlowLiveType)
+                FlowEntryEnumsProtoTranslator.translate(flowEntry.getLiveType()).get();
+
+        // TODO: need to instantiate FlowRule later
+        return new DefaultFlowEntry(null, state, flowEntry.getLife(), liveType,
+                flowEntry.getPackets(), flowEntry.getBytes());
+    }
+
+    // Utility class not intended for instantiation.
+    private FlowEntryProtoTranslator() {}
+}
diff --git a/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/flow/package-info.java b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/flow/package-info.java
new file mode 100644
index 0000000..08d0df9
--- /dev/null
+++ b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/flow/package-info.java
@@ -0,0 +1,19 @@
+/*
+ * 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.
+ */
+/**
+ * Utilities to handle ProtoBuf version of ONOS flow models.
+ */
+package org.onosproject.incubator.protobuf.models.net.flow;
\ No newline at end of file
diff --git a/incubator/protobuf/models/src/main/proto/net/flow/FlowEntryEnumsProto.proto b/incubator/protobuf/models/src/main/proto/net/flow/FlowEntryEnumsProto.proto
new file mode 100644
index 0000000..2bb6768
--- /dev/null
+++ b/incubator/protobuf/models/src/main/proto/net/flow/FlowEntryEnumsProto.proto
@@ -0,0 +1,62 @@
+/*
+ * 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.
+ */
+syntax = "proto3";
+option java_package = "org.onosproject.grpc.net.flow.models";
+
+package net.flow;
+
+// Corresponds to org.onosproject.net.flow.FlowEntry.
+enum FlowEntryStateProto {
+
+    // Indicates that this rule has been submitted for addition.
+    // Not necessarily in the flow table.
+    PENDING_ADD = 0;
+
+    // Rule has been added which means it is in the flow table.
+    ADDED = 1;
+
+    // Flow has been marked for removal, might still be in flow table.
+    PENDING_REMOVE = 2;
+
+    // Flow has been removed from flow table and can be purged.
+    REMOVED = 3;
+
+    // Indicates that the installation of this flow has failed.
+    FAILED = 4;
+}
+
+enum FlowLiveTypeProto {
+
+    // Indicates that this rule has been submitted for addition immediately.
+    // Not necessarily collecting flow stats.
+    IMMEDIATE = 0;
+
+    // Indicates that this rule has been submitted for a short time.
+    // Collecting flow stats every SHORT interval, defined by the implementation.
+    SHORT = 1;
+
+    // Indicates that this rule has been submitted for a mid time.
+    // Collecting flow stats every MID interval, defined by the implementation.
+    MID = 2;
+
+    // Indicates that this rule has been submitted for a long time.
+    // Collecting flow stats every LONG interval, defined by the implementation.
+    LONG = 3;
+
+    // Indicates that this rule has been submitted for UNKNOWN or ERROR.
+    // Not necessarily collecting flow stats.
+    UNKNOWN = 4;
+}
\ No newline at end of file
diff --git a/incubator/protobuf/models/src/main/proto/net/flow/FlowEntryProto.proto b/incubator/protobuf/models/src/main/proto/net/flow/FlowEntryProto.proto
new file mode 100644
index 0000000..c28a624
--- /dev/null
+++ b/incubator/protobuf/models/src/main/proto/net/flow/FlowEntryProto.proto
@@ -0,0 +1,34 @@
+/*
+ * 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.
+ */
+syntax = "proto3";
+option java_package = "org.onosproject.grpc.net.flow.models";
+
+package net.flow;
+
+import "net/flow/FlowEntryEnumsProto.proto";
+
+// Corresponds to org.onosproject.net.flow.DefaultFlowEntry.
+message FlowEntryProto {
+    int64 life = 1;
+    int64 packets = 2;
+    int64 bytes = 3;
+    int64 last_seen = 4;
+    int32 err_type = 5;
+    int32 err_code = 6;
+
+    net.flow.FlowEntryStateProto state = 7;
+    net.flow.FlowLiveTypeProto live_type = 8;
+}
\ No newline at end of file