[ONOS-7233] Add MastershipRole protobuf model with translators

Change-Id: I02168e91d45c61593a02702b7a05a9810424a2c2
This commit is contained in:
Jian Li 2017-11-17 10:54:23 +09:00
parent 448e68499f
commit ec80a3392a
6 changed files with 244 additions and 0 deletions

View File

@ -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() {}
}

View File

@ -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() {}
}

View File

@ -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;

View File

@ -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() {}
}

View File

@ -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;
}

View File

@ -0,0 +1,10 @@
syntax = "proto3";
option java_package = "org.onosproject.grpc.net.models";
package net;
enum MastershipRoleProto {
NONE = 0;
MASTER = 1;
STANDBY = 2;
}