blob: 9082f27d5675e57a72417f05a6de19f7c40e7593 [file] [log] [blame]
Jian Lib44ad282017-12-12 18:10: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.net.flow;
17
18import org.onosproject.grpc.net.flow.models.FlowEntryProtoOuterClass.FlowEntryProto;
19import org.onosproject.net.flow.DefaultFlowEntry;
20import org.onosproject.net.flow.FlowEntry;
21import org.onosproject.net.flow.FlowRule;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25/**
26 * gRPC FlowEntryProto message to equivalent ONOS FlowEntry conversion related utilities.
27 */
28public final class FlowEntryProtoTranslator {
29
30 private static final Logger log = LoggerFactory.getLogger(FlowEntryProtoTranslator.class);
31
32 /**
33 * Translates {@link FlowRule} to gRPC FlowRuleProto.
34 *
35 * @param flowEntry {@link FlowRule}
36 * @return gRPC message
37 */
38 public static FlowEntryProto translate(FlowEntry flowEntry) {
39
40 if (flowEntry != null) {
41 FlowEntryProto.Builder builder = FlowEntryProto.newBuilder();
42 builder.setLife(flowEntry.life())
43 .setPackets(flowEntry.packets())
44 .setBytes(flowEntry.bytes())
45 .setLastSeen(flowEntry.lastSeen())
46 .setErrType(flowEntry.errType())
47 .setErrCode(flowEntry.errCode())
48 .setState(FlowEntryEnumsProtoTranslator.translate(flowEntry.state()))
49 .setLiveType(FlowEntryEnumsProtoTranslator.translate(flowEntry.liveType()));
50 return builder.build();
51 }
52
53 return FlowEntryProto.getDefaultInstance();
54 }
55
56 /**
57 * Translates gRPC FlowRule to {@link FlowRule}.
58 *
59 * @param flowEntry gRPC message
60 * @return {@link FlowRule}
61 */
62 public static FlowEntry translate(FlowEntryProto flowEntry) {
63 if (flowEntry.equals(FlowEntryProto.getDefaultInstance())) {
64 return null;
65 }
66
67 FlowEntry.FlowEntryState state = (FlowEntry.FlowEntryState)
68 FlowEntryEnumsProtoTranslator.translate(flowEntry.getState()).get();
69 FlowEntry.FlowLiveType liveType = (FlowEntry.FlowLiveType)
70 FlowEntryEnumsProtoTranslator.translate(flowEntry.getLiveType()).get();
71
72 // TODO: need to instantiate FlowRule later
73 return new DefaultFlowEntry(null, state, flowEntry.getLife(), liveType,
74 flowEntry.getPackets(), flowEntry.getBytes());
75 }
76
77 // Utility class not intended for instantiation.
78 private FlowEntryProtoTranslator() {}
79}