blob: 9082f27d5675e57a72417f05a6de19f7c40e7593 [file] [log] [blame]
/*
* Copyright 2017-present Open Networking Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.incubator.protobuf.models.net.flow;
import org.onosproject.grpc.net.flow.models.FlowEntryProtoOuterClass.FlowEntryProto;
import org.onosproject.net.flow.DefaultFlowEntry;
import org.onosproject.net.flow.FlowEntry;
import org.onosproject.net.flow.FlowRule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* gRPC FlowEntryProto message to equivalent ONOS FlowEntry conversion related utilities.
*/
public final class FlowEntryProtoTranslator {
private static final Logger log = LoggerFactory.getLogger(FlowEntryProtoTranslator.class);
/**
* Translates {@link FlowRule} to gRPC FlowRuleProto.
*
* @param flowEntry {@link FlowRule}
* @return gRPC message
*/
public static FlowEntryProto translate(FlowEntry flowEntry) {
if (flowEntry != null) {
FlowEntryProto.Builder builder = FlowEntryProto.newBuilder();
builder.setLife(flowEntry.life())
.setPackets(flowEntry.packets())
.setBytes(flowEntry.bytes())
.setLastSeen(flowEntry.lastSeen())
.setErrType(flowEntry.errType())
.setErrCode(flowEntry.errCode())
.setState(FlowEntryEnumsProtoTranslator.translate(flowEntry.state()))
.setLiveType(FlowEntryEnumsProtoTranslator.translate(flowEntry.liveType()));
return builder.build();
}
return FlowEntryProto.getDefaultInstance();
}
/**
* Translates gRPC FlowRule to {@link FlowRule}.
*
* @param flowEntry gRPC message
* @return {@link FlowRule}
*/
public static FlowEntry translate(FlowEntryProto flowEntry) {
if (flowEntry.equals(FlowEntryProto.getDefaultInstance())) {
return null;
}
FlowEntry.FlowEntryState state = (FlowEntry.FlowEntryState)
FlowEntryEnumsProtoTranslator.translate(flowEntry.getState()).get();
FlowEntry.FlowLiveType liveType = (FlowEntry.FlowLiveType)
FlowEntryEnumsProtoTranslator.translate(flowEntry.getLiveType()).get();
// TODO: need to instantiate FlowRule later
return new DefaultFlowEntry(null, state, flowEntry.getLife(), liveType,
flowEntry.getPackets(), flowEntry.getBytes());
}
// Utility class not intended for instantiation.
private FlowEntryProtoTranslator() {}
}