/** Initializes mocks */
 @Before
 public void startUp() {
   MockitoAnnotations.initMocks(this);
   when(sc.getPrimaryConductor()).thenReturn(conductor);
   when(conductor.getVersion()).thenReturn((short) EncodeConstants.OF13_VERSION_ID);
   when(sc.getFeatures()).thenReturn(features);
   when(features.getDatapathId()).thenReturn(new BigInteger("42"));
 }
  @Override
  public List<DataObject> translate(
      final SwitchConnectionDistinguisher cookie, final SessionContext sc, final OfHeader msg) {

    List<DataObject> listDataObject = new CopyOnWriteArrayList<DataObject>();

    OpenflowVersion ofVersion = OpenflowVersion.get(sc.getPrimaryConductor().getVersion());

    if (msg instanceof MultipartReplyMessage) {
      MultipartReplyMessage mpReply = (MultipartReplyMessage) msg;
      NodeId node = MultipartReplyTranslator.nodeIdFromDatapathId(sc.getFeatures().getDatapathId());
      switch (mpReply.getType()) {
        case OFPMPFLOW:
          {
            logger.debug(
                "Received flow statistics reponse from openflow {} switch",
                msg.getVersion() == 1 ? "1.0" : "1.3+");
            FlowsStatisticsUpdateBuilder message = new FlowsStatisticsUpdateBuilder();
            message.setId(node);
            message.setMoreReplies(mpReply.getFlags().isOFPMPFREQMORE());
            message.setTransactionId(generateTransactionId(mpReply.getXid()));
            MultipartReplyFlowCase caseBody =
                (MultipartReplyFlowCase) mpReply.getMultipartReplyBody();
            MultipartReplyFlow replyBody = caseBody.getMultipartReplyFlow();
            message.setFlowAndStatisticsMapList(
                flowStatsConvertor.toSALFlowStatsList(
                    replyBody.getFlowStats(), sc.getFeatures().getDatapathId(), ofVersion));

            logger.debug("Converted flow statistics : {}", message.build().toString());
            listDataObject.add(message.build());
            return listDataObject;
          }
        case OFPMPAGGREGATE:
          {
            logger.debug(
                "Received aggregate flow statistics reponse from openflow {} switch",
                msg.getVersion() == 1 ? "1.0" : "1.3+");
            AggregateFlowStatisticsUpdateBuilder message =
                new AggregateFlowStatisticsUpdateBuilder();
            message.setId(node);
            message.setMoreReplies(mpReply.getFlags().isOFPMPFREQMORE());
            message.setTransactionId(generateTransactionId(mpReply.getXid()));

            MultipartReplyAggregateCase caseBody =
                (MultipartReplyAggregateCase) mpReply.getMultipartReplyBody();
            MultipartReplyAggregate replyBody = caseBody.getMultipartReplyAggregate();
            message.setByteCount(new Counter64(replyBody.getByteCount()));
            message.setPacketCount(new Counter64(replyBody.getPacketCount()));
            message.setFlowCount(new Counter32(replyBody.getFlowCount()));

            logger.debug("Converted aggregate flow statistics : {}", message.build().toString());
            listDataObject.add(message.build());
            return listDataObject;
          }
        case OFPMPPORTSTATS:
          {
            logger.debug("Received port statistics multipart response");

            NodeConnectorStatisticsUpdateBuilder message =
                new NodeConnectorStatisticsUpdateBuilder();
            message.setId(node);
            message.setMoreReplies(mpReply.getFlags().isOFPMPFREQMORE());
            message.setTransactionId(generateTransactionId(mpReply.getXid()));

            MultipartReplyPortStatsCase caseBody =
                (MultipartReplyPortStatsCase) mpReply.getMultipartReplyBody();
            MultipartReplyPortStats replyBody = caseBody.getMultipartReplyPortStats();

            List<NodeConnectorStatisticsAndPortNumberMap> statsMap =
                new ArrayList<NodeConnectorStatisticsAndPortNumberMap>();
            for (PortStats portStats : replyBody.getPortStats()) {

              NodeConnectorStatisticsAndPortNumberMapBuilder statsBuilder =
                  new NodeConnectorStatisticsAndPortNumberMapBuilder();
              statsBuilder.setNodeConnectorId(
                  InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(
                      sc.getFeatures().getDatapathId(), portStats.getPortNo(), ofVersion));

              BytesBuilder bytesBuilder = new BytesBuilder();
              bytesBuilder.setReceived(portStats.getRxBytes());
              bytesBuilder.setTransmitted(portStats.getTxBytes());
              statsBuilder.setBytes(bytesBuilder.build());

              PacketsBuilder packetsBuilder = new PacketsBuilder();
              packetsBuilder.setReceived(portStats.getRxPackets());
              packetsBuilder.setTransmitted(portStats.getTxPackets());
              statsBuilder.setPackets(packetsBuilder.build());

              DurationBuilder durationBuilder = new DurationBuilder();
              if (portStats.getDurationSec() != null) {
                durationBuilder.setSecond(new Counter32(portStats.getDurationSec()));
              }
              if (portStats.getDurationNsec() != null) {
                durationBuilder.setNanosecond(new Counter32(portStats.getDurationNsec()));
              }
              statsBuilder.setDuration(durationBuilder.build());
              statsBuilder.setCollisionCount(portStats.getCollisions());
              statsBuilder.setKey(
                  new NodeConnectorStatisticsAndPortNumberMapKey(
                      statsBuilder.getNodeConnectorId()));
              statsBuilder.setReceiveCrcError(portStats.getRxCrcErr());
              statsBuilder.setReceiveDrops(portStats.getRxDropped());
              statsBuilder.setReceiveErrors(portStats.getRxErrors());
              statsBuilder.setReceiveFrameError(portStats.getRxFrameErr());
              statsBuilder.setReceiveOverRunError(portStats.getRxOverErr());
              statsBuilder.setTransmitDrops(portStats.getTxDropped());
              statsBuilder.setTransmitErrors(portStats.getTxErrors());

              statsMap.add(statsBuilder.build());
            }
            message.setNodeConnectorStatisticsAndPortNumberMap(statsMap);

            logger.debug("Converted ports statistics : {}", message.build().toString());

            listDataObject.add(message.build());
            return listDataObject;
          }
        case OFPMPGROUP:
          {
            logger.debug("Received group statistics multipart reponse");
            GroupStatisticsUpdatedBuilder message = new GroupStatisticsUpdatedBuilder();
            message.setId(node);
            message.setMoreReplies(mpReply.getFlags().isOFPMPFREQMORE());
            message.setTransactionId(generateTransactionId(mpReply.getXid()));
            MultipartReplyGroupCase caseBody =
                (MultipartReplyGroupCase) mpReply.getMultipartReplyBody();
            MultipartReplyGroup replyBody = caseBody.getMultipartReplyGroup();
            message.setGroupStats(
                groupStatsConvertor.toSALGroupStatsList(replyBody.getGroupStats()));

            logger.debug("Converted group statistics : {}", message.toString());
            listDataObject.add(message.build());
            return listDataObject;
          }
        case OFPMPGROUPDESC:
          {
            logger.debug("Received group description statistics multipart reponse");

            GroupDescStatsUpdatedBuilder message = new GroupDescStatsUpdatedBuilder();
            message.setId(node);
            message.setMoreReplies(mpReply.getFlags().isOFPMPFREQMORE());
            message.setTransactionId(generateTransactionId(mpReply.getXid()));
            MultipartReplyGroupDescCase caseBody =
                (MultipartReplyGroupDescCase) mpReply.getMultipartReplyBody();
            MultipartReplyGroupDesc replyBody = caseBody.getMultipartReplyGroupDesc();

            message.setGroupDescStats(
                groupStatsConvertor.toSALGroupDescStatsList(replyBody.getGroupDesc(), ofVersion));

            logger.debug("Converted group statistics : {}", message.toString());
            listDataObject.add(message.build());
            return listDataObject;
          }
        case OFPMPGROUPFEATURES:
          {
            logger.debug("Received group features multipart reponse");
            GroupFeaturesUpdatedBuilder message = new GroupFeaturesUpdatedBuilder();
            message.setId(node);
            message.setMoreReplies(mpReply.getFlags().isOFPMPFREQMORE());
            message.setTransactionId(generateTransactionId(mpReply.getXid()));
            MultipartReplyGroupFeaturesCase caseBody =
                (MultipartReplyGroupFeaturesCase) mpReply.getMultipartReplyBody();
            MultipartReplyGroupFeatures replyBody = caseBody.getMultipartReplyGroupFeatures();
            List<Class<? extends GroupType>> supportedGroups =
                new ArrayList<Class<? extends GroupType>>();

            if (replyBody.getTypes().isOFPGTALL()) {
              supportedGroups.add(GroupAll.class);
            }
            if (replyBody.getTypes().isOFPGTSELECT()) {
              supportedGroups.add(GroupSelect.class);
            }
            if (replyBody.getTypes().isOFPGTINDIRECT()) {
              supportedGroups.add(GroupIndirect.class);
            }
            if (replyBody.getTypes().isOFPGTFF()) {
              supportedGroups.add(GroupFf.class);
            }
            message.setGroupTypesSupported(supportedGroups);
            message.setMaxGroups(replyBody.getMaxGroups());

            List<Class<? extends GroupCapability>> supportedCapabilities =
                new ArrayList<Class<? extends GroupCapability>>();

            if (replyBody.getCapabilities().isOFPGFCCHAINING()) {
              supportedCapabilities.add(Chaining.class);
            }
            if (replyBody.getCapabilities().isOFPGFCCHAININGCHECKS()) {
              supportedCapabilities.add(ChainingChecks.class);
            }
            if (replyBody.getCapabilities().isOFPGFCSELECTLIVENESS()) {
              supportedCapabilities.add(SelectLiveness.class);
            }
            if (replyBody.getCapabilities().isOFPGFCSELECTWEIGHT()) {
              supportedCapabilities.add(SelectWeight.class);
            }

            message.setGroupCapabilitiesSupported(supportedCapabilities);

            message.setActions(getGroupActionsSupportBitmap(replyBody.getActionsBitmap()));
            listDataObject.add(message.build());

            return listDataObject;
          }
        case OFPMPMETER:
          {
            logger.debug("Received meter statistics multipart reponse");
            MeterStatisticsUpdatedBuilder message = new MeterStatisticsUpdatedBuilder();
            message.setId(node);
            message.setMoreReplies(mpReply.getFlags().isOFPMPFREQMORE());
            message.setTransactionId(generateTransactionId(mpReply.getXid()));

            MultipartReplyMeterCase caseBody =
                (MultipartReplyMeterCase) mpReply.getMultipartReplyBody();
            MultipartReplyMeter replyBody = caseBody.getMultipartReplyMeter();
            message.setMeterStats(
                meterStatsConvertor.toSALMeterStatsList(replyBody.getMeterStats()));

            listDataObject.add(message.build());
            return listDataObject;
          }
        case OFPMPMETERCONFIG:
          {
            logger.debug("Received meter config statistics multipart reponse");

            MeterConfigStatsUpdatedBuilder message = new MeterConfigStatsUpdatedBuilder();
            message.setId(node);
            message.setMoreReplies(mpReply.getFlags().isOFPMPFREQMORE());
            message.setTransactionId(generateTransactionId(mpReply.getXid()));

            MultipartReplyMeterConfigCase caseBody =
                (MultipartReplyMeterConfigCase) mpReply.getMultipartReplyBody();
            MultipartReplyMeterConfig replyBody = caseBody.getMultipartReplyMeterConfig();
            message.setMeterConfigStats(
                meterStatsConvertor.toSALMeterConfigList(replyBody.getMeterConfig()));

            listDataObject.add(message.build());
            return listDataObject;
          }
        case OFPMPMETERFEATURES:
          {
            logger.debug("Received meter features multipart reponse");
            // Convert OF message and send it to SAL listener
            MeterFeaturesUpdatedBuilder message = new MeterFeaturesUpdatedBuilder();
            message.setId(node);
            message.setMoreReplies(mpReply.getFlags().isOFPMPFREQMORE());
            message.setTransactionId(generateTransactionId(mpReply.getXid()));

            MultipartReplyMeterFeaturesCase caseBody =
                (MultipartReplyMeterFeaturesCase) mpReply.getMultipartReplyBody();
            MultipartReplyMeterFeatures replyBody = caseBody.getMultipartReplyMeterFeatures();
            message.setMaxBands(replyBody.getMaxBands());
            message.setMaxColor(replyBody.getMaxColor());
            message.setMaxMeter(new Counter32(replyBody.getMaxMeter()));

            List<Class<? extends MeterCapability>> supportedCapabilities =
                new ArrayList<Class<? extends MeterCapability>>();
            if (replyBody.getCapabilities().isOFPMFBURST()) {
              supportedCapabilities.add(MeterBurst.class);
            }
            if (replyBody.getCapabilities().isOFPMFKBPS()) {
              supportedCapabilities.add(MeterKbps.class);
            }
            if (replyBody.getCapabilities().isOFPMFPKTPS()) {
              supportedCapabilities.add(MeterPktps.class);
            }
            if (replyBody.getCapabilities().isOFPMFSTATS()) {
              supportedCapabilities.add(MeterStats.class);
            }
            message.setMeterCapabilitiesSupported(supportedCapabilities);

            List<Class<? extends MeterBand>> supportedMeterBand =
                new ArrayList<Class<? extends MeterBand>>();
            if (replyBody.getBandTypes().isOFPMBTDROP()) {
              supportedMeterBand.add(MeterBandDrop.class);
            }
            if (replyBody.getBandTypes().isOFPMBTDSCPREMARK()) {
              supportedMeterBand.add(MeterBandDscpRemark.class);
            }
            message.setMeterBandSupported(supportedMeterBand);
            listDataObject.add(message.build());

            return listDataObject;
          }
        case OFPMPTABLE:
          {
            logger.debug(
                "Received flow table statistics reponse from openflow {} switch",
                msg.getVersion() == 1 ? "1.0" : "1.3+");

            FlowTableStatisticsUpdateBuilder message = new FlowTableStatisticsUpdateBuilder();
            message.setId(node);
            message.setMoreReplies(mpReply.getFlags().isOFPMPFREQMORE());
            message.setTransactionId(generateTransactionId(mpReply.getXid()));

            MultipartReplyTableCase caseBody =
                (MultipartReplyTableCase) mpReply.getMultipartReplyBody();
            MultipartReplyTable replyBody = caseBody.getMultipartReplyTable();
            List<TableStats> swTablesStats = replyBody.getTableStats();

            List<FlowTableAndStatisticsMap> salFlowStats =
                new ArrayList<FlowTableAndStatisticsMap>();
            for (TableStats swTableStats : swTablesStats) {
              FlowTableAndStatisticsMapBuilder statisticsBuilder =
                  new FlowTableAndStatisticsMapBuilder();

              statisticsBuilder.setActiveFlows(new Counter32(swTableStats.getActiveCount()));
              statisticsBuilder.setPacketsLookedUp(new Counter64(swTableStats.getLookupCount()));
              statisticsBuilder.setPacketsMatched(new Counter64(swTableStats.getMatchedCount()));
              statisticsBuilder.setTableId(new TableId(swTableStats.getTableId()));
              salFlowStats.add(statisticsBuilder.build());
            }

            message.setFlowTableAndStatisticsMap(salFlowStats);

            logger.debug("Converted flow table statistics : {}", message.build().toString());
            listDataObject.add(message.build());
            return listDataObject;
          }
        case OFPMPQUEUE:
          {
            logger.debug("Received queue statistics multipart response");

            QueueStatisticsUpdateBuilder message = new QueueStatisticsUpdateBuilder();
            message.setId(node);
            message.setMoreReplies(mpReply.getFlags().isOFPMPFREQMORE());
            message.setTransactionId(generateTransactionId(mpReply.getXid()));

            MultipartReplyQueueCase caseBody =
                (MultipartReplyQueueCase) mpReply.getMultipartReplyBody();
            MultipartReplyQueue replyBody = caseBody.getMultipartReplyQueue();

            List<QueueIdAndStatisticsMap> statsMap = new ArrayList<QueueIdAndStatisticsMap>();

            for (QueueStats queueStats : replyBody.getQueueStats()) {

              QueueIdAndStatisticsMapBuilder statsBuilder = new QueueIdAndStatisticsMapBuilder();
              statsBuilder.setNodeConnectorId(
                  InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(
                      sc.getFeatures().getDatapathId(), queueStats.getPortNo(), ofVersion));
              statsBuilder.setTransmissionErrors(new Counter64(queueStats.getTxErrors()));
              statsBuilder.setTransmittedBytes(new Counter64(queueStats.getTxBytes()));
              statsBuilder.setTransmittedPackets(new Counter64(queueStats.getTxPackets()));

              DurationBuilder durationBuilder = new DurationBuilder();
              durationBuilder.setSecond(new Counter32(queueStats.getDurationSec()));
              durationBuilder.setNanosecond(new Counter32(queueStats.getDurationNsec()));
              statsBuilder.setDuration(durationBuilder.build());

              statsBuilder.setQueueId(new QueueId(queueStats.getQueueId()));
              statsBuilder.setNodeConnectorId(
                  InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(
                      sc.getFeatures().getDatapathId(), queueStats.getPortNo(), ofVersion));

              statsMap.add(statsBuilder.build());
            }
            message.setQueueIdAndStatisticsMap(statsMap);

            logger.debug("Converted queue statistics : {}", message.build().toString());
            listDataObject.add(message.build());
            return listDataObject;
          }

        default:
          return listDataObject;
      }
    }

    return listDataObject;
  }