blob: 6e0c24355deefa3454e5881ac3c3bec595f0c24e [file] [log] [blame]
Jian Li8780edc2017-08-26 02:44:29 +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.core;
17
18import org.onosproject.core.Version;
19import org.onosproject.grpc.core.models.VersionProtoOuterClass.VersionProto;
20
21import static org.onosproject.grpc.core.models.VersionProtoOuterClass.VersionProto.getDefaultInstance;
22
23/**
24 * gRPC Version message to equivalent ONOS Version conversion related utilities.
25 */
26public final class VersionProtoTranslator {
27
28 /**
29 * Translates {@link Version} to gRPC version message.
30 *
31 * @param version {@link Version}
32 * @return gRPC message
33 */
34 public static VersionProto translate(Version version) {
35
36 if (version != null) {
37 return VersionProto.newBuilder()
38 .setMajor(version.major())
39 .setMinor(version.minor())
40 .setPatch(version.patch())
41 .setBuild(version.build())
42 .build();
43 }
44
45 return getDefaultInstance();
46 }
47
48 /**
49 * Translates gRPC version message to {@link Version}.
50 *
51 * @param version gRPC message
52 * @return {@link Version}
53 */
54 public static Version translate(VersionProto version) {
55
56 return Version.version(version.getMajor(), version.getMinor(),
57 version.getPatch(), version.getBuild());
58 }
59
60 // Utility class not intended for instantiation.
61 private VersionProtoTranslator() {}
62}