Refactor OpenstackTelemetry App for better readability
Change-Id: I93353de31fb9671d8670ee44fc248fe7f36ac12b
diff --git a/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/codec/TinaFlowInfoByteBufferCodec.java b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/codec/TinaFlowInfoByteBufferCodec.java
index 35547b2..8b28797 100644
--- a/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/codec/TinaFlowInfoByteBufferCodec.java
+++ b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/codec/TinaFlowInfoByteBufferCodec.java
@@ -35,14 +35,19 @@
public class TinaFlowInfoByteBufferCodec extends ByteBufferCodec<FlowInfo> {
private static final int MESSAGE_SIZE = 88;
+ private static final String OF_PREFIX = "of:";
@Override
public ByteBuffer encode(FlowInfo flowInfo) {
ByteBuffer byteBuffer = ByteBuffer.allocate(MESSAGE_SIZE);
+ String deviceId = flowInfo.deviceId().toString();
+ short switchId = (short) Integer.parseInt(deviceId.substring(3,
+ deviceId.length()), 16);
+
byteBuffer.put(flowInfo.flowType())
- .putShort(Short.valueOf(flowInfo.deviceId().toString()))
+ .putShort(switchId)
.putInt(flowInfo.inputInterfaceId())
.putInt(flowInfo.outputInterfaceId())
.putShort(flowInfo.vlanId().toShort())
@@ -67,7 +72,8 @@
public FlowInfo decode(ByteBuffer byteBuffer) {
byte flowType = byteBuffer.get();
- DeviceId deviceId = DeviceId.deviceId(String.valueOf(byteBuffer.getShort()));
+ String deviceIdStr = String.format("%016x", byteBuffer.getShort());
+ DeviceId deviceId = DeviceId.deviceId(OF_PREFIX + deviceIdStr);
int inputInterfaceId = byteBuffer.getInt();
int outputInterfaceId = byteBuffer.getInt();
VlanId vlanId = VlanId.vlanId(byteBuffer.getShort());
diff --git a/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/codec/TinaMessageByteBufferCodec.java b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/codec/TinaMessageByteBufferCodec.java
new file mode 100644
index 0000000..3b849d1
--- /dev/null
+++ b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/codec/TinaMessageByteBufferCodec.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2018-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.openstacktelemetry.codec;
+
+import org.onosproject.openstacktelemetry.api.FlowInfo;
+
+import java.nio.ByteBuffer;
+import java.util.Set;
+
+/**
+ * Tina Message ByteBuffer Codec.
+ */
+public class TinaMessageByteBufferCodec {
+
+ private static final int HEADER_SIZE = 8;
+ private static final int ENTRY_SIZE = 88;
+ private static final int MILLISECONDS = 1000;
+ private static final short KAFKA_MESSAGE_TYPE = 1;
+
+ /**
+ * Encodes a collection flow infos into byte buffer.
+ *
+ * @param flowInfos a collection of flow info
+ * @return encoded byte buffer
+ */
+ public ByteBuffer encode(Set<FlowInfo> flowInfos) {
+ ByteBuffer byteBuffer =
+ ByteBuffer.allocate(HEADER_SIZE + flowInfos.size() * ENTRY_SIZE);
+
+ byteBuffer.put(buildMessageHeader(flowInfos));
+ byteBuffer.put(buildMessageBody(flowInfos));
+
+ return byteBuffer;
+ }
+
+ private byte[] buildMessageHeader(Set<FlowInfo> flowInfos) {
+ ByteBuffer byteBuffer = ByteBuffer.allocate(HEADER_SIZE);
+
+ byteBuffer.putShort((short) flowInfos.size());
+ byteBuffer.putShort(KAFKA_MESSAGE_TYPE);
+ byteBuffer.putInt((int) (System.currentTimeMillis() / MILLISECONDS));
+
+ return byteBuffer.array();
+ }
+
+ private byte[] buildMessageBody(Set<FlowInfo> flowInfos) {
+ ByteBuffer byteBuffer = ByteBuffer.allocate(flowInfos.size() * ENTRY_SIZE);
+
+ TinaFlowInfoByteBufferCodec codec = new TinaFlowInfoByteBufferCodec();
+ flowInfos.forEach(flowInfo -> byteBuffer.put(codec.encode(flowInfo)));
+
+ return byteBuffer.array();
+ }
+}