Exemplo n.º 1
0
  @Override
  public int addRule(FlowEntry fe) throws ConfigError {
    Connection conn = null;
    PreparedStatement ps = null;
    PreparedStatement slice = null;
    PreparedStatement queues = null;
    ResultSet set = null;
    try {
      conn = settings.getConnection();

      int sliceid = -1;
      int ruleid = -1;
      int wildcards = -1;

      wildcards = fe.getRuleMatch().getWildcards();
      ps = conn.prepareStatement(SFLOWMAP, Statement.RETURN_GENERATED_KEYS);
      ps.setLong(1, fe.getDpid());
      ps.setInt(2, fe.getPriority());
      ps.setShort(3, fe.getRuleMatch().getInputPort());
      if ((wildcards & FVMatch.OFPFW_DL_VLAN) != 0) ps.setNull(4, Types.SMALLINT);
      else ps.setShort(4, fe.getRuleMatch().getDataLayerVirtualLan());

      if ((wildcards & FVMatch.OFPFW_DL_VLAN_PCP) != 0) ps.setNull(5, Types.SMALLINT);
      else ps.setShort(5, fe.getRuleMatch().getDataLayerVirtualLanPriorityCodePoint());

      if ((wildcards & FVMatch.OFPFW_DL_SRC) != 0) ps.setNull(6, Types.BIGINT);
      else ps.setLong(6, FlowSpaceUtil.toLong(fe.getRuleMatch().getDataLayerSource()));

      if ((wildcards & FVMatch.OFPFW_DL_DST) != 0) ps.setNull(7, Types.BIGINT);
      else ps.setLong(7, FlowSpaceUtil.toLong(fe.getRuleMatch().getDataLayerDestination()));

      if ((wildcards & FVMatch.OFPFW_DL_TYPE) != 0) ps.setNull(8, Types.SMALLINT);
      else ps.setShort(8, fe.getRuleMatch().getDataLayerType());

      if ((wildcards & FVMatch.OFPFW_NW_SRC_ALL) != 0) ps.setNull(9, Types.INTEGER);
      else ps.setInt(9, fe.getRuleMatch().getNetworkSource());

      if ((wildcards & FVMatch.OFPFW_NW_DST_ALL) != 0) ps.setNull(10, Types.INTEGER);
      else ps.setInt(10, fe.getRuleMatch().getNetworkDestination());

      if ((wildcards & FVMatch.OFPFW_NW_PROTO) != 0) ps.setNull(11, Types.SMALLINT);
      else ps.setShort(11, fe.getRuleMatch().getNetworkProtocol());

      if ((wildcards & FVMatch.OFPFW_NW_TOS) != 0) ps.setNull(12, Types.SMALLINT);
      else ps.setShort(12, fe.getRuleMatch().getNetworkTypeOfService());

      if ((wildcards & FVMatch.OFPFW_TP_SRC) != 0) ps.setNull(13, Types.SMALLINT);
      else ps.setShort(13, fe.getRuleMatch().getTransportSource());

      if ((wildcards & FVMatch.OFPFW_TP_DST) != 0) ps.setNull(14, Types.SMALLINT);
      else ps.setShort(14, fe.getRuleMatch().getTransportDestination());

      ps.setInt(15, (int) fe.getForcedQueue());
      ps.setInt(16, wildcards);
      ps.executeUpdate();
      set = ps.getGeneratedKeys();
      set.next();
      ruleid = set.getInt(1);
      slice = conn.prepareStatement(GSLICEID);
      ps = conn.prepareStatement(SACTIONS);
      for (OFAction act : fe.getActionsList()) {
        slice.setString(1, ((SliceAction) act).getSliceName());
        set = slice.executeQuery();
        if (set.next()) sliceid = set.getInt("id");
        else {
          FVLog.log(
              LogLevel.WARN,
              null,
              "Slice name " + ((SliceAction) act).getSliceName() + " does not exist... skipping.");
          continue;
        }

        ps.setInt(1, ruleid);
        ps.setInt(2, sliceid);
        ps.setInt(3, ((SliceAction) act).getSlicePerms());
        // ps.setInt(4, fe.getQueueId());
        ps.executeUpdate();
      }

      queues = conn.prepareStatement(SQUEUES);
      queues.setInt(1, ruleid);
      for (Integer queue_id : fe.getQueueId()) {
        queues.setInt(2, queue_id);
        queues.executeUpdate();
      }
      return ruleid;
    } catch (SQLException e) {
      FVLog.log(LogLevel.DEBUG, null, e.getMessage());
      throw new ConfigError("Unable to set the flowmap in db");
    } finally {
      close(set);
      close(ps);
      close(slice);
      close(queues);
      close(conn);
    }
  }