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.core; |
| 17 | |
| 18 | import org.onosproject.app.ApplicationState; |
| 19 | import org.onosproject.core.ApplicationRole; |
| 20 | import org.onosproject.grpc.app.models.ApplicationEnumsProto.ApplicationRoleProto; |
| 21 | import org.onosproject.grpc.app.models.ApplicationEnumsProto.ApplicationStateProto; |
| 22 | import org.slf4j.Logger; |
| 23 | import org.slf4j.LoggerFactory; |
| 24 | |
| 25 | import java.util.Optional; |
| 26 | |
| 27 | /** |
| 28 | * gRPC ApplicationEnumsProto message to equivalent ONOS Application Enums conversion related utilities. |
| 29 | */ |
| 30 | public final class ApplicationEnumsProtoTranslator { |
| 31 | |
| 32 | private static final Logger log = LoggerFactory.getLogger(ApplicationEnumsProtoTranslator.class); |
| 33 | |
| 34 | /** |
| 35 | * Translates {@link ApplicationRole} to gRPC ApplicationRole. |
| 36 | * |
| 37 | * @param role {@link ApplicationRole} |
| 38 | * @return gRPC message |
| 39 | */ |
| 40 | public static ApplicationRoleProto translate(ApplicationRole role) { |
| 41 | |
| 42 | switch (role) { |
| 43 | case USER: |
| 44 | return ApplicationRoleProto.USER; |
| 45 | case ADMIN: |
| 46 | return ApplicationRoleProto.ADMIN; |
| 47 | case UNSPECIFIED: |
| 48 | return ApplicationRoleProto.UNSPECIFIED; |
| 49 | |
| 50 | default: |
| 51 | log.warn("Unexpected application role: {}", role); |
| 52 | return ApplicationRoleProto.UNSPECIFIED; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Translates gRPC ApplicationRole to {@link ApplicationRole}. |
| 58 | * |
| 59 | * @param roleProto gRPC message |
| 60 | * @return {@link ApplicationRole} |
| 61 | */ |
Jian Li | 1aaa64d | 2017-12-14 11:25:39 +0900 | [diff] [blame] | 62 | public static Optional<ApplicationRole> translate(ApplicationRoleProto roleProto) { |
Jian Li | 8780edc | 2017-08-26 02:44:29 +0900 | [diff] [blame] | 63 | |
| 64 | switch (roleProto) { |
| 65 | case USER: |
| 66 | return Optional.of(ApplicationRole.USER); |
| 67 | case ADMIN: |
| 68 | return Optional.of(ApplicationRole.ADMIN); |
| 69 | case UNSPECIFIED: |
| 70 | return Optional.of(ApplicationRole.UNSPECIFIED); |
| 71 | |
| 72 | default: |
| 73 | log.warn("Unexpected application role proto: {}", roleProto); |
| 74 | return Optional.empty(); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Translate {@link ApplicationState} to gRPC ApplicationState. |
| 80 | * |
| 81 | * @param state {@link ApplicationState} |
| 82 | * @return gRPC message |
| 83 | */ |
| 84 | public static ApplicationStateProto translate(ApplicationState state) { |
| 85 | |
| 86 | switch (state) { |
| 87 | case ACTIVE: |
| 88 | return ApplicationStateProto.ACTIVE; |
| 89 | case INSTALLED: |
| 90 | return ApplicationStateProto.INSTALLED; |
| 91 | |
| 92 | default: |
| 93 | log.warn("Unexpected application state: {}", state); |
| 94 | return ApplicationStateProto.INSTALLED; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Translate gRPC ApplicationState to {@link ApplicationState}. |
| 100 | * |
| 101 | * @param stateProto gRPC message |
| 102 | * @return {@link ApplicationState} |
| 103 | */ |
Jian Li | 1aaa64d | 2017-12-14 11:25:39 +0900 | [diff] [blame] | 104 | public static Optional<ApplicationState> translate(ApplicationStateProto stateProto) { |
Jian Li | 8780edc | 2017-08-26 02:44:29 +0900 | [diff] [blame] | 105 | |
| 106 | switch (stateProto) { |
| 107 | case ACTIVE: |
| 108 | return Optional.of(ApplicationState.ACTIVE); |
| 109 | case INSTALLED: |
| 110 | return Optional.of(ApplicationState.INSTALLED); |
| 111 | |
| 112 | default: |
| 113 | log.warn("Unexpected application state proto: {}", stateProto); |
| 114 | return Optional.empty(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // Utility class not intended for instantiation. |
| 119 | private ApplicationEnumsProtoTranslator() {} |
| 120 | } |