blob: 12ddf298a9f20f12c8a1901c49c5002a5894a020 [file] [log] [blame]
Jian Li02cfc7f2017-12-13 16:54:21 +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.flow;
17
18import org.onosproject.grpc.net.flow.models.FlowRuleEnumsProto;
19import org.onosproject.net.flow.FlowRule.FlowRemoveReason;
20import org.slf4j.Logger;
21import org.slf4j.LoggerFactory;
22
23import java.util.Optional;
24
25/**
26 * gRPC FlowRuleEnumsProto message to equivalent ONOS FlowRule enums conversion related utilities.
27 */
28public final class FlowRuleEnumsProtoTranslator {
29
30 private static final Logger log = LoggerFactory.getLogger(FlowRuleEnumsProtoTranslator.class);
31
32 /**
33 * Translates {@link FlowRemoveReason} to gRPC FlowRemoveReason.
34 *
35 * @param flowRemoveReason {@link FlowRemoveReason}
36 * @return gRPC message
37 */
38 public static FlowRuleEnumsProto.FlowRemoveReasonProto translate(FlowRemoveReason flowRemoveReason) {
39
40 switch (flowRemoveReason) {
41 case DELETE:
42 return FlowRuleEnumsProto.FlowRemoveReasonProto.DELETE;
43 case EVICTION:
44 return FlowRuleEnumsProto.FlowRemoveReasonProto.EVICTION;
45 case GROUP_DELETE:
46 return FlowRuleEnumsProto.FlowRemoveReasonProto.GROUP_DELETE;
47 case METER_DELETE:
48 return FlowRuleEnumsProto.FlowRemoveReasonProto.METER_DELETE;
49 case HARD_TIMEOUT:
50 return FlowRuleEnumsProto.FlowRemoveReasonProto.HARD_TIMEOUT;
51 case IDLE_TIMEOUT:
52 return FlowRuleEnumsProto.FlowRemoveReasonProto.IDLE_TIMEOUT;
53 case NO_REASON:
54 return FlowRuleEnumsProto.FlowRemoveReasonProto.NO_REASON;
55 default:
56 log.warn("Unexpected flow remove reason: {}", flowRemoveReason);
57 return FlowRuleEnumsProto.FlowRemoveReasonProto.UNRECOGNIZED;
58 }
59 }
60
61 /**
62 * Translates gRPC FlowRemoveReason to {@link FlowRemoveReason}.
63 *
64 * @param flowRemoveReason gRPC message
65 * @return {@link FlowRemoveReason}
66 */
Jian Li1aaa64d2017-12-14 11:25:39 +090067 public static Optional<FlowRemoveReason> translate(FlowRuleEnumsProto.FlowRemoveReasonProto flowRemoveReason) {
Jian Li02cfc7f2017-12-13 16:54:21 +090068
69 switch (flowRemoveReason) {
70 case DELETE:
71 return Optional.of(FlowRemoveReason.DELETE);
72 case EVICTION:
73 return Optional.of(FlowRemoveReason.EVICTION);
74 case GROUP_DELETE:
75 return Optional.of(FlowRemoveReason.GROUP_DELETE);
76 case METER_DELETE:
77 return Optional.of(FlowRemoveReason.METER_DELETE);
78 case HARD_TIMEOUT:
79 return Optional.of(FlowRemoveReason.HARD_TIMEOUT);
80 case IDLE_TIMEOUT:
81 return Optional.of(FlowRemoveReason.IDLE_TIMEOUT);
82 case NO_REASON:
83 return Optional.of(FlowRemoveReason.NO_REASON);
84 default:
85 log.warn("Unexpected flow remove reason: {}", flowRemoveReason);
86 return Optional.empty();
87 }
88 }
89
90 // Utility class not intended for instantiation.
91 private FlowRuleEnumsProtoTranslator() {}
92}