[ONOS-7233] Add MastershipRole protobuf model with translators

Change-Id: I02168e91d45c61593a02702b7a05a9810424a2c2
diff --git a/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/cluster/NodeIdProtoTranslator.java b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/cluster/NodeIdProtoTranslator.java
new file mode 100644
index 0000000..f41586d
--- /dev/null
+++ b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/cluster/NodeIdProtoTranslator.java
@@ -0,0 +1,59 @@
+/*
+ * 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.cluster;
+
+import org.onosproject.cluster.NodeId;
+import org.onosproject.grpc.net.cluster.models.NodeIdProtoOuterClass;
+
+/**
+ * gRPC NodeIdProto message to equivalent ONOS NodeId conversion related utilities.
+ */
+public final class NodeIdProtoTranslator {
+
+    /**
+     * Translates gRPC NodeId to {@link NodeId}.
+     *
+     * @param nodeId gRPC message
+     * @return {@link NodeId}
+     */
+    public static NodeId translate(NodeIdProtoOuterClass.NodeIdProto nodeId) {
+        if (nodeId.equals(NodeIdProtoOuterClass.NodeIdProto.getDefaultInstance())) {
+            return null;
+        }
+
+        return NodeId.nodeId(nodeId.getNodeId());
+    }
+
+    /**
+     * Translates {@link NodeId} to gRPC NodeId message.
+     *
+     * @param nodeId {@link NodeId}
+     * @return gRPC NodeId message
+     */
+    public static NodeIdProtoOuterClass.NodeIdProto translate(NodeId nodeId) {
+
+        if (nodeId != null) {
+            return NodeIdProtoOuterClass.NodeIdProto.newBuilder()
+                    .setNodeId(nodeId.id())
+                    .build();
+        }
+
+        return NodeIdProtoOuterClass.NodeIdProto.getDefaultInstance();
+    }
+
+    // Utility class not intended for instantiation.
+    private NodeIdProtoTranslator() {}
+}
diff --git a/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/cluster/RoleInfoProtoTranslator.java b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/cluster/RoleInfoProtoTranslator.java
new file mode 100644
index 0000000..ddea094
--- /dev/null
+++ b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/cluster/RoleInfoProtoTranslator.java
@@ -0,0 +1,67 @@
+/*
+ * 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.cluster;
+
+import com.google.common.collect.Lists;
+import org.onosproject.cluster.NodeId;
+import org.onosproject.cluster.RoleInfo;
+import org.onosproject.grpc.cluster.models.RoleInfoProtoOuterClass;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * gRPC RoleInfoProto message to equivalent ONOS RoleInfo conversion related utilities.
+ */
+public final class RoleInfoProtoTranslator {
+
+    /**
+     * Translates gRPC RoleInfo to {@link RoleInfo}.
+     *
+     * @param roleInfo gRPC message
+     * @return {@link RoleInfo}
+     */
+    public static RoleInfo translate(RoleInfoProtoOuterClass.RoleInfoProto roleInfo) {
+        NodeId master = NodeIdProtoTranslator.translate(roleInfo.getMaster());
+
+        List<NodeId> backups = Lists.newArrayList();
+        roleInfo.getBackupsList().stream().map(r ->
+                NodeIdProtoTranslator.translate(r)).collect(Collectors.toList());
+        return new RoleInfo(master, backups);
+    }
+
+    /**
+     * Translates {@link RoleInfo} to gRPC RoleInfo message.
+     *
+     * @param roleInfo {@link RoleInfo}
+     * @return gRPC RoleInfo message
+     */
+    public static RoleInfoProtoOuterClass.RoleInfoProto translate(RoleInfo roleInfo) {
+
+        if (roleInfo != null) {
+            RoleInfoProtoOuterClass.RoleInfoProto.Builder builder =
+                    RoleInfoProtoOuterClass.RoleInfoProto.newBuilder();
+            builder.setMaster(NodeIdProtoTranslator.translate(roleInfo.master()));
+            roleInfo.backups().forEach(b -> builder.addBackups(NodeIdProtoTranslator.translate(b)));
+            return builder.build();
+        }
+
+        return RoleInfoProtoOuterClass.RoleInfoProto.getDefaultInstance();
+    }
+
+    // Utility class not intended for instantiation.
+    private RoleInfoProtoTranslator() {}
+}
diff --git a/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/cluster/package-info.java b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/cluster/package-info.java
new file mode 100644
index 0000000..0f120f5
--- /dev/null
+++ b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/cluster/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 cluster models.
+ */
+package org.onosproject.incubator.protobuf.models.cluster;
\ No newline at end of file
diff --git a/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/MastershipRoleProtoTranslator.java b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/MastershipRoleProtoTranslator.java
new file mode 100644
index 0000000..d10c621
--- /dev/null
+++ b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/MastershipRoleProtoTranslator.java
@@ -0,0 +1,78 @@
+/*
+ * 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;
+
+import org.onosproject.grpc.net.models.MastershipRoleProtoOuterClass;
+import org.onosproject.net.MastershipRole;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Optional;
+
+/**
+ * gRPC MastershipRoleProto message to equivalent ONOS MastershipRole conversion related utilities.
+ */
+public final class MastershipRoleProtoTranslator {
+
+    private static final Logger log = LoggerFactory.getLogger(MastershipRoleProtoTranslator.class);
+
+    /**
+     * Translates {@link MastershipRole} to gRPC MastershipRole.
+     *
+     * @param mastershipRole {@link MastershipRole}
+     * @return gRPC message
+     */
+    public static MastershipRoleProtoOuterClass.MastershipRoleProto translate(MastershipRole mastershipRole) {
+
+        switch (mastershipRole) {
+            case MASTER:
+                return MastershipRoleProtoOuterClass.MastershipRoleProto.MASTER;
+            case STANDBY:
+                return MastershipRoleProtoOuterClass.MastershipRoleProto.STANDBY;
+            case NONE:
+                return MastershipRoleProtoOuterClass.MastershipRoleProto.NONE;
+
+            default:
+                log.warn("Unexpected mastership role: {}", mastershipRole);
+                return MastershipRoleProtoOuterClass.MastershipRoleProto.NONE;
+        }
+    }
+
+    /**
+     * Translate gRPC MastershipRole to {@link MastershipRole}.
+     *
+     * @param mastershipRole gRPC message
+     * @return {@link MastershipRole}
+     */
+    public static Optional<Object> translate(MastershipRoleProtoOuterClass.MastershipRoleProto mastershipRole) {
+
+        switch (mastershipRole) {
+            case MASTER:
+                return Optional.of(MastershipRole.MASTER);
+            case STANDBY:
+                return Optional.of(MastershipRole.STANDBY);
+            case UNRECOGNIZED:
+                return Optional.of(MastershipRole.NONE);
+
+            default:
+                log.warn("Unexpected mastership role: {}", mastershipRole);
+                return Optional.empty();
+        }
+    }
+
+    // Utility class not intended for instantiation.
+    private MastershipRoleProtoTranslator() {}
+}
diff --git a/incubator/protobuf/models/src/main/proto/cluster/RoleInfoProto.proto b/incubator/protobuf/models/src/main/proto/cluster/RoleInfoProto.proto
new file mode 100644
index 0000000..44dfdaa
--- /dev/null
+++ b/incubator/protobuf/models/src/main/proto/cluster/RoleInfoProto.proto
@@ -0,0 +1,11 @@
+syntax = "proto3";
+option java_package = "org.onosproject.grpc.cluster.models";
+
+import "cluster/NodeIdProto.proto";
+
+package cluster;
+
+message RoleInfoProto {
+    cluster.NodeIdProto master = 1;
+    repeated cluster.NodeIdProto backups = 2;
+}
\ No newline at end of file
diff --git a/incubator/protobuf/models/src/main/proto/net/MastershipRoleProto.proto b/incubator/protobuf/models/src/main/proto/net/MastershipRoleProto.proto
new file mode 100644
index 0000000..d5d9ecb
--- /dev/null
+++ b/incubator/protobuf/models/src/main/proto/net/MastershipRoleProto.proto
@@ -0,0 +1,10 @@
+syntax = "proto3";
+option java_package = "org.onosproject.grpc.net.models";
+
+package net;
+
+enum MastershipRoleProto {
+    NONE = 0;
+    MASTER = 1;
+    STANDBY = 2;
+}
\ No newline at end of file