Jian Li | 8780edc | 2017-08-26 02:44:29 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.incubator.protobuf.models.security; |
| 17 | |
| 18 | import org.onosproject.grpc.security.models.PermissionProtoOuterClass.PermissionProto; |
| 19 | import org.onosproject.security.Permission; |
| 20 | |
| 21 | import static org.onosproject.grpc.security.models.PermissionProtoOuterClass.PermissionProto.getDefaultInstance; |
| 22 | |
| 23 | /** |
| 24 | * gRPC Permission message to equivalent ONOS Permission conversion related utilities. |
| 25 | */ |
| 26 | public final class PermissionProtoTranslator { |
| 27 | |
| 28 | /** |
| 29 | * Translate {@link Permission} to gRPC permission message. |
| 30 | * |
| 31 | * @param permission {@link Permission} |
| 32 | * @return gRPC message |
| 33 | */ |
| 34 | public static PermissionProto translate(Permission permission) { |
| 35 | |
| 36 | if (permission != null) { |
| 37 | return PermissionProto.newBuilder() |
| 38 | .setActions(permission.getActions()) |
| 39 | .setClassname(permission.getClassName()) |
| 40 | .setName(permission.getName()) |
| 41 | .build(); |
| 42 | } |
| 43 | |
| 44 | return getDefaultInstance(); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Translate gRPC permission message to {@link Permission}. |
| 49 | * |
| 50 | * @param permission gRPC message |
| 51 | * @return {@link Permission} |
| 52 | */ |
| 53 | public static Permission translate(PermissionProto permission) { |
| 54 | |
| 55 | return new Permission(permission.getClassname(), |
| 56 | permission.getName(), permission.getActions()); |
| 57 | } |
| 58 | |
| 59 | // Utility class not intended for instantiation. |
| 60 | private PermissionProtoTranslator() {} |
| 61 | } |