[ONOS-7327] Add FlowRule and FlowRuleEnum protobuf translator
Change-Id: I1c8145772c6d8a47fa672d26faa58acb35c89ffd
diff --git a/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/flow/FlowEntryEnumsProtoTranslator.java b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/flow/FlowEntryEnumsProtoTranslator.java
index 1c61a68..2c7d51e 100644
--- a/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/flow/FlowEntryEnumsProtoTranslator.java
+++ b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/flow/FlowEntryEnumsProtoTranslator.java
@@ -25,7 +25,7 @@
/**
- * gRPC FlowEntryEnumsProto message to equivalant ONOS FlowRule enums conversion related utilities.
+ * gRPC FlowEntryEnumsProto message to equivalent ONOS FlowEntry enums conversion related utilities.
*/
public final class FlowEntryEnumsProtoTranslator {
diff --git a/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/flow/FlowEntryProtoTranslator.java b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/flow/FlowEntryProtoTranslator.java
index 9082f27..dfbbdf8 100644
--- a/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/flow/FlowEntryProtoTranslator.java
+++ b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/flow/FlowEntryProtoTranslator.java
@@ -18,7 +18,6 @@
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;
@@ -30,9 +29,9 @@
private static final Logger log = LoggerFactory.getLogger(FlowEntryProtoTranslator.class);
/**
- * Translates {@link FlowRule} to gRPC FlowRuleProto.
+ * Translates {@link FlowEntry} to gRPC FlowEntryProto.
*
- * @param flowEntry {@link FlowRule}
+ * @param flowEntry {@link FlowEntry}
* @return gRPC message
*/
public static FlowEntryProto translate(FlowEntry flowEntry) {
@@ -54,10 +53,10 @@
}
/**
- * Translates gRPC FlowRule to {@link FlowRule}.
+ * Translates gRPC FlowEntry to {@link FlowEntry}.
*
* @param flowEntry gRPC message
- * @return {@link FlowRule}
+ * @return {@link FlowEntry}
*/
public static FlowEntry translate(FlowEntryProto flowEntry) {
if (flowEntry.equals(FlowEntryProto.getDefaultInstance())) {
diff --git a/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/flow/FlowRuleEnumsProtoTranslator.java b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/flow/FlowRuleEnumsProtoTranslator.java
new file mode 100644
index 0000000..dcb2038
--- /dev/null
+++ b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/flow/FlowRuleEnumsProtoTranslator.java
@@ -0,0 +1,92 @@
+/*
+ * 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.FlowRuleEnumsProto;
+import org.onosproject.net.flow.FlowRule.FlowRemoveReason;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Optional;
+
+/**
+ * gRPC FlowRuleEnumsProto message to equivalent ONOS FlowRule enums conversion related utilities.
+ */
+public final class FlowRuleEnumsProtoTranslator {
+
+ private static final Logger log = LoggerFactory.getLogger(FlowRuleEnumsProtoTranslator.class);
+
+ /**
+ * Translates {@link FlowRemoveReason} to gRPC FlowRemoveReason.
+ *
+ * @param flowRemoveReason {@link FlowRemoveReason}
+ * @return gRPC message
+ */
+ public static FlowRuleEnumsProto.FlowRemoveReasonProto translate(FlowRemoveReason flowRemoveReason) {
+
+ switch (flowRemoveReason) {
+ case DELETE:
+ return FlowRuleEnumsProto.FlowRemoveReasonProto.DELETE;
+ case EVICTION:
+ return FlowRuleEnumsProto.FlowRemoveReasonProto.EVICTION;
+ case GROUP_DELETE:
+ return FlowRuleEnumsProto.FlowRemoveReasonProto.GROUP_DELETE;
+ case METER_DELETE:
+ return FlowRuleEnumsProto.FlowRemoveReasonProto.METER_DELETE;
+ case HARD_TIMEOUT:
+ return FlowRuleEnumsProto.FlowRemoveReasonProto.HARD_TIMEOUT;
+ case IDLE_TIMEOUT:
+ return FlowRuleEnumsProto.FlowRemoveReasonProto.IDLE_TIMEOUT;
+ case NO_REASON:
+ return FlowRuleEnumsProto.FlowRemoveReasonProto.NO_REASON;
+ default:
+ log.warn("Unexpected flow remove reason: {}", flowRemoveReason);
+ return FlowRuleEnumsProto.FlowRemoveReasonProto.UNRECOGNIZED;
+ }
+ }
+
+ /**
+ * Translates gRPC FlowRemoveReason to {@link FlowRemoveReason}.
+ *
+ * @param flowRemoveReason gRPC message
+ * @return {@link FlowRemoveReason}
+ */
+ public static Optional<Object> translate(FlowRuleEnumsProto.FlowRemoveReasonProto flowRemoveReason) {
+
+ switch (flowRemoveReason) {
+ case DELETE:
+ return Optional.of(FlowRemoveReason.DELETE);
+ case EVICTION:
+ return Optional.of(FlowRemoveReason.EVICTION);
+ case GROUP_DELETE:
+ return Optional.of(FlowRemoveReason.GROUP_DELETE);
+ case METER_DELETE:
+ return Optional.of(FlowRemoveReason.METER_DELETE);
+ case HARD_TIMEOUT:
+ return Optional.of(FlowRemoveReason.HARD_TIMEOUT);
+ case IDLE_TIMEOUT:
+ return Optional.of(FlowRemoveReason.IDLE_TIMEOUT);
+ case NO_REASON:
+ return Optional.of(FlowRemoveReason.NO_REASON);
+ default:
+ log.warn("Unexpected flow remove reason: {}", flowRemoveReason);
+ return Optional.empty();
+ }
+ }
+
+ // Utility class not intended for instantiation.
+ private FlowRuleEnumsProtoTranslator() {}
+}
diff --git a/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/flow/FlowRuleProtoTranslator.java b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/flow/FlowRuleProtoTranslator.java
new file mode 100644
index 0000000..2babd83
--- /dev/null
+++ b/incubator/protobuf/models/src/main/java/org/onosproject/incubator/protobuf/models/net/flow/FlowRuleProtoTranslator.java
@@ -0,0 +1,97 @@
+/*
+ * 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.FlowRuleProtoOuterClass.FlowRuleProto;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.flow.DefaultFlowRule;
+import org.onosproject.net.flow.FlowRule;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * gRPC FlowRuleProto message to equivalent ONOS FlowRule conversion related utilities.
+ */
+public final class FlowRuleProtoTranslator {
+
+ private static final Logger log = LoggerFactory.getLogger(FlowRuleProtoTranslator.class);
+
+ /**
+ * Translates {@link FlowRule} to gRPC FlowRuleProto.
+ *
+ * @param flowRule {@link FlowRule}
+ * @return gRPC message
+ */
+ public static FlowRuleProto translate(FlowRule flowRule) {
+
+ if (flowRule != null) {
+ FlowRuleProto.Builder builder = FlowRuleProto.newBuilder();
+ builder.setAppId(flowRule.appId())
+ .setDeviceId(flowRule.deviceId().toString())
+ .setFlowId(flowRule.id().value())
+ .setPermanent(flowRule.isPermanent())
+ .setPriority(flowRule.priority())
+ .setReason(FlowRuleEnumsProtoTranslator.translate(flowRule.reason()))
+ .setTableId(flowRule.tableId())
+ .setTableName(flowRule.table().toString());
+
+ // TODO: need to set TrafficTreatment and TrafficSelector
+
+ return builder.build();
+ }
+ return FlowRuleProto.getDefaultInstance();
+ }
+
+ /**
+ * Translates gRPC FlowRule to {@link FlowRule}.
+ *
+ * @param flowRule gRPC message
+ * @return {@link FlowRule}
+ */
+ public static FlowRule translate(FlowRuleProto flowRule) {
+
+ if (flowRule.equals(FlowRuleProto.getDefaultInstance())) {
+ return null;
+ }
+
+ DeviceId deviceId = DeviceId.deviceId(flowRule.getDeviceId());
+
+ // TODO: to register AppId need to find a way to get CoreService
+
+ FlowRule.FlowRemoveReason reason = (FlowRule.FlowRemoveReason)
+ FlowRuleEnumsProtoTranslator.translate(flowRule.getReason()).get();
+
+ FlowRule.Builder resultBuilder = new DefaultFlowRule.Builder();
+ resultBuilder.forDevice(deviceId);
+ resultBuilder.forTable(flowRule.getTableId());
+ resultBuilder.withPriority(flowRule.getPriority());
+ resultBuilder.withCookie(flowRule.getFlowId());
+ resultBuilder.withReason(reason);
+
+ if (flowRule.getPermanent()) {
+ resultBuilder.makePermanent();
+ } else {
+ resultBuilder.makeTemporary(flowRule.getTimeout());
+ }
+
+ // TODO: need to deal with TrafficTreatment and TrafficSelector
+
+ return resultBuilder.build();
+ }
+
+ // Utility class not intended for instantiation.
+ private FlowRuleProtoTranslator() {}
+}