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