blob: d10c62172225a6ec253794a091ed402723424920 [file] [log] [blame]
Jian Liec80a332017-11-17 10:54:23 +09001/*
2 * Copyright 2017-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.incubator.protobuf.models.net;
17
18import org.onosproject.grpc.net.models.MastershipRoleProtoOuterClass;
19import org.onosproject.net.MastershipRole;
20import org.slf4j.Logger;
21import org.slf4j.LoggerFactory;
22
23import java.util.Optional;
24
25/**
26 * gRPC MastershipRoleProto message to equivalent ONOS MastershipRole conversion related utilities.
27 */
28public final class MastershipRoleProtoTranslator {
29
30 private static final Logger log = LoggerFactory.getLogger(MastershipRoleProtoTranslator.class);
31
32 /**
33 * Translates {@link MastershipRole} to gRPC MastershipRole.
34 *
35 * @param mastershipRole {@link MastershipRole}
36 * @return gRPC message
37 */
38 public static MastershipRoleProtoOuterClass.MastershipRoleProto translate(MastershipRole mastershipRole) {
39
40 switch (mastershipRole) {
41 case MASTER:
42 return MastershipRoleProtoOuterClass.MastershipRoleProto.MASTER;
43 case STANDBY:
44 return MastershipRoleProtoOuterClass.MastershipRoleProto.STANDBY;
45 case NONE:
46 return MastershipRoleProtoOuterClass.MastershipRoleProto.NONE;
47
48 default:
49 log.warn("Unexpected mastership role: {}", mastershipRole);
50 return MastershipRoleProtoOuterClass.MastershipRoleProto.NONE;
51 }
52 }
53
54 /**
55 * Translate gRPC MastershipRole to {@link MastershipRole}.
56 *
57 * @param mastershipRole gRPC message
58 * @return {@link MastershipRole}
59 */
60 public static Optional<Object> translate(MastershipRoleProtoOuterClass.MastershipRoleProto mastershipRole) {
61
62 switch (mastershipRole) {
63 case MASTER:
64 return Optional.of(MastershipRole.MASTER);
65 case STANDBY:
66 return Optional.of(MastershipRole.STANDBY);
67 case UNRECOGNIZED:
68 return Optional.of(MastershipRole.NONE);
69
70 default:
71 log.warn("Unexpected mastership role: {}", mastershipRole);
72 return Optional.empty();
73 }
74 }
75
76 // Utility class not intended for instantiation.
77 private MastershipRoleProtoTranslator() {}
78}