Jian Li | b44ad28 | 2017-12-12 18:10: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.net.flow; |
| 17 | |
| 18 | import org.onosproject.grpc.net.flow.models.FlowEntryEnumsProto; |
| 19 | import org.onosproject.net.flow.FlowEntry.FlowEntryState; |
| 20 | import org.onosproject.net.flow.FlowEntry.FlowLiveType; |
| 21 | import org.slf4j.Logger; |
| 22 | import org.slf4j.LoggerFactory; |
| 23 | |
| 24 | import java.util.Optional; |
| 25 | |
| 26 | |
| 27 | /** |
| 28 | * gRPC FlowEntryEnumsProto message to equivalant ONOS FlowRule enums conversion related utilities. |
| 29 | */ |
| 30 | public final class FlowEntryEnumsProtoTranslator { |
| 31 | |
| 32 | private static final Logger log = LoggerFactory.getLogger(FlowEntryEnumsProtoTranslator.class); |
| 33 | |
| 34 | /** |
| 35 | * Translates {@link FlowEntryState} to gRPC FlowEntryState. |
| 36 | * |
| 37 | * @param flowEntryState {@link FlowEntryState} |
| 38 | * @return gRPC message |
| 39 | */ |
| 40 | public static FlowEntryEnumsProto.FlowEntryStateProto translate(FlowEntryState flowEntryState) { |
| 41 | |
| 42 | switch (flowEntryState) { |
| 43 | case PENDING_ADD: |
| 44 | return FlowEntryEnumsProto.FlowEntryStateProto.PENDING_ADD; |
| 45 | case ADDED: |
| 46 | return FlowEntryEnumsProto.FlowEntryStateProto.ADDED; |
| 47 | case PENDING_REMOVE: |
| 48 | return FlowEntryEnumsProto.FlowEntryStateProto.PENDING_REMOVE; |
| 49 | case REMOVED: |
| 50 | return FlowEntryEnumsProto.FlowEntryStateProto.REMOVED; |
| 51 | case FAILED: |
| 52 | return FlowEntryEnumsProto.FlowEntryStateProto.FAILED; |
| 53 | |
| 54 | default: |
| 55 | log.warn("Unexpected flow entry state: {}", flowEntryState); |
| 56 | return FlowEntryEnumsProto.FlowEntryStateProto.UNRECOGNIZED; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Translates gRPC FlowEntryState to {@link FlowEntryState}. |
| 62 | * |
| 63 | * @param flowEntryState gRPC message |
| 64 | * @return {@link FlowEntryState} |
| 65 | */ |
| 66 | public static Optional<Object> translate(FlowEntryEnumsProto.FlowEntryStateProto flowEntryState) { |
| 67 | |
| 68 | switch (flowEntryState) { |
| 69 | case PENDING_ADD: |
| 70 | return Optional.of(FlowEntryState.PENDING_ADD); |
| 71 | case ADDED: |
| 72 | return Optional.of(FlowEntryState.ADDED); |
| 73 | case PENDING_REMOVE: |
| 74 | return Optional.of(FlowEntryState.PENDING_REMOVE); |
| 75 | case REMOVED: |
| 76 | return Optional.of(FlowEntryState.REMOVED); |
| 77 | case FAILED: |
| 78 | return Optional.of(FlowEntryState.FAILED); |
| 79 | |
| 80 | default: |
| 81 | log.warn("Unexpected flow entry state: {}", flowEntryState); |
| 82 | return Optional.empty(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Translates {@link FlowLiveType} to gRPC FlowLiveType. |
| 88 | * |
| 89 | * @param flowLiveType {@link FlowLiveType} |
| 90 | * @return gRPC message |
| 91 | */ |
| 92 | public static FlowEntryEnumsProto.FlowLiveTypeProto translate(FlowLiveType flowLiveType) { |
| 93 | |
| 94 | switch (flowLiveType) { |
| 95 | case IMMEDIATE: |
| 96 | return FlowEntryEnumsProto.FlowLiveTypeProto.IMMEDIATE; |
| 97 | case SHORT: |
| 98 | return FlowEntryEnumsProto.FlowLiveTypeProto.SHORT; |
| 99 | case MID: |
| 100 | return FlowEntryEnumsProto.FlowLiveTypeProto.MID; |
| 101 | case LONG: |
| 102 | return FlowEntryEnumsProto.FlowLiveTypeProto.LONG; |
| 103 | case UNKNOWN: |
| 104 | return FlowEntryEnumsProto.FlowLiveTypeProto.UNKNOWN; |
| 105 | |
| 106 | default: |
| 107 | log.warn("Unexpected flow live type : {}", flowLiveType); |
| 108 | return FlowEntryEnumsProto.FlowLiveTypeProto.UNRECOGNIZED; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Translates gRPC FlowLiveType to {@link FlowLiveType}. |
| 114 | * |
| 115 | * @param flowLiveType gRPC message |
| 116 | * @return {@link FlowLiveType} |
| 117 | */ |
| 118 | public static Optional<Object> translate(FlowEntryEnumsProto.FlowLiveTypeProto flowLiveType) { |
| 119 | |
| 120 | switch (flowLiveType) { |
| 121 | case IMMEDIATE: |
| 122 | return Optional.of(FlowLiveType.IMMEDIATE); |
| 123 | case SHORT: |
| 124 | return Optional.of(FlowLiveType.SHORT); |
| 125 | case MID: |
| 126 | return Optional.of(FlowLiveType.MID); |
| 127 | case LONG: |
| 128 | return Optional.of(FlowLiveType.LONG); |
| 129 | case UNKNOWN: |
| 130 | return Optional.of(FlowLiveType.UNKNOWN); |
| 131 | |
| 132 | default: |
| 133 | log.warn("Unexpected flow live type : {}", flowLiveType); |
| 134 | return Optional.empty(); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // Utility class not intended for instantiation. |
| 139 | private FlowEntryEnumsProtoTranslator() {} |
| 140 | } |