blob: 76046e72f99034b894e9007df26187e32ef454c8 [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.FlowRuleProtoOuterClass.FlowRuleProto;
19import org.onosproject.net.DeviceId;
20import org.onosproject.net.flow.DefaultFlowRule;
21import org.onosproject.net.flow.FlowRule;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25/**
26 * gRPC FlowRuleProto message to equivalent ONOS FlowRule conversion related utilities.
27 */
28public final class FlowRuleProtoTranslator {
29
30 private static final Logger log = LoggerFactory.getLogger(FlowRuleProtoTranslator.class);
31
32 /**
33 * Translates {@link FlowRule} to gRPC FlowRuleProto.
34 *
35 * @param flowRule {@link FlowRule}
36 * @return gRPC message
37 */
38 public static FlowRuleProto translate(FlowRule flowRule) {
39
40 if (flowRule != null) {
41 FlowRuleProto.Builder builder = FlowRuleProto.newBuilder();
42 builder.setAppId(flowRule.appId())
43 .setDeviceId(flowRule.deviceId().toString())
44 .setFlowId(flowRule.id().value())
45 .setPermanent(flowRule.isPermanent())
46 .setPriority(flowRule.priority())
47 .setReason(FlowRuleEnumsProtoTranslator.translate(flowRule.reason()))
48 .setTableId(flowRule.tableId())
49 .setTableName(flowRule.table().toString());
50
51 // TODO: need to set TrafficTreatment and TrafficSelector
52
53 return builder.build();
54 }
55 return FlowRuleProto.getDefaultInstance();
56 }
57
58 /**
59 * Translates gRPC FlowRule to {@link FlowRule}.
60 *
61 * @param flowRule gRPC message
62 * @return {@link FlowRule}
63 */
64 public static FlowRule translate(FlowRuleProto flowRule) {
65
66 if (flowRule.equals(FlowRuleProto.getDefaultInstance())) {
67 return null;
68 }
69
70 DeviceId deviceId = DeviceId.deviceId(flowRule.getDeviceId());
71
72 // TODO: to register AppId need to find a way to get CoreService
73
Jian Li1aaa64d2017-12-14 11:25:39 +090074 FlowRule.FlowRemoveReason reason =
Jian Li02cfc7f2017-12-13 16:54:21 +090075 FlowRuleEnumsProtoTranslator.translate(flowRule.getReason()).get();
76
77 FlowRule.Builder resultBuilder = new DefaultFlowRule.Builder();
78 resultBuilder.forDevice(deviceId);
79 resultBuilder.forTable(flowRule.getTableId());
80 resultBuilder.withPriority(flowRule.getPriority());
81 resultBuilder.withCookie(flowRule.getFlowId());
82 resultBuilder.withReason(reason);
83
84 if (flowRule.getPermanent()) {
85 resultBuilder.makePermanent();
86 } else {
87 resultBuilder.makeTemporary(flowRule.getTimeout());
88 }
89
90 // TODO: need to deal with TrafficTreatment and TrafficSelector
91
92 return resultBuilder.build();
93 }
94
95 // Utility class not intended for instantiation.
96 private FlowRuleProtoTranslator() {}
97}